Richard
Richard

Reputation: 489

How to subtract negative fee from cart total/subtotal to set shipping rates programmatically when reaching threshold in WooCommerce

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 );
}

Example 1 - Cart without any coupons or fees - Code Works:

enter image description here


Example 2 - Cart with coupon - Code works:

enter image description here


Example 3 - Cart with fee - Code doesn't work:

enter image description here.


Example 4 - Cart with fee and coupon - Code doesn't work:

enter image description here


What I am trying to achieve:

Perhaps there is an alternative solution for setting a discount, that doesn't involve a negative fee. Or maybe there is something I am looking past.


List of References:


Upvotes: 2

Views: 57

Answers (0)

Related Questions