I am trying to subtract a negative discount fee from the cart total/subtotal in the WooCommerce cart, in order to set the shipping price to 0 when reaching a total/subtotal of 750 excluding coupons and fees.
However, I am encountering some issues where my negative fee is not taken into consideration in the calculation, no matter if I use the subtotal or total for my logic.
I have tried using almost any cart->get
(cart getters), and indifferent from which ones I use or combine, I just can’t seem to make my code subtract the negative fee from the total/subtotal, to get a new total.
My code works when using Coupons, and also for the cart totals/subtotal without negative fee, as you can see in the examples below. In my code, I use the subtotal to subtract fees and discounts, but I have also tried to use the total to no avail.
My Code for setting the shipping cost to 0 when reaching X amount excluding any discount (coupon or fee):
add_filter('woocommerce_package_rates', 'null_shipping_costs_conditionally', 10, 2 );
function null_shipping_costs_conditionally( $rates, $package ){
// Shipping method to change price
$methodprice_key_ids = array('flat_rate:4');
// Initialising variables for new shipping price
$found = false;
$min_amount = 750; // threshold
// Calculate Subtotal incl tax
$subtotal_excl_tax = WC()->cart->get_subtotal();
$subtotal_tax = WC()->cart->get_subtotal_tax();
$subtotal_incl_tax = $subtotal_excl_tax + $subtotal_tax;
// Calculate Discount Totals incl tax
$discount_total_excl_tax = WC()->cart->get_cart_discount_total();
$discount_tax_total = WC()->cart->get_cart_discount_tax_total();
$discount_incl_tax = $discount_total_excl_tax + $discount_tax_total;
// Calculate Fee Totals incl tax
$fee_total_excl_tax = WC()->cart->get_fee_total();
$fee_tax_total = WC()->cart->get_fee_tax();
$fee_incl_tax = $fee_total_excl_tax + $fee_tax_total;
// The new cart total value minus fee and coupons
$cart_total = $subtotal_incl_tax - $fee_incl_tax - $discount_incl_tax;
// Check if cart total is bigger than or equal to minimum threshold
if( $cart_total >= $min_amount) {
$found = true;
}
if( $found ) {
foreach( $rates as $rate_key => $rate ) {
// If shipping rate found then change shipping rate price.
if( in_array($rate_key, $methodprice_key_ids) ) {
// Set the rate cost to zero
$rates[$rate_key]->cost = 0;
// Append rate label title (free)
$rates[$rate_key]->label .= ' ' . __( '- gratis', 'woocommerce' );
// Taxes rate cost (if enabled)
$taxes = array();
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
$taxes[$key] = 0;
$has_taxes = true;
}
else {
$has_taxes = false;
}
}
if( isset($has_taxes) && $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
How I set my discount:
// Discount percentage based on item count
if ($items_count >= 6) {
$percent = 15; // Discount percentage
$discount -= $items_subtotal * $percent / 100; // Calculation
$cart->add_fee( __('Mix & Match rabat', 'woocommerce') . " - {$percent}%", $discount );
}
- The full discount code can be found in this SO thread
Example 1 – Cart without any coupons or fees – Code Works:
- Here is an example where I have products in my cart, without applied discount or fees. As you can see, the shipping cost is set to 0 when reaching above the threshold.
Example 2 – Cart with coupon – Code Works:
- Here is an example where I have products in my cart with an applied coupon discount. As you can see the shipping cost has been set to 0 when reaching above the threshold excluding coupon discounts.
Example 3 – Cart with fee – Code doesn’t work:
- Here is an example where I have products in my cart where the discount is applied. As you can see the shipping cost has been set to 0 even though the total excluding discounts is below the threshold.
.
Example 4 – Cart with fee and coupon – Code doesn’t work:
- Here is an example where I have products in my cart with the discount applied and coupons applied. As you can see the shipping cost has been set to 0 even though the total excluding discounts is below the threshold.
What I am trying to achieve:
- I am looking for a way to set the shipping cost to 0, when the total cart amount is equal to or above the threshold of 750 excluding any discount (fee and coupon). Everything including taxes.
I hope someone can guide me, or help me in reaching my goal. Perhaps there is an alternative solution for setting a discount, that doesn’t involve a negative fee. Or maybe there is something I am look past.
Thank you for reading this.
List of References:
- How to get sum of shipping and payment method fees in WooCommerce
- Add a calculated fee based on total after discounts in WooCommerce
- Woocommerce cart total not showing extra fees added by add_fee()
- woocommerce discount cart withouth using add_fee method
- Subtracting cart subtotal to cart total error issue In WooCommerce
- Custom dynamic shipping rate cost calculation in Woocommerce
- Set custom shipping rates programmatically in WooCommerce 3
- Get order fee and discount amounts for order totals calculations in WooCommerce