Last active
October 28, 2022 21:50
-
-
Save Rahmon/ff0d8773b163a6bf4c8c990d3f2156f1 to your computer and use it in GitHub Desktop.
Define a minimum order amount for WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Check the minimum amount when the order is processed. | |
add_action( 'woocommerce_checkout_process', 'custom_woocommerce_minimum_order_amount' ); | |
// Check the minimum amount on the cart page. | |
add_action( 'woocommerce_before_cart', 'custom_woocommerce_minimum_order_amount' ); | |
// Check the minimum amount on the mini cart. | |
add_action( 'woocommerce_before_mini_cart', 'custom_woocommerce_minimum_order_amount' ); | |
/** | |
* Define a minimum order amount for WooCommerce | |
* | |
* @author Ramon Ahnert <https://profiles.wordpress.org/rahmohn/> | |
*/ | |
function custom_woocommerce_minimum_order_amount() { | |
// Set this variable to specify a minimum order value. | |
$minimum = 50; | |
if ( 1 < did_action( 'woocommerce_before_mini_cart' ) ) { | |
return; | |
} | |
if ( WC()->cart->total < $minimum ) { | |
$message = sprintf( | |
// translators: %1$s cart total; %2$s minimum order amount. | |
__( 'Your current order total is %1$s — you must have an order with a minimum of %2$s to place your order' ), | |
wc_price( WC()->cart->total ), | |
wc_price( $minimum ) | |
); | |
if ( is_cart() ) { | |
wc_print_notice( $message, 'error' ); | |
} else { | |
wc_add_notice( $message, 'error' ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment