设定最低订单量,显示一个通知提示客户。
/** * 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文件的底部。
您好非常感谢您的教程,但是我按照这个方法添加提示错误,能帮我下吗?
谢谢
已更新代码,重新设置
添加产品到购物车,可看到演示效果:https://www.zhudc.com/demo/wordpress-pennant