Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 特定类别的产品不影响免费送货_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 特定类别的产品不影响免费送货

Php 特定类别的产品不影响免费送货,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我需要过滤特定产品类别的产品,这些产品(金额)不影响免费送货最低金额 所以我想我用特殊产品的数量来增加最小数量 例如: 免费送货40欧元起。 客户以35欧元和6欧元的价格从空间类别购买产品。总计=41欧元 正常情况下,他可以免费送货。但卡上没有特殊产品 这可能吗 add_filter( 'woocommerce_package_rates', 'cardNo_freeShipping', 10, 2 ); function cardNo_freeShipping($rates, $packag

我需要过滤特定产品类别的产品,这些产品(金额)不影响免费送货最低金额

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

例如: 免费送货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;
}

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

也许有人又需要它了

下面是正确的代码:

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;
}

也许有人又需要它

下面是正确的代码:

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;
}

你想做什么?解释一下。购物车中特殊类别的产品不会增加免费送货的购物车总金额。您的代码似乎正确。什么问题?我不确定过滤器是否正确。。。它应该发生在购物车和收银台上。它运行大约66%。。。有三种变体。。。1.只有特殊类别的产品。。。2.仅限不含特殊类别产品的产品。。。3.一起。。。2和3的变化似乎运行。1制造问题你想做什么?解释一下。购物车中特殊类别的产品不会增加免费送货的购物车总金额。您的代码似乎正确。什么问题?我不确定过滤器是否正确。。。它应该发生在购物车和收银台上。它运行大约66%。。。有三种变体。。。1.只有特殊类别的产品。。。2.仅限不含特殊类别产品的产品。。。3.一起。。。2和3的变化似乎运行。我制造问题