#### how to get progressive percent discount based on cart amount

• Open
• gagandeep
Participant
October 8, 2019 at 9:55 am #3375

I am trying to make a simple discount code for WooCommerce that gives you a percent discount before buying. Lets say that if you add products worth \$100 you get 2% discount and if you add products worth \$250 you get 4%, etc.

``````// Hook before calculate fees

/**
* Add custom fee if more than three article
* @param WC_Cart \$cart
*/
if( \$cart->cart_contents_count < 3 ){
return;
}

// Calculate the amount to reduce
\$discount = \$cart->subtotal * 0.1;
\$cart->add_fee( 'You have more than 3 items in your cart, a 10% discount has been added.', -\$discount);
}``````

But could not manage to make it work with the modifying the hooks with those for the price.

How can I get this?

1. Santosh Kumar
Keymaster
Posts: 62
October 8, 2019 at 9:58 am #3377

Here is the way to do it using conditions based on cart subtotal excl tax amount to add this progressive percentage as a negative fee, so a discount:

``````add_action( 'woocommerce_cart_calculate_fees','cart_price_progressive_discount' );
function cart_price_progressive_discount() {

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

\$has_discount = false;
\$stotal_ext = WC()->cart->subtotal_ex_tax;

// Discount percent based on cart amount conditions
if( \$stotal_ext >= 100 && \$stotal_ext < 250 ) {
\$percent = -0.02;
\$percent_text = ' 2%';
\$has_discount =true;
} elseif( \$stotal_ext >= 250 ) {
\$percent = -0.04;
\$percent_text = ' 4%';
\$has_discount =true;
}
// Calculation
\$discount = \$stotal_ext * \$percent;

// Displayed text
\$discount_text = __('Discount', 'woocommerce') . \$percent_text;

if( \$has_discount ) {