Restrict WooCommerce Checkout Based on Product and Shipping Country

In a recent Business Bloomer Club Slack thread, a member asked how to restrict a specific product in WooCommerce based on the customer’s selected shipping country. This type of restriction is essential when logistics or compliance prevents certain products from shipping to specific regions.

To achieve this, you can deny checkout if a restricted product is in the cart and the selected shipping country is not allowed. Below, we’ll go through the code and approach needed to implement this functionality in WooCommerce.

Denying Checkout Based on Product and Shipping Country

Using a combination of filters and WooCommerce functions, you can restrict specific products from being purchased in certain shipping destinations. This approach involves:

  1. Identifying the restricted product by its ID
  2. Checking the customer’s selected shipping country
  3. Denying checkout and displaying an error message if the product-country combination is not allowed

Here’s the custom code to apply this restriction:

add_action( 'woocommerce_check_cart_items', 'bbloomer_restrict_checkout_by_country_product' );

function bbloomer_restrict_checkout_by_country_product() {
$restricted_product_id = 123; // Replace with the restricted product ID
$restricted_countries = array( 'US', 'CA' ); // Add the country codes to restrict
$shipping_country = WC()->cart->get_customer()->get_shipping_country();

if ( bbloomer_is_product_in_cart( $restricted_product_id ) && ! in_array( $shipping_country, $restricted_countries ) ) {
wc_add_notice( 'Sorry, this product cannot be shipped to your selected country.', 'error' );
}
}

function bbloomer_is_product_in_cart( $product_id ) {
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( $cart_item['product_id'] == $product_id ) {
return true;
}
}
return false;
}

Explanation of the Code

  • Line 5: Set the specific product ID in $restricted_product_id that you wish to restrict by country.
  • Line 6: Define the countries where this restriction applies by entering their country codes in the $restricted_countries array.
  • Line 7: Retrieve the currently selected shipping country using WC()->cart->get_customer()->get_shipping_country().
  • Line 9: Check if the restricted product is in the cart using the helper function bbloomer_is_product_in_cart() (defined below) and if the selected country is restricted. If both conditions are met, an error message prevents checkout.

This helper function bbloomer_is_product_in_cart() verifies whether the restricted product is in the cart by checking the product ID in each cart item.

Additional Resources

For more details on restricting checkout, checking product IDs, and getting the shipping country, you can refer to these guides:

By using this code, you ensure that customers cannot complete their purchase when the restricted product is in the cart and the destination country is disallowed. This approach provides a user-friendly experience by notifying customers about the restriction before they finalize their purchase.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Reply

Your email address will not be published. Required fields are marked *