Halloween Sale 15% off Ends in See the plans
PHP

Flag Orders Whose Billing and Shipping Countries Differ

Adds a private order note, and optionally holds the order, when the billing and shipping countries do not match, a common sign of card fraud.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_mismatch_hold = false; // Also set the order to On hold.

$wpide_check_mismatch = function ( $order ) use ( $wpide_mismatch_hold ) {
    if ( ! $order instanceof WC_Order || ! $order->has_shipping_address() ) {
        return;
    }

    $billing  = $order->get_billing_country();
    $shipping = $order->get_shipping_country();

    if ( $billing && $shipping && $billing !== $shipping ) {
        $order->add_order_note( sprintf( 'Check: billing country (%1$s) and shipping country (%2$s) differ.', $billing, $shipping ) );

        if ( $wpide_mismatch_hold && $order->has_status( array( 'processing', 'pending' ) ) ) {
            $order->update_status( 'on-hold', 'Held because the countries differ.' );
        }
    }
};

add_action( 'woocommerce_checkout_order_created', $wpide_check_mismatch );
add_action( 'woocommerce_store_api_checkout_order_processed', $wpide_check_mismatch );

Notes

Works with the classic and block checkouts; set $wpide_mismatch_hold to true to also put these orders on hold.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.