Skip to content

CUI in WooCommerce checkout: company details filled in from ANAF

· The AYT team · 6 min read

Person tying string around a cardboard parcel on a white table

WooCommerce has no fields for a Romanian company's tax ID (CUI) or Trade Register number out of the box, so a business buying from your store can't enter its billing details. You add them with a checkout plugin built for Romania, and the autofill comes from the public web service of ANAF (the Romanian tax authority): the customer types the CUI, and the company name, registered address and registration number come from ANAF's database.

Accurate data matters more now that B2B invoices go through e-Factura: a wrong CUI means a rejected invoice or one sent to another company. The obligations are covered in the guide to e-Factura for WooCommerce.

The fields you need

For an invoice to a Romanian company:

  • the customer type: individual or company;
  • the company name (WooCommerce already has the billing_company field);
  • the CUI, with the "RO" prefix if the company is VAT-registered;
  • the Trade Register number (in ANAF's response it's nrRegCom);
  • the registered office address, which can differ from the delivery address.

For individuals, don't add a field for the personal number (CNP). On a B2C invoice the CNP isn't required: when it's missing, the invoice carries 13 zeros in its place (OUG 120/2021, art. 10^1).

Step 1: add the fields with a plugin

The most widely used free plugin for this is Facturare WooCommerce (over 3,000 active installs). It adds the individual / company choice, CUI and Trade Register fields with validation, and shows the company fields only when the customer picks company. Romanian invoicing plugins (SmartBill, Oblio) can read the data it saves.

Two things to check first:

  1. Which checkout you use. New stores use the block-based checkout (Checkout Block); older ones use the [woocommerce_checkout] shortcode. Facturare WooCommerce shows its fields in the block checkout only on WooCommerce 9.9 and later.
  2. Compatibility with your invoicing plugin: place a test order as a company and check that the CUI appears on the invoice.

The plugin doesn't look anything up in ANAF. For that you need a separate plugin or a little code, as below.

Step 2: autofill from ANAF

ANAF offers a free web service for looking up companies by CUI. The current version is v9, at https://webservicesp.anaf.ro/api/PlatitorTvaRest/v9/tva. You POST a JSON list with the CUI and the date you want data for, and get back the name, Trade Register number, registered address and whether the company is VAT-registered. The official details are in ANAF's web service documentation.

Two limits to keep in mind: the service takes at most 100 CUIs per request and one request per second. That's why the request should come from your server, with the answer cached.

With a plugin

On wordpress.org there are plugins that fill in company details from the CUI, some straight from ANAF, others through an intermediary service. At the time of writing they all have few installs, so pick one that declares support for your checkout type and test it on a staging site. Also check that it saves the data in the same fields your invoicing plugin reads.

With code (classic checkout)

If you use the shortcode checkout, a little code is enough. The PHP below goes in a small custom plugin or in your child theme's functions.php. It takes the CUI, asks ANAF and caches the answer for a day:

add_action( 'wp_ajax_ayt_cui', 'ayt_cui_lookup' );
add_action( 'wp_ajax_nopriv_ayt_cui', 'ayt_cui_lookup' );

function ayt_cui_lookup() {
    $cui = preg_replace( '/\D/', '', sanitize_text_field( wp_unslash( $_GET['cui'] ?? '' ) ) );
    if ( strlen( $cui ) < 2 || strlen( $cui ) > 10 ) {
        wp_send_json_error();
    }
    $firma = get_transient( 'ayt_cui_' . $cui );
    if ( false === $firma ) {
        $raspuns = wp_remote_post( 'https://webservicesp.anaf.ro/api/PlatitorTvaRest/v9/tva', array(
            'headers' => array( 'Content-Type' => 'application/json' ),
            'body'    => wp_json_encode( array( array( 'cui' => (int) $cui, 'data' => wp_date( 'Y-m-d' ) ) ) ),
            'timeout' => 8,
        ) );
        $date = json_decode( wp_remote_retrieve_body( $raspuns ), true );
        $f    = $date['found'][0] ?? null;
        if ( ! $f ) {
            wp_send_json_error();
        }
        $s     = $f['adresa_sediu_social'];
        $firma = array(
            'nume'   => $f['date_generale']['denumire'],
            'regcom' => $f['date_generale']['nrRegCom'],
            'tva'    => (bool) $f['inregistrare_scop_Tva']['scpTVA'],
            'adresa' => trim( $s['sdenumire_Strada'] . ' ' . $s['snumar_Strada'] . ' ' . $s['sdetalii_Adresa'] ),
            'oras'   => $s['sdenumire_Localitate'],
            'judet'  => $s['scod_JudetAuto'],
        );
        set_transient( 'ayt_cui_' . $cui, $firma, DAY_IN_SECONDS );
    }
    wp_send_json_success( $firma );
}

The JavaScript listens to the CUI field and fills in the rest. Put it in a cui.js file in the child theme and load it only on the checkout page, with wc-checkout as a dependency:

add_action( 'wp_enqueue_scripts', function () {
    if ( is_checkout() ) {
        wp_enqueue_script( 'ayt-cui', get_stylesheet_directory_uri() . '/cui.js', array( 'wc-checkout' ), '1.0', true );
    }
} );

In cui.js, replace #billing_cui and #billing_regcom with the IDs of the fields your plugin creates:

jQuery( function ( $ ) {
    $( document.body ).on( 'change', '#billing_cui', function () {
        $.getJSON( wc_checkout_params.ajax_url, { action: 'ayt_cui', cui: this.value }, function ( r ) {
            if ( ! r.success ) return;
            $( '#billing_company' ).val( r.data.nume );
            $( '#billing_regcom' ).val( r.data.regcom );
            $( '#billing_address_1' ).val( r.data.adresa );
            $( '#billing_city' ).val( r.data.oras );
            $( '#billing_state' ).val( r.data.judet ).trigger( 'change' );
        } );
    } );
} );

The county field fills in directly: for Romania WooCommerce uses the counties' licence-plate codes (CJ, IS, B for Bucharest), which is exactly what ANAF returns in scod_JudetAuto.

The block checkout uses neither jQuery nor these fields, so the code above does nothing there. For the block checkout, pick a plugin that explicitly supports it.

Step 3: test before going live

  1. Make the changes on a staging copy of the store.
  2. Place an order as a company with your own company's CUI and check that the name and address fill in correctly.
  3. Try a CUI that doesn't exist: the form must stay fillable by hand.
  4. Issue the invoice from your invoicing app and check the company details on it.
  5. Only then publish the changes on the live store.

Keep the fields editable after the autofill. ANAF's database shows the registered office, but some companies want to be invoiced at another address or have data newer than the service's.

Frequently asked questions

Should the CUI include "RO"?

The "RO" prefix shows that the company is VAT-registered. The tva field in the code above (scpTVA in ANAF's response) tells you whether that's the case, and the invoicing app usually adds the prefix itself. For ANAF's service you send the CUI as digits only.

Can I use the same method for companies from other EU countries?

No, ANAF's service only holds Romanian companies. VAT numbers from other member states are checked in the European Commission's VIES system.

Your checkout on AYT

On AYT's plans you create a staging copy of the store from the WordPress Toolkit and test the plugin or the code there before it goes on the live site. You edit the child theme's files in the File Manager, with its built-in code editor, without FTP. If something breaks after publishing, you restore the site from the daily backups straight from the panel.

More articles

All articles