
In a recent Business Bloomer Club Slack thread, a question arose about integrating the “Minimum Order Amount” functionality with WooCommerce Blocks.
The original code provided an error notification on the Cart and Checkout pages if the order subtotal was below a specified threshold, but only worked with legacy Cart and Checkout shortcodes. However, with the rise of WooCommerce Blocks, ensuring compatibility with the block-based checkout flow is becoming increasingly important.
This article discusses how the minimum order amount logic can be adapted for blocks and includes a custom function that uses WooCommerce’s woocommerce_store_api_cart_errors
hook. Keep reading to explore how this solution works for both traditional and block-based checkouts.
Implementing the Minimum Order Amount for WooCommerce Blocks
The original snippet from the Business Bloomer blog ensures that a minimum order amount is enforced. If the cart subtotal is below a set value, an error is triggered.
Here’s how you can adapt the solution for WooCommerce Blocks using the latest WooCommerce API. This code checks the cart subtotal and adds an error if it doesn’t meet the minimum threshold.
To make this functionality work with WooCommerce Blocks, you’ll need to ensure that the validation is properly applied during the block-based checkout process. The WooCommerce Blocks feature may handle cart errors differently, and itβs essential to hook into the appropriate actions to display the error message in the block checkout UI.
// Minimum order check for block-based checkout
function block_cart_count_error( $errors, $cart = null ) {
// Define minimum order amount
$minimum_amount = 5;
// Ensure cart is valid
if ( $cart && is_object( $cart ) && method_exists( $cart, 'get_subtotal' ) ) {
// Check if cart subtotal is below minimum
if ( $cart->get_subtotal() <= $minimum_amount ) {
$errors->add( 'cart_error_code', sprintf( esc_html( 'The cart subtotal must be more than %s!', 'your-text-domain' ), wc_price( $minimum_amount ) ) );
}
}
}
add_action( 'woocommerce_store_api_cart_errors', 'block_cart_count_error', 10, 2 );
This uses WooCommerce’s wc_price()
function to display the minimum amount in the store’s currency format, and also ensures the error message is translation-ready.
Conclusion
By using the woocommerce_store_api_cart_errors
hook, this solution enforces a minimum order amount across both traditional and block-based WooCommerce checkouts. The function checks the cart subtotal, and if the amount is below the threshold, it triggers an error message. This approach is easily customizable, and adding WooCommerce Blocks compatibility ensures that your store provides a seamless user experience, regardless of the checkout method used.