Applied coupons disable Free shipping conditionally in Woocommerce
The below code will enable "Free shipping" for applied coupons only if cart subtotal reaches a minimal amount (discounted including taxes):
add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
function coupons_removes_free_shipping( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
$min_subtotal = 250; // Minimal subtotal allowing free shipping
// Get needed cart subtotals
$subtotal_excl_tax = WC()->cart->get_subtotal();
$subtotal_incl_tax = $subtotal_excl_tax + WC()->cart->get_subtotal_tax();
$discount_excl_tax = WC()->cart->get_discount_total();
$discount_incl_tax = $discount_total + WC()->cart->get_discount_tax();
// Calculating the discounted subtotal including taxes
$discounted_subtotal_incl_taxes = $subtotal_incl_tax - $discount_incl_tax;
$applied_coupons = WC()->cart->get_applied_coupons();
if( sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
foreach ( $rates as $rate_key => $rate ){
// Targeting "Free shipping"
if( 'free_shipping' === $rate->method_id ){
unset($rates[$rate_key]);
}
}
}
return $rates;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
Original answer:
The below code will remove "Free shipping" shipping methods when any coupon is applied without any settings need. There is some mistakes in your actual code. Try the following:
add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
function coupons_removes_free_shipping( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
$applied_coupons = WC()->cart->get_applied_coupons();
if( sizeof($applied_coupons) > 0 ){
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ){
// Targeting "Free shipping" only
if( 'free_shipping' === $rate->method_id ){
unset($rates[$rate_key]); // Removing current method
}
}
}
return $rates;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Hide specific shipping methods when coupon is applied
Actually you are there, all you have to do is to compare $rate_key
instead of $rate->mothod_id
You code should look like this:
if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
foreach ( $rates as $rate_key => $rate ){
// Set 2x cost"
if( $rate_key === 'flat_rate:1' ){
// Set 2x of the cost
$rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;
}
// Disable "flat_rate:7"
if( $rate_key === 'flat_rate:7' ){
unset($rates[$rate_key]);
}
// Disable "Free shipping"
if( 'free_shipping' === $rate_key ){
unset($rates[$rate_key]);
}
}
}
Or, a bit simplier:
if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
unset($rates['flat_rate:7']);
unset($rates['free_shipping']);
foreach ( $rates as $rate_key => $rate ){
if( $rate_key === 'flat_rate:1' ) $rates[$rate_key]->cost = $rates[$rate_key]->cost * 2; // Set 2x of the cost
}
}
Hide free shipping when specific coupons are applied in Woocommerce
Try the following instead:
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
function filter_woocommerce_package_rates( $rates, $package ) {
$targeted_coupons = array("544"); // Here set your related coupon codes
$applied_coupons = WC()->cart->get_applied_coupons();
if ( ! empty($applied_coupons) && array_intersect( $targeted_coupons, $applied_coupons ) ) {
foreach ( $rates as $rate_key => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
unset($rates[$rate_key]);
}
}
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Refresh the shipping caches:
- This code is already saved on your functions.php file.
- In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.
Disable free shipping for specific coupon codes in WooCommerce
Use instead woocommerce_package_rates
filter hook. In the code below you will set the related coupon codes that will hide the free shipping method:
add_filter( 'woocommerce_package_rates', 'hide_free_shipping_method_based_on_coupons', 10, 2 );
function hide_free_shipping_method_based_on_coupons( $rates, $package )
{
$coupon_codes = array('summer'); // <== HERE set your coupon codes
$applied_coupons = WC()->cart->get_applied_coupons(); // Applied coupons
if( empty($applied_coupons) )
return $rates;
// For specific applied coupon codes
if( array_intersect($coupon_codes, $applied_coupons) ) {
foreach ( $rates as $rate_key => $rate ) {
// Targetting "Free shipping"
if ( 'free_shipping' === $rate->method_id ) {
unset($rates[$rate_key]); // hide free shipping method
}
}
}
return $rates;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Clearing shipping caches:
- You will need to empty your cart, to clear cached shipping data
- Or In shipping settings, you can disable / save any shipping method, then enable back / save.
Hide Free shipping for specific applied coupon when discounted subtotal is below 75
You are very near… The following will do the job:
add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 33, 38 );
function coupons_removes_free_shipping( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
$min_subtotal = 75; // Minimal subtotal allowing free shipping
$coupon_code = 'summer'; // The required coupon code
// Get cart subtotals and applied coupons
$cart = WC()->cart;
$subtotal_excl_tax = $cart->get_subtotal();
$subtotal_incl_tax = $subtotal_excl_tax + $cart->get_subtotal_tax();
$discount_excl_tax = $cart->get_discount_total();
$discount_incl_tax = $discount_excl_tax + $cart->get_discount_tax();
$applied_coupons = $cart->get_applied_coupons(); // Get applied coupons array
// Calculating the discounted subtotal including taxes
$disc_subtotal_incl_tax = $subtotal_incl_tax - $discount_incl_tax;
if( in_array( strtolower($coupon_code), $applied_coupons ) && $disc_subtotal_incl_tax < $min_subtotal ){
foreach ( $rates as $rate_key => $rate ){
// Targeting "Free shipping"
if( 'free_shipping' === $rate->method_id ){
unset($rates[$rate_key]);
}
}
}
return $rates;
}
Don't forget after saving that code to your theme's functions.php file to refresh your shipping rates in Admin shipping rates settings, disabling and save any shipping method and re-enable and save it back…
Enable free shipping for a min amount based on WooCommerce discounted subtotal
Updated
First in your shipping settings for Free shipping, you will need to set the minimal amount to 0 (zero). Then the following code will handle cart item non discounted subtotal for a "Free shipping" minimal amount (that will solve your issue):
add_filter( 'woocommerce_package_rates', 'conditional_free_shipping', 10, 2 );
function conditional_free_shipping( $rates, $package ){
$shipping_country = WC()->customer->get_shipping_country(); // Get shipping country
$free_shipping = $other_rates = array(); // Initializing
if ($shipping_country === 'DE') {
$min_subtotal = 45;
} elseif ($shipping_country === 'AT') {
$min_subtotal = 75;
}
// Get subtotal incl tax (non discounted) for the current shipping package
$items_subtotal = array_sum( wp_list_pluck( $package['contents'], 'line_subtotal' ) );
$items_subtotal += array_sum( wp_list_pluck( $package['contents'], 'line_subtotal_tax' ) );
// Loop through shipping rates for current shipping package
foreach ( $rates as $rate_key => $rate ){
if( 'free_shipping' === $rate->method_id ){
$free_shipping[$rate_key] = $rate;
} else
$other_rates[$rate_key] = $rate;
}
}
return isset($min_subtotal) && $items_subtotal >= $min_subtotal ? $free_shipping : $other_rates;
}
Code goes in functions.php file of the active child theme (or active theme). It should work.
Don't forget to empty your cart to refresh shipping cached data.
Related Topics
Set Active State on Navigation Dynamically
PHP Datetime() Class, Change First Day of the Week to Monday
Change Single Variable Value in Querystring
Use Openssl_Encrypt to Replace Mcrypt for 3Des-Ecb Encryption
Laravel Checking If a Record Exists
How to Include External CSS and Js File in Laravel 5
Design Patterns: How to Create Database Object/Connection Only When Needed
How to Enable PHP to Work with Postgresql
Run a PHP Script Every Second Using Cli
Sending Mail with PHP & Escaping Hotmails Junk Folder
Dynamically Changing Image Colours
Protection Against Xss Exploits
PHP to Clean-Up Pasted Microsoft Input
MySQL - Insert Date Range into Date Columns If Dates Don't Overlap with Existing Ones
PHP Composer Update "Cannot Allocate Memory" Error (Using Laravel 4)
PHP Sessions in a Load Balancing Cluster - How
Category Hierarchy (Php/Mysql)
Detecting Ajax in PHP and Making Sure Request Was from My Own Website