0
votes

How can I force free delivery to be applied, based on my specific cart conditional?

My conditional (which works):

  • If 4 items in cart and NO items in cart are from category christmas...

I can't get free shipping to be applied aswell. Would I need to use the woocommerce_cart_calculate_fees hook to do this?

add_filter( 'woocommerce_calculated_total', 'calculated_total', 10, 2 );
function calculated_total( $total, $cart ) {
    $taster_count = 4;
    $item_count   = $cart->get_cart_contents_count();
    $chistm_count = 0;

    foreach ( $cart->get_cart() as $cart_item ) {
        if ( ! has_term( 'christmas', 'product_cat', $cart_item['product_id'] ) ) {
            $chistm_count += $cart_item['quantity'];
        }
    }
    if( $taster_count == $item_count && $chistm_count == $taster_count  )
        $discount = 3.00;
        $cart->add_fee( 'You subscribed! Free shipping applied!', -$discount);
        return 10;
    return $total;
}
1

1 Answers

3
votes

You can use woocommerce_package_rates hook instead of woocommerce_calculated_total

with your condition there unset all the method except Free Shipping.

Hope this will do the trick.