WordPress 主题开发 WooCommerce 设定最低订单量,显示一个通知提示客户:
/**
* Set a minimum order amount for checkout
*/
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
https:// 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'
);
}
}
}
但未达到最低金额时如何禁用结帐按钮?参考以下:
add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
function required_min_cart_subtotal_amount() {
https:// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
https:// HERE Set minimum cart total amount
$min_total = 250;
https:// Total (before taxes and shipping charges)
$total = WC()->cart->subtotal;
https:// Add an error notice is cart total is less than the minimum required
if( $total <= $min_total ) {
https:// Display an error message
wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($min_total) ) . '<strong>', 'error' );
}
}
}
将 PHP 代码放在主题或子主题 functions.php 文件的底部。
专业提供WordPress主题安装、深度汉化、加速优化等各类网站建设服务,详询在线客服!