=== Ballitech Checkout Fields for WooCommerce ===
Contributors: ballitech
Tags: cpf, cnpj, brasil, checkout, cep
Requires at least: 6.5
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Brazilian checkout fields for WooCommerce: CPF, CNPJ (alphanumeric included), state registration, and address autofill from the CEP.

== Description ==

This plugin adds the fields a Brazilian store needs at checkout and that
WooCommerce does not ship with: person type, CPF, CNPJ, state registration
(inscricao estadual), street number and district.

Three things set it apart from the plugins that came before it.

**1. It works in the block checkout.**

WooCommerce replaced its checkout with one built on blocks. Plugins written for
the old checkout simply draw nothing there: the customer reaches the payment
step and there is no CPF field at all. This plugin uses the official additional
checkout fields API, the same one WooCommerce itself uses, so the fields show up
in both checkouts without workarounds.

**2. It accepts the alphanumeric CNPJ.**

Since 31 July 2026 the Brazilian federal tax authority issues CNPJ numbers that
contain letters (Normative Instruction RFB 2.229/2024). The identifier still has
14 positions, but the first 12 may include letters; only the 2 final check
digits remain numeric. A validator that accepts digits only will reject a
legitimate company at checkout, and the sale is lost without anyone finding out
why.

The validation rule here was checked during development against 200,000 random
numeric CNPJ numbers plus six real, publicly known ones: the new algorithm
accepts everything the old one accepted, with no divergence. The plugin also
ships a diagnostics page that runs a short validator battery inside your own
store, so you can see the rule working on your installation.

**3. You can move off the old plugin without losing data.**

Stores already running *Brazilian Market on WooCommerce* have the CPF stored in
`_billing_cpf`, and the whole ecosystem reads it from there: invoicing software,
payment gateways, themes, export spreadsheets. This plugin **writes to the same
keys**, in addition to the new ones. Nothing that used to work stops working
when you switch.

Existing customer records are imported automatically, in batches, without
locking up the admin.

= Address autofill from the CEP =

Type the Brazilian postal code and the street, district and city fill
themselves, in the block checkout and in the classic one alike. The lookup uses
ViaCEP, needs no key and no account.

It only writes into fields that are still empty, so it never erases something
the customer typed. If the CEP is unknown, or ViaCEP is unreachable, nothing
happens and the person fills the address by hand: the checkout is never blocked
by a lookup. Stores that prefer to turn it off can return false from the
`ballitech_cf_buscar_cep` filter.

= Included fields =

* Person type (individual or company), which shows either CPF or CNPJ
* CPF, with check digit validation
* CNPJ, numeric and alphanumeric, with validation
* State registration (inscricao estadual)
* Street number and district, on both billing **and** shipping, so the shipping
  label comes out right
* Input masks for CPF, CNPJ, postal code and phone
* Address lookup from the Brazilian postal code (CEP)

= Compatibility =

* Block checkout and classic checkout
* HPOS (High-Performance Order Storage), declared and tested
* WooCommerce 8.9 or newer

== Is there a paid version? ==

Yes, and this plugin is complete without it. Everything described above is free
and stays free: the fields, the validation, the alphanumeric CNPJ, the migration
and the postal code lookup.

**Ballitech Checkout Fields Pro** adds one thing only: it looks the CNPJ up at the federal
tax authority and fills in company name, address, district and postal code by
itself, warning you when the company is no longer active. It is a separate
plugin, distributed outside this directory, and nothing here depends on it.

== External services ==

This plugin uses **ViaCEP** (https://viacep.com.br) to fill in street, district
and city from the postal code typed by the customer.

* **What is sent:** the postal code typed by the customer, and nothing else.
* **When:** at the moment the customer finishes typing the 8 digits, from their
  own browser. The store server does not take part in the request.
* **Terms of use and privacy policy:** https://viacep.com.br

To turn the lookup off, use this filter:

`add_filter( 'ballitech_cf_buscar_cep', '__return_false' );`

== Installation ==

1. Install and activate the plugin.
2. That is all. There is no settings screen; the fields appear at checkout.
3. If you were using *Brazilian Market on WooCommerce*, **deactivate it**. Your
   data has already been imported.
4. Open **WooCommerce > Ballitech Checkout** to confirm everything is green.

== Frequently Asked Questions ==

= The fields show up twice. What happened? =

Either the old plugin is still active, or your WooCommerce already draws the
fields on its own and compatibility mode is switched on for nothing. Open
**WooCommerce > Ballitech Checkout**: the page tells you which of the two it is, in
one line.

= Will my invoicing software keep working? =

Yes. The plugin writes to the same keys *Brazilian Market on WooCommerce* used
(`_billing_cpf`, `_billing_cnpj`, `_billing_number`, `_billing_neighborhood`),
which is where invoicing tools read from.

= What is an alphanumeric CNPJ? =

A CNPJ with letters in the first 12 positions, issued by the Brazilian federal
tax authority since 31 July 2026. Older CNPJ numbers, made of digits only, stay
valid forever: there is no migration and no renumbering.

= Do I need to configure anything? =

No. If you need to change the behaviour, the code documents the available
filters.

= Does it work with the classic (shortcode) checkout? =

Yes. The plugin detects which of the two the store uses and adapts.

== Screenshots ==

1. The fields in the block checkout.
2. Switching between individual and company.
3. The Brazilian data on the order screen in the admin.
4. The diagnostics page, with the validator battery.

== Changelog ==

= 1.0.0 =
* First release.
* Fields: person type, CPF, CNPJ, state registration, street number, district.
* CPF validation and CNPJ validation, numeric and alphanumeric (IN RFB
  2.229/2024).
* Block checkout support through the official additional fields API.
* HPOS compatibility declared.
* Import and mirroring of the Brazilian Market on WooCommerce meta keys.
* Diagnostics page with a validator battery.

== Upgrade Notice ==

= 1.0.0 =
First release.
