WordPress 主题开发 WooCommerce 设定最低订单量

WordPress 主题开发 WooCommerce 设定最低订单量,显示一个通知提示客户: /**   * …

WordPress 主题开发 WooCommerce 设定最低订单量,显示一个通知提示客户:

  1.     /**
  2.      * Set a minimum order amount for checkout
  3.      */
  4.     add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
  5.     add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
  6. 
    
  7.     function wc_minimum_order_amount() {
  8.         https:// Set this variable to specify a minimum order value
  9.         $minimum = 50;
  10. 
    
  11.         if ( WC()->cart->total < $minimum ) {
  12. 
    
  13.             if( is_cart() ) {
  14. 
    
  15.                 wc_print_notice(
  16.                     sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order ' ,
  17.                         wc_price( WC()->cart->total ),
  18.                         wc_price( $minimum )
  19.                     ), 'error'
  20.                 );
  21. 
    
  22.             } else {
  23. 
    
  24.                 wc_add_notice(
  25.                     sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order' ,
  26.                         wc_price( WC()->cart->total ),
  27.                         wc_price( $minimum )
  28.                     ), 'error'
  29.                 );
  30. 
    
  31.             }
  32.         }
  33.     }

但未达到最低金额时如何禁用结帐按钮?参考以下:

  1.     add_action( 'woocommerce_check_cart_items', 'required_min_cart_subtotal_amount' );
  2.     function required_min_cart_subtotal_amount() {
  3.         https:// Only run in the Cart or Checkout pages
  4.         if( is_cart() || is_checkout() ) {
  5. 
    
  6.             https:// HERE Set minimum cart total amount
  7.             $min_total = 250;
  8. 
    
  9.             https:// Total (before taxes and shipping charges)
  10.             $total = WC()->cart->subtotal;
  11. 
    
  12.             https:// Add an error notice is cart total is less than the minimum required
  13.             if( $total <= $min_total  ) {
  14.                 https:// Display an error message
  15.                 wc_add_notice( '<strong>' . sprintf( __("A minimum total purchase amount of %s is required to checkout."), wc_price($min_total) ) . '<strong>', 'error' );
  16.             }
  17.         }
  18.     }

将 PHP 代码放在主题或子主题 functions.php 文件的底部。

类别:WordPress教程

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册