Skip to content
Advertisement

Shipping rates based on service custom delivery type and items total in Woocommerce

I just require a small help on displaying the correct shipping fees in my WordPress and woocommerce site.

I have set my shipping fees as the following:

  1. Flat rate (Delivery): $10
  2. Free Shipping (Free Delivery)
  3. Local Pickup (Pickup): $0

When I make an order and go onto the checkout page, it gives me the options to select from for shipping underneath the order total. I don’t want the user to be able to choose out of the options.

Basically what I require is the following:

  1. If the service type is Pickup, hide delivery options and only show the local pickup option
  2. If the service type is delivery and the order total is $60 or below, then only show the flat rate option.
  3. If the service type is delivery and the order total is above $60, then only show the free shipping option.

Does anybody know how to set this? Also, one thing to note, on the menu page when you add to the cart, it adds a delivery fee automatically when you open the order review container at the bottom, would be good to not set this charge until checkout if possible?

Here is the website containing the food items: https://puffpastrydelights.com/order-online/

I’ve tried this so far but it doesn’t work:

add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available_in_zone');
   
function bbloomer_unset_shipping_when_free_is_available_in_zone( $rates ) {
    $service = $_POST['wfs_service_type']; 
    if ($service == 'pickup') {              
        unset( $rates['flat_rate:1'] );
        unset( $rates['free_shipping:3'] );
    }else if ($service == 'delivery'){
        unset( $rates['local_pickup:2'] );
        if ( isset( $rates['free_shipping:3'] )) {
        unset( $rates['flat_rate:1'] );
        }
    }
    return $rates;
}

Advertisement

Answer

Your service_type is saved in a cookie. you can retrieve by $_COOKIE. Try the below code.

add_filter( 'woocommerce_package_rates', 'unset_shipping_based_on_service_type_cart_total', 99 );
function unset_shipping_based_on_service_type_cart_total( $rates ) {

    $total   = WC()->cart->get_cart_contents_total();
    
    $service = ( isset( $_COOKIE['service_type'] ) && $_COOKIE['service_type'] != '' ) ? $_COOKIE['service_type'] : 'pickup' ;

    if ($service == 'pickup') {              
        unset( $rates['flat_rate:1'] );
        unset( $rates['free_shipping:3'] );
    }else if ( $service == 'delivery' && $total <= 60 ){
        unset( $rates['local_pickup:2'] );
        unset( $rates['free_shipping:3'] );
    }else if ( $service == 'delivery' && $total > 60 ){
        unset( $rates['local_pickup:2'] );
        unset( $rates['flat_rate:1'] );
    }
    return $rates;
}

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement