在Woocommerce购物车中设置最低金额,但一个类别除外

时间:2016-10-21 09:21:49

标签: php wordpress function woocommerce checkout

我试图设置最低订购金额为200美元除外,对于一堆产品(大约5个)可以单独添加,无论购物车数量多少。

到目前为止,我发现了这个代码,如果没有达到最小值,这似乎可以阻止结帐:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );

function wc_minimum_order_amount() {

            global $woocommerce;

            $minimum = 200;

            if ( $woocommerce->cart->get_cart_total() < $minimum ) {

       $woocommerce->add_error( sprintf( 'You must have an order with a minimum of %s to place your order.' , $minimum ) );

            }

}

我现在正试图弄清楚如何排除某些product_id甚至某些category_id的

这里有类似的问题,但不完全像我的,我不知道如何修改代码以满足我的需求

Woocomerce set category minimum cart

由于 亲切的问候

现在经过一些更改后,我对代码有一些问题/问题,如下面的答案中所述。我使用的代码如下:

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 = 200;

            // 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( '481', 'product_cat', $product['product_id'] ) ) :

                //Get price of that product
                $regular_price = get_post_meta($product['product_id'], '_regular_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( '481', '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 excl. TAX',
                        $minimum_cart_total,
                        get_option( 'woocommerce_currency'),
                        $total,
                        get_option( 'woocommerce_currency') ),
                    'error' );
                }
            endif;
        endforeach;

    }

}

好的,除了类别之外,我设法让它全部工作。让我解释一下自己。

现在正在检查购物车中的产品是否属于一个类别,如果属于,则检查它是否超过200美元。如果购物车的总金额不超过200美元且购物车中有指定类别的产品,则会显示错误消息。我需要为这个函数添加多个类别,但我真的不知道该怎么做。我尝试了下面的代码,即使它没有给出任何错误,它也无法识别类别。如果我只使用一个类别ID,它可以正常工作:

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 = 200;

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

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

                //Get price of that product
                $regular_price = get_post_meta($product['product_id'], '_regular_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;


            if ( has_term( '481,482', '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 200 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 from stock category before checking out.</strong>'
                        .'<br />Current cart\'s total: %s %s excl. TAX',
                        $minimum_cart_total,
                        get_option( 'woocommerce_currency'),
                        $total,
                        get_option( 'woocommerce_currency') ),
                    'error' );
                }
            endif;


    }

}

由于

1 个答案:

答案 0 :(得分:1)

我不得不做很多改变,但现在它完美地工作了。这是我使用的代码:

add_action( 'woocommerce_check_cart_items', 'cart_set_min_total' );
function set_min_total() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {

        global $woocommerce, $product;
        $i=0;
        // Minimum order checking
        $minimumCheck = false;
        // Set minimum cart total
        $minimum_cart_total = 200;
        //loop through all cart products
        foreach ( $woocommerce->cart->cart_contents as $product ) {
            // Total we are going to be using for the Math
            // This is before taxes and shipping charges
            $total = WC()->cart->total;

            // See if any product is from the STOCK category or not
            if ( has_term( '481', 'product_cat', $product['product_id'] ) || has_term( '482', 'product_cat', $product['product_id'] ) ) {
                $minimumCheck = true;
                //Get price of that product
                $regular_price = get_post_meta($product['product_id'], '_regular_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'] );

            }
            if (has_term( '503', 'product_cat', $product['product_id']) || has_term( '495', 'product_cat', $product['product_id'] ) ) {
                //Get price of that product
                $regular_price = get_post_meta($product['product_id'], '_regular_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'] );
            }

        }


            if ( $minimumCheck && $subtotal_cat <= $minimum_cart_total) {

                // 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 200 USD is required before checking out. (Cont. below)
                // Current cart total: 6 USD 
                wc_add_notice( sprintf( '<strong>A Minimum of %s %s excl. TAX is required category before checking out.</strong>'
                        .'<br />Current cart\'s total: %s %s excl. TAX',
                        $minimum_cart_total,
                        get_option( 'woocommerce_currency'),
                        $subtotal_cat,
                        get_option( 'woocommerce_currency') ),
                    'error' );
            }


    }

}