我想在我的 WooCommerce 商店中设置最低订单量。如果未达到金额但仍然可以结帐,则以下代码完美显示通知。未达到最低金额时如何禁用结帐按钮?
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 50;
if ( WC()->cart->total < $minimum ) {
if( is_cart() ) {
wc_print_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 {
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'
);
}
}
}
最佳答案
要设置最低订购量,您可以使用 woocommerce_check_cart_items
这样的 Action 钩子(Hook):
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
// HERE Set minimum cart total amount
$minimum_amount = 250;
// Total (before taxes and shipping charges)
$cart_subtotal = WC()->cart->subtotal;
// Add an error notice is cart total is less than the minimum required
if( $cart_subtotal < $minimum_amount ) {
// Display an error message
wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($minimum_amount) ) . '<strong>', 'error' );
}
}
代码位于您的事件子主题(或事件主题)的 function.php 文件中。测试和工作。If customer update the cart changing quantities or removing items, The behavior will be updated too.
相关答案:Woocommerce set minimum order for a specific user role
关于php - 在 WooCommerce 中设置最低订单金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55038477/