CVR and EAN at checkout (classic checkout)
WooCommerce has no CVR or EAN field. The billing address only has “company”, so CVR (the Danish company number) and EAN (the public-sector invoice address) must be stored as extra order data. The code below adds two optional fields to the classic checkout, checks CVR with modulus 11 and EAN with the GS1 check digit, and saves them on the order in a way that also works with WooCommerce’s new order tables (HPOS). Put it in a small plugin of your own, not in the theme’s functions.php.
<?php
/**
* Plugin Name: BC Danish B2B fields
* Description: Optional CVR and EAN fields in the WooCommerce checkout, with validation.
*/
defined( 'ABSPATH' ) || exit;
function bc_cvr_valid( string $cvr ): bool {
if ( ! preg_match( '/^\d{8}$/', $cvr ) ) {
return false;
}
$weights = array( 2, 7, 6, 5, 4, 3, 2, 1 );
$sum = 0;
foreach ( str_split( $cvr ) as $i => $digit ) {
$sum += (int) $digit * $weights[ $i ];
}
return 0 === $sum % 11;
}
function bc_ean_valid( string $ean ): bool {
if ( ! preg_match( '/^\d{13}$/', $ean ) ) {
return false;
}
$sum = 0;
for ( $i = 0; $i < 12; $i++ ) {
$sum += (int) $ean[ $i ] * ( 0 === $i % 2 ? 1 : 3 );
}
return ( 10 - $sum % 10 ) % 10 === (int) $ean[12];
}
add_filter( 'woocommerce_checkout_fields', function ( array $fields ): array {
$fields['billing']['billing_cvr'] = array(
'label' => __( 'CVR number (businesses only)', 'bc' ),
'required' => false,
'priority' => 31,
'class' => array( 'form-row-first' ),
);
$fields['billing']['billing_ean'] = array(
'label' => __( 'EAN number (public-sector customers)', 'bc' ),
'required' => false,
'priority' => 32,
'class' => array( 'form-row-last' ),
);
return $fields;
} );
add_action( 'woocommerce_after_checkout_validation', function ( array $data, WP_Error $errors ): void {
$cvr = preg_replace( '/\D/', '', (string) ( $data['billing_cvr'] ?? '' ) );
$ean = preg_replace( '/\D/', '', (string) ( $data['billing_ean'] ?? '' ) );
if ( '' !== $cvr && ! bc_cvr_valid( $cvr ) ) {
$errors->add( 'billing_cvr', __( 'The CVR number is not valid. It must be 8 digits.', 'bc' ) );
}
if ( '' !== $ean && ! bc_ean_valid( $ean ) ) {
$errors->add( 'billing_ean', __( 'The EAN number is not valid. It must be 13 digits.', 'bc' ) );
}
if ( '' !== $ean && '' === $cvr ) {
$errors->add( 'billing_cvr', __( 'Please also enter the CVR number when you use EAN.', 'bc' ) );
}
}, 10, 2 );
add_action( 'woocommerce_checkout_create_order', function ( WC_Order $order, array $data ): void {
foreach ( array( 'billing_cvr', 'billing_ean' ) as $key ) {
$value = preg_replace( '/\D/', '', (string) ( $data[ $key ] ?? '' ) );
if ( '' !== $value ) {
$order->update_meta_data( '_' . $key, $value );
}
}
}, 10, 2 );
add_action( 'woocommerce_admin_order_data_after_billing_address', function ( WC_Order $order ): void {
foreach ( array( '_billing_cvr' => 'CVR', '_billing_ean' => 'EAN' ) as $key => $label ) {
$value = $order->get_meta( $key );
if ( $value ) {
printf( '<p><strong>%s:</strong> %s</p>', esc_html( $label ), esc_html( $value ) );
}
}
} );
Modulus 11 catches typos, but not a CVR number that exists and belongs to someone else. We learned that in our own Soge Lookup plugin: an unknown CVR used to show the nearest other company. Since version 0.5.5 it accepts only an exact CVR. To check that the company exists, look the number up on the server. soge.dk (our own product) has a CVR lookup that returns name, address, postcode, town, VAT number and status, and can be used without a key at a low quota.
<?php
function bc_cvr_lookup( string $cvr ): ?array {
$response = wp_remote_get(
'https://soge.dk/api/v1/checkout/cvr/' . rawurlencode( $cvr ),
array(
'timeout' => 3,
'headers' => array( 'x-api-key' => defined( 'BC_SOGE_KEY' ) ? BC_SOGE_KEY : '' ),
)
);
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return null; // The lookup must never block an order.
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
return $body['data'] ?? null; // name, address, zipcode, city, vat, status
}
CVR field in the block checkout
If the shop uses the new block checkout, the filter above does nothing. There you register the field with WooCommerce’s additional checkout fields API, on woocommerce_init or later. The validation reuses the function from before.
<?php
add_action( 'woocommerce_init', function (): void {
if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) {
return;
}
woocommerce_register_additional_checkout_field(
array(
'id' => 'bc/cvr',
'label' => __( 'CVR number (businesses only)', 'bc' ),
'location' => 'order',
'type' => 'text',
'required' => false,
'validate_callback' => function ( $value ) {
$cvr = preg_replace( '/\D/', '', (string) $value );
if ( '' !== $cvr && ! bc_cvr_valid( $cvr ) ) {
return new WP_Error( 'bc_invalid_cvr', __( 'The CVR number is not valid.', 'bc' ) );
}
},
)
);
} );
WooCommerce stores the field on the order itself, here under the key _wc_other/bc/cvr, while the classic checkout above uses _billing_cvr. If you run both checkouts, an accounting connector needs to know both keys; check it on a test order. Our CVR Checkout (our own product, in development) covers both checkouts and the P-number.
Find outdated payment and shipping plugins with WP-CLI
A payment that stops costs more than anything else in the shop. This command lists the Danish payment, shipping and accounting plugins with the WordPress version they are tested with and when they were last updated on wordpress.org. The fields tested_up_to and wporg_last_updated exist in recent WP-CLI.
wp plugin list \
--fields=name,status,version,update_version,tested_up_to,wporg_last_updated \
--format=table \
| grep -Ei 'name|vipps|quickpay|stripe|dibs|nexi|reepay|frisbii|onpay|pensopay|shipmondo|pakkelabels|gls|postnord|smart-send|webshipper|economic|dinero|billy|postnumre'
# Only those tested with an older major version than the installed WordPress
CUR=$(wp core version | cut -d. -f1,2)
wp plugin list --fields=name,tested_up_to --format=csv | tail -n +2 \
| awk -F, -v cur="$CUR" '$2 != "" && $2 < cur { print $1 " (tested up to " $2 ")" }'
The awk comparison is a string comparison, which is good enough for 6.9 against 7.1, but read the list with your own eyes. If “pakkelabels-for-woocommerce” or “woo-postnumre” shows up, that is Shipmondo’s deprecated plugin and a plugin that still calls the closed DAWA.
VAT from the command line
wp option update woocommerce_calc_taxes yes
wp option update woocommerce_prices_include_tax yes
wp option update woocommerce_tax_display_shop incl
wp option update woocommerce_tax_display_cart incl
wp wc tax create --country=DK --rate=25.0000 --name="Moms" --shipping=true --user=1
Run it on a copy of the shop first, and place a test order afterwards. Why the settings look like this is explained on the VAT page.
Orders via the WooCommerce REST API
With a REST key (WooCommerce → Settings → Advanced → REST API, read-only for reports) you can pull orders into a spreadsheet or another system. Keep the keys in environment variables, never in a shared script.
curl -s "https://butik.example.dk/wp-json/wc/v3/orders?status=processing&per_page=20" \
-u "$WC_KEY:$WC_SECRET" \
| jq '.[] | {id, date_created, total, company: .billing.company,
cvr: ([.meta_data[] | select(.key == "_billing_cvr") | .value][0])}'
# Mark an order as completed (needs a key with write access)
curl -s -X PUT "https://butik.example.dk/wp-json/wc/v3/orders/1234" \
-u "$WC_KEY:$WC_SECRET" \
-H "Content-Type: application/json" \
-d '{"status": "completed"}'
Mistakes we made in WooCommerce
- The first variation wins. WooCommerce picks the first variation that matches. A published “any/any” variation without a price made the whole product impossible to buy.
- “Unavailable” is not “sold out”. A variation without a price looks like a combination that does not exist. Count them separately.
- The theme takes over. Porto switches its own swatches on when another swatch plugin is deactivated. That is one reason we are building Lynbro Variation Swatches (our own product, in development): an availability index for 1,200 variations takes 3.1 KB compressed, and imports from other plugins can be dry-run and undone.
- A nonce behind a long cache. Behind a 30-day page cache the HTML must not contain a nonce. Validate on the server instead.
- Dead data. In a Danish workwear webshop we maintain, 56% of the variation combinations were dead and 69% of products had no description, and the blog page was set to the same page as the shop, so the blog did not exist.
More common errors – with symptom, cause and fix – are on WooCommerce errors and questions.
Sources
- WooCommerce – Additional checkout fields — block checkout API, meta keys
- WooCommerce REST API – orders
- WP-CLI – wp plugin list — fields tested_up_to and wporg_last_updated, read 19.11.2026
- soge.dk — our own product, CVR lookup tested 19.11.2026
Questions about the code
Where should the code live?
In a small plugin of your own or a must-use plugin, not in the theme’s functions.php. That way it survives a theme change and an update.
Does modulus 11 make the lookup unnecessary?
No. Modulus 11 catches typos. A lookup tells you whether the company exists and is active. Never let the lookup block an order if the service does not answer.
Will the CVR field work with my accounting connector?
Only if the connector reads the key the field is stored under. Check it on a test order and in the connector’s settings before you go live.
Can you set it up for us?
Yes. Tell us which checkout you use and which accounting system the orders should go to, and you get a fixed price.
Fixed price, no commitment
Get a free WooCommerce check
Tell us briefly what you use today and what you want to achieve. A person answers by e-mail with a proposal and a fixed price before any work starts.
- A fixed price before we start
- An answer by e-mail, no calls
- Your details are used only to answer you