I need to filter products from a particular product category and these products (amount) does not affect the free shipping min amount.
So i thought i increase the min amount with the special product amount.
Example:
Free Shipping is from 40 Euro.
The Customer buys products for 35 Euro and a 6 Euro product from the spacial category. Total = 41 Euro
Normaly he gets free shipping. But not with the special product in his card.
Is this posible?
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;
}
Maybe you have a better idea? Thanks 😉
Maybe someone need it again…
Here the right code: