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技巧

WordPress教程:WooCommerce有促销商品

2019-3-17 18:02:32

WordPress技巧

WordPress教程:WooCommerce商城页面按价格,日期或标题排序

2019-3-17 18:04:56

3 条回复 A文章作者 M管理员
  1. 您好非常感谢您的教程,但是我按照这个方法添加提示错误,能帮我下吗?
    谢谢

    • 已更新代码,重新设置

    • 添加产品到购物车,可看到演示效果:https://www.zhudc.com/demo/wordpress-pennant

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索