简体   繁体   中英

Woocommerce force free delivery based on cart count and cart categories

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM