【问题标题】:Set a minimum order amount in WooCommerce在 WooCommerce 中设置最低订单金额
【发布时间】:2019-07-29 00:37:21
【问题描述】:

我想在我的 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 挂钩。

标签: php wordpress woocommerce cart checkout


【解决方案1】:
function disable_checkout_button() { 

    // Set this variable to specify a minimum order value
    $minimum = 50;
    $total = WC()->cart->cart_contents_total;
    if( $total < $minimum ){
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        echo '<a style="pointer-events: none !important;" href="#" class="checkout-button button alt wc-forward">Proceed to checkout</a>';
    }  
}

add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button', 1 );

【讨论】:

  • 谢谢,这通常是有效的,但如果你增加购物车数量,按钮仍然不起作用。
  • 我在这段代码中发现了错误。替换:$total = WC()->cart->get_cart_subtotal();在 $total = WC()->cart->cart_contents_total 上; - 将购物车总数作为数字。
  • @RuslanNovikov 如果测试没问题,请修改答案
【解决方案2】:

要设置最小订单量,您可以这样使用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 文件中。经过测试并且可以工作。

如果客户更新购物车更改数量或删除商品,行为也会更新。




相关回答:Woocommerce set minimum order for a specific user role

【讨论】:

  • 效果很好,谢谢!但是,缺少结束 &lt;strong&gt; 标记,否则可能会破坏样式。
【解决方案3】:
// Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Set minimum cart total
        $minimum_cart_total = 500;

        // Total we are going to be using for the Math
        // This is before taxes and shipping charges
        $total = WC()->cart->subtotal;
        
        // Compare values and add an error is Cart's total
        // happens to be less than the minimum required before checking out.
        // Will display a message along the lines of
        // A Minimum of USD 500 is required before checking out. (Cont. below)
        // Current cart total: USD 6 
        if( $total <= $minimum_cart_total  ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>A minimum order value of %s is required before checking out.</strong>'
                .'<br />Your current value is: %s',
                wc_price( $minimum_cart_total ),
                wc_price( $total ) ),
            'error' );
        }
    }
}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 2020-05-11
  • 2013-06-05
  • 2021-07-09
  • 1970-01-01
  • 2014-12-22
相关资源
最近更新 更多