Free shipping minimum amount without sale products
-
I’m trying to apply a Free Shipping shipping method in my store, but without sale products. So the total amount (price) of the cart should ignore all products that are on sale. The minimum Free Shipping amount should only be compared to the total amount of regular (not sale) products in the cart.
How can I get this to work?
I’ve tried to use the
woocommerce_package_rates
hook to unset (unset($rates[$rate_id]);
) the free_shipping rate if min_amount has a higher value than the sum of all regular products in cart. This does not work.However, when I make the same check directly on the cart page the condition does seem to work.
Below my code (what I’ve tried so far) inside functions.php
add_filter( 'woocommerce_package_rates', 'my_function', 10, 2 );
function my_function( $rates, $package ) {
$sale_items_in_cart = false;
$normal_items_in_cart = false;
$sale_items_in_cart_price_total = 0;
$normal_items_in_cart_price_total = 0;
foreach ( WC()->cart->get_cart() as $key => $values ) {
if ( $values[ 'data' ]->get_sale_price() && !$sale_items_in_cart ) {
$sale_items_in_cart = true;
}
if ( !$values[ 'data' ]->get_sale_price() && !$normal_items_in_cart ) {
$normal_items_in_cart = true;
}
if ( $values[ 'data' ]->get_sale_price() ) {
$sale_items_in_cart_price_total += $values[ 'data' ]->get_sale_price() * $values['quantity'];
}
else {
$normal_items_in_cart_price_total += $values[ 'data' ]->get_price() * $values['quantity'];
}
}
if ( ( $sale_items_in_cart && $normal_items_in_cart ) || !$sale_items_in_cart ) {
$free = false;
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$rate_data = get_option('woocommerce_' . $rate->method_id . '_' . $rate_id . '_settings');
if ( isset($rate_data['min_amount']) && floatval($normal_items_in_cart_price_total) <= floatval($rate_data['min_amount']) ) {
$free = false;
unset($rates[$rate_id]);
}
else {
$free = true;
}
break;
}
}
if ( $free ) {
foreach ( $rates as $rate_key => $rate ) {
if ( 'flat_rate' === $rate->method_id ) {
unset($rates[$rate_key]);
}
}
}
}
return $rates;
}
- You must be logged in to reply to this topic.