【问题标题】:Minimum cart amount except for specific products in WooCommerceWooCommerce 中的特定产品除外的最低购物车数量
【发布时间】:2018-07-25 06:15:01
【问题描述】:

我只允许在我的网站上进行最低价值 15 欧元的订单,但我想对一种产品进行例外处理。如果有人知道如何帮助我,我将不胜感激。最小订单价值的编码如下。任何人都知道我可以如何调整它以通过产品 ID 排除一个产品?

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

    if( is_cart() || is_checkout() ) {
        global $woocommerce;

        // Setting the minimum cart total
        $minimum_cart_total = 15;

        $total = WC()->cart->subtotal;


        if( $total <= $minimum_cart_total  ) {

            wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required before checking out.</strong>'
                .'<br />Current cart\'s total: %s %s',
                $minimum_cart_total,
                get_option( 'woocommerce_currency'),
                $total,
                get_option( 'woocommerce_currency') ),
            'error' );
        }
    }
}

【问题讨论】:

    标签: php wordpress woocommerce cart hook-woocommerce


    【解决方案1】:

    更新 - 以下代码将避免在定义的最小购物车金额下结帐,但定义的产品 ID 除外:

    add_action( 'woocommerce_check_cart_items', 'min_cart_amount' );
    function min_cart_amount() {
        ## ----- Your Settings below ----- ##
    
        $min_amount = 15; // Minimum cart amount
        $except_ids = array(37, 53); // Except for this product(s) ID(s)
    
        // Loop though cart items searching for the defined product
        foreach( WC()->cart->get_cart() as $cart_item ){
            if( in_array( $cart_item['variation_id'], $except_ids ) 
            ||  in_array( $cart_item['product_id'], $except_ids )
                return; // Exit if the defined product is in cart
        }
    
        if( WC()->cart->subtotal < $min_amount ) {
            wc_add_notice( sprintf(
                __( "<strong>A Minimum of %s is required before checking out.</strong><br>The current cart's total is %s" ),
                wc_price( $min_amount ),
                wc_price( WC()->cart->subtotal )
            ), 'error' );
        }
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试和工作。

    【讨论】:

    • 这很好,但是某处有错误(在 sn-ps 中加载并给出 php 警告)...
    • @Dav334 是的,只是在wc_price( WC()-&gt;cart-&gt;subtotal ) 中缺少一个关闭括号
    • 合法!今天对我来说工作得很好! XD 只在我们已经运行的函数 sn-p 中插入了它......? // Except for this product(s) ID(s) $except_ids = array(666); // Loop though cart items searching for the defined product foreach( WC()-&gt;cart-&gt;get_cart() as $cart_item ){ if( in_array( $cart_item['variation_id'], $except_ids ) || in_array( $cart_item['product_id'], $except_ids ) // Exit if the defined product is in cart return; }
    猜你喜欢
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2021-11-27
    • 2019-09-18
    • 2017-12-10
    • 2021-10-31
    • 1970-01-01
    • 2018-07-31
    相关资源
    最近更新 更多