Support » Plugin: WooCommerce » Remove woocommerce shipping based on query string in url

  • Resolved jimishsoni1990

    (@jimishsoni1990)


    I’ve two shipping methods on site, flat_rate and local_pickup. On checkout page i ask user for choice like, Do you want a delivery or pickup? when user select the option,i refresh the page and pass the selection as query string like this: http://www.mysite.com/?shipping=pickup.

    So if user select pickup, I want to remove the flat_rate from shipping option. and same when delivery is selected, i want to remove the local_pickup method.

    Here is a code i’m using:

    
    /* add shipping variable to query variables */
    function add_query_var_for_shipping(){
      add_filter( 'query_vars', 'add_query_vars_filter', 1000 );
    }
    add_action('init', 'add_query_var_for_shipping', 999);
    
    function add_query_vars_filter( $vars ){
      $vars[] = "shipping";
      return $vars;
    }
    
    add_filter( 'woocommerce_package_rates', 'free_shipping_disable_flat_rate' );
    function free_shipping_disable_flat_rate( $rates ) {
    
          if(get_query_var('shipping') == 'pickup'){
    
            foreach ( $rates as $rate_key => $rate ) {
                if ( 'flat_rate' === $rate->method_id ){
                    unset($rates[$rate_key]);
                    return $rates;
                }
            }
    
          }
    
          if(get_query_var('shipping') == 'delivey'){
    
            foreach ( $rates as $rate_key => $rate ) {
                if ( 'local_pickup' === $rate->method_id ){
                    unset($rates[$rate_key]);
                    return $rates;
                }
            }
    
          }
    }

    looks like get_query_var(‘shipping’) return null and because of that no shipping method is returned from my code.

    Please help. thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • anyone?

    Sean Cull

    (@seanreloaded)

    Automattic Happiness Engineer

    Hi there,

    I’ve two shipping methods on site, flat_rate and local_pickup. On checkout page i ask user for choice like, Do you want a delivery or pickup? when user select the option,i refresh the page and pass the selection as query string like this: http://www.mysite.com/?shipping=pickup.

    So if user select pickup, I want to remove the flat_rate from shipping option. and same when delivery is selected, i want to remove the local_pickup method.

    This is more than we can help with here. For customizations we recommend one of the services listed on this page:

    https://woocommerce.com/customizations/

    Another option is to team up with a developer on our WooCommerce Developers Slack Channel:

    https://woocommerceslack.herokuapp.com/

    Plugin Support Julia Amosova

    (@jamosova)

    Automattic Happiness Engineer

    Hi @jimishsoni1990,

    looks like you are all set for now so I am going to mark this thread as Resolved. Feel free to connect with us again should you have any additional questions!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.