I want to change allowed shiping method according to these conditions:
If cart subtotal is less than 300 € then allowed shipping method is local delivery.
if cart subtotal is betwen 300 and 1000€ than allowed shipping method is flat rate.
Over 1000€ is shipping free.
Limit for free shipping can be set in admin settings. But rest couldn't be set. I tried to change it in my custom cart-shiping.php WC template. This Way:
if($woocommerce->cart->subtotal > 300 and $woocommerce->cart->subtotal < 1000)
$chosen_method = "flat_rate";
foreach ( $available_methods as $method ) :
global $woocommerce;
$subtotal = $woocommerce->cart->subtotal;
if((($subtotal > 300 and $subtotal < 1000 or $method->method_id == "local_delivery") and ($subtotal < 300 or $method->method_id == "flat_rate")) or ($subtotal > 1000 and $method->method_id == "free_shipping")){
?>
<li>
<input type="radio" name="shipping_method[<?php echo $index; ?>]" data-index="<?php echo $index; ?>" id="shipping_method_<?php echo $index; ?>_<?php echo sanitize_title( $method->id ); ?>" value="<?php echo esc_attr( $method->id ); ?>" <?php checked( $method->id, $chosen_method ); ?> class="shipping_method" />
<label for="shipping_method_<?php echo $index; ?>_<?php echo sanitize_title( $method->id ); ?>"><?php echo wp_kses_post( wc_cart_totals_shipping_method_label( $method ) ); ?></label>
</li>
<?php
}
endforeach; ?>
This is almost what I need but Total price (price of all items in cart inluding shipping price) sometimes doesn't fit because this price is calcualed in WC_Cart class and shpping method has been selected yet. Does anybody know any way to solve this problem or any entire cleaner way (except this payd plugin)?
Here's the snippet you need to use. Using
woocommerce_package_rates
filter hook you can conditionally set or unset the shipping methods