skip to Main Content

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

2

Answers


  1. 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;
    }
    
    Login or Signup to reply.
  2. Updated

    You need first to set the posted delivery type in a WC Session variable. Then some changes in your code will be needed as follows:

    add_action( 'init', 'set_service_type_in_a_wc_session' );
    function set_service_type_in_a_wc_session() {
        // Check that user session is set
        if ( ! ( is_user_logged_in() || is_admin() )
        && isset(WC()->session) && ! WC()->session->has_session() ) {
            WC()->session->set_customer_session_cookie( true );
        }
        // Set a posted value in a WC Session variable
        if ( isset($_POST['wfs_service_type']) ) {
            WC()->session->set('service_type', sanitize_key($_POST['wfs_service_type']) );
        }
    }
    
    add_filter( 'woocommerce_package_rates', 'unset_shipping_based_on_service_type_cart_total', 100, 2 );
    function unset_shipping_based_on_service_type_cart_total( $rates, $package ) {
        // Get discounted items subtotal for the current shipping package
        $total = array_sum( wp_list_pluck( $package['contents'], 'line_total' ) );
        $service = WC()->session->get('service_type');
    
        if ( 'pickup' === $service ) {
            unset($rates['flat_rate:1']);
            unset($rates['free_shipping:3']);
        }
        elseif ( 'delivery' === $service ) {
            unset($rates['local_pickup:2']);
    
            if ( $total < 60 ){
                unset($rates['free_shipping:3']);
            } else {
                unset($rates['flat_rate:1']);
            }
        }
        return $rates;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

    Don’t forget to empty your cart to refresh shipping cached data.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search