在WooCommerce中设置最低订购量 [英] Set a minimum order amount in WooCommerce

查看:138
本文介绍了在WooCommerce中设置最低订购量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的WooCommerce商店中获得最低订购量。以下代码完美地显示了一个提示,如果未达到金额但仍可以结帐。当未达到最低金额时,如何禁用结帐按钮?
add_action(‘woocommerce_before_cart’,‘wc_minimum_order_amount’);

函数wc_minimum_order_amount(){
//设置此变量以指定最小订单价值
$ minimum = 50;

if(WC()->购物车->总< $ minimum){

if(is_cart()){

wc_print_notice(
sprintf('您当前的订单总数为%s-您必须要有至少%s的订单才能下订单',
wc_price(WC()-> cart->总计),
wc_price($ minimum)
),'错误'
);

}否则{

wc_add_notice(
sprintf('您当前的订单总数为%s-您必须拥有至少%s的订单才能下达订单order',
wc_price(WC()-> cart-> total),
wc_price($ minimum)
),'error'
);

}
}
}


解决方案

要设置最低订购量,您可以使用 woocommerce_check_cart_items 操作挂钩:

  add_action('woocommerce_check_cart_items','required_min_cart_subtotal_amount'); 
function required_min_cart_subtotal_amount(){

//这里设置最小购物车总金额
$ minimum_amount = 250;

//总计(不含税和运费)
$ cart_subtotal = WC()->购物车->小计;

//添加错误通知,表明购物车总额少于所需的最低限额
if($ cart_subtotal< $ minimum_amount){
//显示错误消息
wc_add_notice('< strong>'。sprintf(__(结帐需要%s的最低总购买金额。),wc_price($ minimum_amount))。 );
}
}

代码进入活动子主题(或活动子主题)的function.php文件主题)。经过测试并可以正常工作。


如果客户更新购物车以更改数量或移除商品,则行为也将更新。








< a href = https://i.stack.imgur.com/VOUof.png rel = nofollow noreferrer>




相关答案: Woocommerce为特定用户角色设置了最低订单


I want to have a minimum order amount in my WooCommerce store. The following code is perfectly showing a notice if the amount isn't reached but the checkout is still possible. How to disable checkout-button when the minimum amount isn't reached?

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' 
            );

        }
    }
}

解决方案

To set a minimum order amount you can use woocommerce_check_cart_items action hook this way:

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' );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

If customer update the cart changing quantities or removing items, The behavior will be updated too.




Related answer: Woocommerce set minimum order for a specific user role

这篇关于在WooCommerce中设置最低订购量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆