我需要过滤特定产品类别的产品,这些产品(金额)不会影响免运费最低金额。
所以我认为我用特殊产品量增加最小量。
实施例: 免费送货从40欧元。 客户从空间类别购买35欧元和6欧元产品。总计= 41欧元
Normaly他获得免费送货。但不是他卡片中的特殊产品。
这是可能的吗?
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;
}
也许你有更好的主意?谢谢;)
答案 0 :(得分:0)
也许有人需要它......
这里是正确的代码:
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;
}