【问题标题】:Products from a particular category does not affect the free shipping特定类别的产品不影响免费送货
【发布时间】:2015-09-21 10:35:12
【问题描述】:

我需要从特定产品类别中筛选产品,这些产品(数量)不会影响免费送货最低金额。

所以我想我用特殊产品的数量来增加最小数量。

示例: 免费送货从 40 欧元起。 客户以 35 欧元和 6 欧元的价格购买空间类别的产品。总计 = 41 欧元

通常他会免费送货。但不是他卡里的特殊产品。

这可能吗?

add_filter( 'woocommerce_package_rates', 'cardNo_freeShipping', 10, 2 ); 
function cardNo_freeShipping($rates, $package){

global $woocommerce;
$items = $woocommerce->cart->get_cart(); // Get all cart items

foreach($items as $item => $values) {       
    $terms = get_the_terms( $values['product_id'], 'product_cat' );
    foreach ($terms as $term) {     
        if($term->slug == "The-Special-Category"){  //If is the special category            
            if($values['variation_id']) $price = get_post_meta($values['variation_id'] , '_price', true);   //If variation exist get price
            else $price = get_post_meta($values['product_id'] , '_price', true);                            //else get singel price
            $PriceArray[] = $price * $values['quantity'];           
            }
    }
} 

//get vars
$free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
$CartSubtotal = $woocommerce->cart->subtotal;

if($PriceArray) {
    $Sum = array_sum($PriceArray);
    //The reason it's not exactly 0 lies in the definition of floating point numbers.
    $MinAmount = intval($CartSubtotal*100) - intval($Sum*100);
    $MinAmount = $MinAmount/100;        
    //The reason it's not exactly 0 lies in the definition of floating point numbers.
}   

if(!$MinAmount){ //No Special products in cart
    // Only modify rates if free_shipping is present
    if ( isset( $rates['free_shipping'] ) ) {
        // To unset a single rate/method, do the following. This example unsets flat_rate shipping
        unset( $rates['flat_rate'] );           
        // To unset all methods except for free_shipping, do the following
        $free_shipping          = $rates['free_shipping'];
        $rates                  = array();
        $rates['free_shipping'] = $free_shipping;
        }
    }

if($MinAmount==0){ //Only special products in cart
    // Only modify rates if flat_rate is present
    if ( isset( $rates['flat_rate'] ) ) {
        unset( $rates['free_shipping'] );               
        // To unset all methods except for flat rate, do the following
        $flat_rate          = $rates['flat_rate'];
        $rates              = array();
        $rates['flat_rate'] = $flat_rate;       
        }
    }else{ //Normal and special products in cart
        if($free_shipping_settings['min_amount'] >= $MinAmount){    
            if ( isset( $rates['flat_rate'] ) ) {           
                unset( $rates['free_shipping'] );               
                // To unset all methods except for flat rate, do the following
                $flat_rate          = $rates['flat_rate'];
                $rates              = array();
                $rates['flat_rate'] = $flat_rate;
                }
            }else{
                if ( isset( $rates['free_shipping'] ) ) {
                    // To unset a single rate/method, do the following. This example unsets flat_rate shipping
                    unset( $rates['flat_rate'] );

                    // To unset all methods except for free_shipping, do the following
                    $free_shipping          = $rates['free_shipping'];
                    $rates                  = array();
                    $rates['free_shipping'] = $free_shipping;
                    }
                else if( isset( $rates['flat_rate'] ) ) {           
                    unset( $rates['free_shipping'] );               
                    // To unset all methods except for flat rate, do the following
                    $flat_rate          = $rates['flat_rate'];
                    $rates              = array();
                    $rates['flat_rate'] = $flat_rate;
                    }
                }
        }           
return $rates;
}

也许你有更好的主意?谢谢;)

【问题讨论】:

  • 你想做什么?稍微解释一下。
  • 购物车中特殊类别的产品不增加购物车总金额,免运费。
  • 您的代码似乎正确。有什么问题?
  • 我不确定过滤器是否正确......它应该发生在购物车和结帐中。
  • 它运行大约 66%... 有三种变化... 1. 只有特殊类别产品... 2. 只有没有特殊类别产品的产品... 3. 两者一起... . 2 & 3 的变化似乎在运行。 1 制造问题

标签: php wordpress woocommerce


【解决方案1】:

也许有人再次需要它......

这里是正确的代码:

add_filter( 'woocommerce_package_rates', 'cardNo_freeShipping', 10, 2 ); 
function cardNo_freeShipping($rates, $package){

global $woocommerce;
$items = $woocommerce->cart->get_cart(); // Get all cart items

foreach($items as $item => $values) {       
    $terms = get_the_terms( $values['product_id'], 'product_cat' );
    foreach ($terms as $term) {     
        if($term->slug == "xartotainies"){  //If is the special category            
            if($values['variation_id']) $price = get_post_meta($values['variation_id'] , '_price', true);   //If variation exist get price
            else $price = get_post_meta($values['product_id'] , '_price', true);                            //else get singel price
            $PriceArray[] = $price * $values['quantity'];           
            }
    }
} 

//get vars
$free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' );
$CartSubtotal = $woocommerce->cart->subtotal;

if($PriceArray) {
    $Sum = array_sum($PriceArray);
    //The reason it's not exactly 0 lies in the definition of floating point numbers.
    $MinAmount = intval($CartSubtotal*100) - intval($Sum*100);
    $MinAmount = $MinAmount/100;        
    //The reason it's not exactly 0 lies in the definition of floating point numbers.
    if($MinAmount==0)$MinAmount=0.1; //fix zero problem
}   

if(!$MinAmount){ //No Special products in cart
    // Only modify rates if free_shipping is present
    if ( isset( $rates['free_shipping'] ) ) {
        // To unset a single rate/method, do the following. This example unsets flat_rate shipping
        unset( $rates['flat_rate'] );           
        // To unset all methods except for free_shipping, do the following
        $free_shipping          = $rates['free_shipping'];
        $rates                  = array();
        $rates['free_shipping'] = $free_shipping;
        }           
    }else{          
        if($MinAmount >= $free_shipping_settings['min_amount']){
            if ( isset( $rates['free_shipping'] ) ) {
                // To unset a single rate/method, do the following. This example unsets flat_rate shipping
                unset( $rates['flat_rate'] );           
                // To unset all methods except for free_shipping, do the following
                $free_shipping          = $rates['free_shipping'];
                $rates                  = array();
                $rates['free_shipping'] = $free_shipping;
                }
            }else{
                if ( isset( $rates['flat_rate'] ) ) {
                    unset( $rates['free_shipping'] );               
                    // To unset all methods except for flat rate, do the following
                    $flat_rate          = $rates['flat_rate'];
                    $rates              = array();
                    $rates['flat_rate'] = $flat_rate;       
                    }
                }   

        }
return $rates;
}

【讨论】:

    猜你喜欢
    • 2015-12-06
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多