Remove woocommerce shipping based on query string in url
-
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: https://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)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Remove woocommerce shipping based on query string in url’ is closed to new replies.