Here is a function that you can use to Set a Minimum Order Amount in WooCommerce and display a custom error message if the minimum is not met:
function woocommerce_custom_minimum_order_amount() {
// Set this variable to specify the minimum order value
$minimum = 50;
if ( WC()->cart->total < $minimum ) {
if( is_cart() ) {
// Set a custom error message
wc_add_notice( sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order.' , wc_price( WC()->cart->total ), wc_price( $minimum ) ), 'error' );
} else {
// Set a custom error message
wc_add_notice( sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order.' , wc_price( WC()->cart->total ), wc_price( $minimum ) ), 'error' );
}
}
}
add_action( 'woocommerce_checkout_process', 'woocommerce_custom_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'woocommerce_custom_minimum_order_amount' );
This function will check the total value of the cart and, if it is less than the specified minimum, it will display an error message using the wc_add_notice
function. The message will be displayed on the cart and checkout pages.
You can customize the error message by changing the text inside the sprintf
function. The %s
placeholders will be replaced with the current cart total and the minimum order value, respectively.
You can also adjust the minimum order value by changing the value of the $minimum
variable.
I hope this helps! Let me know if you have any questions.
Rate this post
Leave a Reply