Skip to content

Instantly share code, notes, and snippets.

@varun-pluginhive
Last active September 28, 2018 08:00
Show Gist options
  • Save varun-pluginhive/2cea63276a98c1869328c1743993e4ee to your computer and use it in GitHub Desktop.
Save varun-pluginhive/2cea63276a98c1869328c1743993e4ee to your computer and use it in GitHub Desktop.
Snippet to hide Flat rate if cart amount is more than specified amount else hide Free Shipping. If the product comes with other products add the flat rate to ups and hide other rates. PluginHive Plugins - https://www.pluginhive.com/plugins/
/**
* Snippet to hide Flat rate if cart amount is more than specified amount else hide Free Shipping. If the product comes with other products add the flat rate to ups and hide other rates.
* Created at : 20 Sep 2018
* Updated at : 28 Sep 2018
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/varun-pluginhive/2cea63276a98c1869328c1743993e4ee
*/
if( ! empty('ph_skip_shipping_method_based_on_shipping_class') ) {
function ph_skip_shipping_method_based_on_shipping_class( $shipping_costs, $package ) {
$thresold_order_amount = 1000; // Order Amount above which flat rate needs to be hidden below that Free Shipping needs to be hidden
$flat_or_free_shipping_class = array('shoe'); // Shipping Class of Flat Rate or Free Shipping
$only_flat_or_free_shipping =false;
$ups_only = false;
foreach( $package['contents'] as $line_item) {
$shipping_class = $line_item['data']->get_shipping_class();
if( empty($shipping_class) ) {
$ups_only = true;
continue;
}
elseif( in_array($shipping_class, $flat_or_free_shipping_class) ) {
$only_flat_or_free_shipping = true;
}
else{
$ups_only = true;
}
}
$cost_to_add = 0;
if( $ups_only && $only_flat_or_free_shipping ) {
foreach( $shipping_costs as $key=> $shipping_cost) {
$shipping_method_id = $shipping_cost->get_method_id();
if( $shipping_method_id == 'flat_rate' ) {
$cost_to_add = (double) $shipping_cost->get_cost();
unset( $shipping_costs[$key]);
}
elseif( $shipping_method_id == 'free_shipping' ) {
unset( $shipping_costs[$key]);
}
}
if( ! empty($cost_to_add) ) {
foreach( $shipping_costs as &$shipping_cost) {
$shipping_cost->set_cost( $cost_to_add + (double) $shipping_cost->get_cost() );
}
}
}
elseif( $ups_only ) {
foreach( $shipping_costs as $key=> $shipping_cost) {
$shipping_method_id = $shipping_cost->get_method_id();
if( $shipping_method_id == 'flat_rate' || $shipping_method_id == 'free_shipping' ) {
unset( $shipping_costs[$key]);
}
}
}
elseif( $only_flat_or_free_shipping ) {
$cart_cost = (double) $package['cart_subtotal'];
foreach( $shipping_costs as $key=>$shipping_cost) {
$shipping_method_id = $shipping_cost->get_method_id();
if( $cart_cost > $thresold_order_amount && $shipping_method_id == 'flat_rate') {
unset($shipping_costs[$key]);
}
elseif( $cart_cost <= $thresold_order_amount && $shipping_method_id == 'free_shipping'){
unset($shipping_costs[$key]);
}
}
}
return $shipping_costs;
}
add_filter( 'woocommerce_package_rates', 'ph_skip_shipping_method_based_on_shipping_class', 10, 2 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment