除产品类别以外的最低购物车数量

时间:2016-12-01 15:29:45

标签: php wordpress woocommerce categories cart

我正在尝试为购物车设置最低金额以允许客户结帐。但我对产品类别做了例外,即使不符合最低要求,我也会允许客户继续使用。

我的代码几乎正常,除了:

  • 通知中显示的购物车计算金额(错误)。
  • 此特殊类别 501 的独家条件(除了此特殊类别,其他类别的产品项目也在购物车中时,无法正常工作)。

这是我的代码:

add_action( 'woocommerce_check_cart_items', 'oxynergy_set_min_total' );
    function oxynergy_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'] )  || has_term( '495', '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( '501', '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' );
            }


    }

}

我做错了什么?
如何使此代码能够专门针对特殊产品类别进行例外处理?

感谢。

1 个答案:

答案 0 :(得分:2)

如果我已理解,您需要禁用已定义的&#34;最小所需总购车数量&#34; 仅限 用于产品类别,但如果有其他产品类别的其他购物车商品,则不会。

因此,您只需在下面的代码中设置此产品类别ID,您就不需要对购物车进行任何计算。

此外,我没有使用 has_term() 功能来避免父/子产品类别的某种错误。相反,我使用 wp_get_post_terms() 和foreach循环...

我已经改变了你的代码:

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

        // Set HERE your minimum cart total
        $minimum_cart_total = 200;
        // Set HERE your special product category ID
        $special_category = 501; 

        //Iterating through each cart items
        foreach ( WC()->cart->get_cart() as $cart_item ) {

            // Get the product categories for the current item
            $item_categories = wp_get_post_terms( $cart_item['product_id'], 'product_cat' );

            // Iterating through each product categories defined for the item
            foreach($item_categories as $item_category){
                if( $item_category->term_id == $special_category ) {
                    $minimumCheck = false;
                    break;
                }
                else {
                    $minimumCheck = true;
                }
            }

            // The cart total without taxes:
            $cart_total_excl_taxes =  WC()->cart->subtotal_ex_tax;
        }

        if ( $minimumCheck == 'true' && $cart_total_excl_taxes <= $minimum_cart_total) {

            // Displays the message notice
            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'),
                $cart_total_excl_taxes,
                get_option( 'woocommerce_currency') ),
                'error' );
        }
    }
}

此代码已经过测试并且可以正常运行......

代码可以在您的活动子主题(或主题)的任何php文件中,也可以在任何插件的php文件中。