Lesson 4.3. Checkout Fields and Logic

Friday, 6:20 PM

The client: "The minimum order is 300 UAH. Why is the courier delivering an order for 45?" The amount check sits in woocommerce_checkout_process. Block checkout doesn't call this hook, so orders of any amount go through without a single error.

The rule is in the code, but it only works in the old checkout form, which the site no longer has.

Goal

After the lesson you add fields, validation, fees and a minimum amount so that they work in both classic and block checkout.

Key concepts

  • woocommerce_register_additional_checkout_field() — fields for block checkout (stable since WooCommerce 8.9), registered on woocommerce_init
  • Locations: contact, address, order; types: text, select, checkbox
  • Validation: the woocommerce_validate_additional_field hook or a validation callback in the field settings
  • The value is stored in order meta; read it through the fields API instead of guessing the key
  • Fees: woocommerce_cart_calculate_fees + $cart->add_fee() — works in both checkouts
  • Cart restrictions (minimum amount): an error in the cart blocks checkout

Diagram

flowchart TB
  R["woocommerce_init<br/>register the field"] --> UI["Checkout block<br/>shows the field"]
  UI --> SA["Store API /checkout"]
  SA --> V{"Validation"}
  V -- error --> UI
  V -- ok --> M["Order meta"]
  M --> E["Email, admin, REST"]

Code

A company tax ID field (Ukrainian IPN / EDRPOU):

PHP
<?php
add_action( 'woocommerce_init', static function () {
	if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) {
		return;
	}
	woocommerce_register_additional_checkout_field( [
		'id'       => 'my-plugin/tax-id',
		'label'    => __( 'Tax ID (IPN / EDRPOU)', 'my-plugin' ),
		'location' => 'order',
		'type'     => 'text',
		'required' => false,
	] );
} );

add_action( 'woocommerce_validate_additional_field', static function ( WP_Error $errors, string $key, $value ) {
	if ( 'my-plugin/tax-id' === $key && '' !== $value && ! preg_match( '/^\d{8}(\d{2})?$/', $value ) ) {
		$errors->add( 'invalid_tax_id', __( 'IPN has 10 digits, EDRPOU has 8.', 'my-plugin' ) );
	}
}, 10, 3 );

A cash on delivery fee — works in both checkouts:

PHP
<?php
add_action( 'woocommerce_cart_calculate_fees', static function ( WC_Cart $cart ) {
	if ( is_admin() && ! wp_doing_ajax() ) {
		return;
	}
	$method = WC()->session ? WC()->session->get( 'chosen_payment_method' ) : '';
	if ( 'cod' === $method ) {
		$cart->add_fee( __( 'Cash on delivery fee', 'my-plugin' ), 30, true );
	}
} );

Common mistakes

SymptomCauseFix
The field doesn't appearRegistered before woocommerce_init or an old WooCommerce versionRegister on woocommerce_init, check function_exists
A payment method fee doesn't update in blocksBlock checkout doesn't recalculate the cart when the payment method changesCheck the behavior on your version; be careful with fees that depend on payment
The fee is added twiceIt's added in several hooksOnly woocommerce_cart_calculate_fees

Weak and strong answer

Question: How do you add a minimum order amount?

Weak answer. Check the amount on the checkout page and show a message.

Strong answer. The check must be on the server, because checkout can also arrive through the Store API. For both checkouts I add a cart error through woocommerce_check_cart_items with wc_add_notice( ..., 'error' ) — the error blocks checkout. I test it in classic and block checkout and with a direct Store API request.

Why the second is stronger: The candidate thinks about a bypass through the API and tests both checkouts.

Practice

Two separate cases: first fix the code, then check it with direct requests.

Case 1. A minimum amount for both checkouts (IDE)

Rewrite the minimum amount check from the start of the lesson so that it fires in both classic and block checkout.

TEXT

Case 2. Direct Store API requests (terminal)

Check the scenarios the way block checkout sees them: an order below the minimum, an invalid and a valid tax ID, the cash on delivery fee in the order. On your own site, add the tax ID field from the Code section and show it in the admin email.

curl / wp — Module 4. Cart and Checkout

Interview Questions

With the Additional Checkout Fields API: woocommerce_register_additional_checkout_field() on the woocommerce_init hook, with an id in the namespace/name format, a location and a field type.

woocommerce_cart_calculate_fees and $cart->add_fee( $name, $amount, $taxable ). Fees are calculated on the server, so they work in both checkouts.

A check in woocommerce_check_cart_items with an error through wc_add_notice(): a cart with an error won't pass checkout, neither in the template nor through the Store API.

Save your progress

Sign in or create a free account to mark lessons as completed and continue the whole course. You will come back to this lesson.