Woocommerce 设置类别最小购物车 [英] Woocommerce set category minimum cart

查看:25
本文介绍了Woocommerce 设置类别最小购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要特定 woocommerce 类别的块价格,现在仅适用于我的代码的所有类别 woocommerce.我的类别名称是: stock .我只想用于此类别.

I would like block price for specific woocommerce category, now work only all category woocommerce my code. My category name is: stock . I want only use for this category.

我希望用户如果购买股票类别必须至少购买 1200.

I want that a user if buy stock category have to buy minimum 1200.

    // 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 = 1200;

        // 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 10 USD is required before checking out. (Cont. below)
        // Current cart total: 6 USD 
        if( $total <= $minimum_cart_total  ) {
            // Display our error message
            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' );
        }
    }
}

推荐答案

试试下面的代码:

    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, $product;
            $i=0;
            //loop through all cart products
            foreach ( $woocommerce->cart->cart_contents as $product ) :


                // Set minimum cart total
                $minimum_cart_total = 1200;

                // Total we are going to be using for the Math
                // This is before taxes and shipping charges
                $total = WC()->cart->subtotal;

                // See if any product is from the STOCK category or not
                if ( has_term( '112', 'product_cat', $product['product_id'] ) ) :

                    //Get price of that product
                    $regular_price = get_post_meta($product['product_id'], '_sale_price', true); //change to _sale_price if it is in sale
                    //echo $regular_price."<br>";
                    $total = $regular_price * $product['quantity']; 
                    //echo $total."<br>";
                    $subtotal_cat += $total; //get total of 
                    //echo $subtotal_cat;
                    //$category_price += ( $product['line_subtotal'] + $product['line_subtotal_tax'] );

                endif;

            endforeach;
            foreach ( $woocommerce->cart->cart_contents as $product ) :

                if ( has_term( '112', 'product_cat', $product['product_id'] ) ) :

                    // 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 10 USD is required before checking out. (Cont. below)
                    // Current cart total: 6 USD 
                    if( $subtotal_cat <= $minimum_cart_total  ) {
                        // Display our error message
                        wc_add_notice( sprintf( '<strong>A Minimum of %s %s is required from stock category before checking out.</strong>'
                            .'<br />Current cart\'s total: %s %s',
                            $minimum_cart_total,
                            get_option( 'woocommerce_currency'),
                            $subtotal_cat,
                            get_option( 'woocommerce_currency') ),
                        'error' );
                    }
                endif;
            endforeach;

        }

    }

让我知道输出..

这篇关于Woocommerce 设置类别最小购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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