Skip to content

Instantly share code, notes, and snippets.

@munts
Created September 10, 2020 23:42
Show Gist options
  • Save munts/7a7d3803d4add08fe41f9199d337e0c1 to your computer and use it in GitHub Desktop.
Save munts/7a7d3803d4add08fe41f9199d337e0c1 to your computer and use it in GitHub Desktop.
WooCommerce - Check cart amount and applied coupons to determine shipping options. If excluded coupon is in the applied coupon, remove free shipping and force flat rate shipping....
/* Change shipping based on total cost purchased and or applied coupons are excluded. */
add_filter( 'woocommerce_shipping_packages', 'hide_flat_rate_if_cart_total_is_greater_than_threshold', 5, 1 );
function hide_flat_rate_if_cart_total_is_greater_than_threshold( $packages ) {
$threshold1 = 249;
$applied_coupons = WC()->session->get( 'applied_coupons', array() );
$amount = WC()->cart->cart_contents_total;
$availableRates = $packages[0]['rates'];
$excluded_coupons = array('coupon1', 'coupon2', 'coupon3', 'coupon4', 'coupon5', 'coupon6', 'cooupon7', 'eight', 'nine', 'ten');
$isExcludedCoupon = false;
$map = array_map(function($c) use($excluded_coupons, &$isExcludedCoupon ){
if( in_array($c, $excluded_coupons) ){
$isExcludedCoupon = true;
}
return $c;
}, $applied_coupons);
$flatRateKey = '';
array_map(function($r) use($availableRates, &$flatRateKey){
$id = $r->get_id();
$pos = substr_count($id, 'flat_rate');
if($pos == 1 ){
$flatRateKey = $id;
}
return $r;
}, $availableRates);
if ( $amount > $threshold1 && !$isExcludedCoupon) {
unset($packages[0]['rates'][$flatRateKey]);
}
if ($isExcludedCoupon) {
unset($packages[0]['rates']['woodiscountfree']);
unset($packages[0]['rates']['free_shipping:2']);
}
return $packages;
}
@munts
Copy link
Author

munts commented Sep 10, 2020

Here are the conditions I needed to check in this function:
// If the cart total is greater than 250 dollars and none of the excluded coupons are applied then the user should get free shipping or pick up at the shop. Remove flat Rate
// Here is the list of excluded coupons: $excluded_coupons = array('coupon1', 'coupon2', 'coupon3', 'coupon4', 'coupon5', 'coupon6', 'cooupon7', 'eight', 'nine', 'ten');
// If an excluded coupon is applied, then free shipping should no longer be an option. They should only get presented pickup at the shop or flat rate shipping.
// If the cart is under 250 they get flat rate shipping or free pickup at the shop.
// If a cart is over 250 and they use any other coupon, not in the excluded coupons array, the user should get free shipping or pickup at the shop.
// So the only time Flat Rate should get presented is when the cart is under 250 or the user has applied a coupon in the Excluded coupons array.
// free shipping should be an option to anyone with a cart over 250 and is not using an excluded coupon

@munts
Copy link
Author

munts commented Sep 12, 2020

Found this while doing some research and decided to save it to compare to what I did above: From https://www.tychesoftwares.com/how-to-hide-woocommerce-shipping-methods-for-certain-conditions/
/**

  • Hide shipping rates when order total is more than $250.
  • @param array $rates Array of rates found for the package.
  • @return array
    */
    function ts_hide_shipping_for_order_total( $rates ) {
    $free = array();

$order_total = WC()->cart->get_subtotal();

if( $order_total > 250 ) {
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->get_method_id() ) {
$free[ $rate_id ] = $rate;
}
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_for_order_total', 100 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment