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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật