I need to add discount according to number of product in cart and this discount will apply on total of cart. Is there any other option without use of coupons?
Asked
Active
Viewed 2.4k times
3
-
Refer this : 1 - https://wordpress.org/plugins/woocommerce-direct-bulk-category-discount/ 2 - https://wordpress.org/plugins/woocommerce-bulk-discount/screenshots/ – Helping Hands Nov 27 '14 at 07:51
-
these plugins are working with product wise i need to add discount according to total number of products in cart. – user3714488 Nov 27 '14 at 08:46
-
Then you will need to do custom code for that I think.. – Helping Hands Nov 27 '14 at 08:49
-
Is there not any hook of woocommerce to add custom discounts? – user3714488 Nov 27 '14 at 08:52
-
Refer : http://stackoverflow.com/questions/22928367/how-to-create-custom-discount-for-the-cart-in-woocommerce – Helping Hands Nov 27 '14 at 08:55
-
This only shows the discount amount but not cut the discounted price from total. – user3714488 Nov 27 '14 at 09:16
-
Then you will have to do custom code. you will not get ready made code I think.. – Helping Hands Nov 27 '14 at 09:17
2 Answers
31
I prefer this way, cleaner I think
// Hook before calculate fees
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
/**
* Add custom fee if more than three article
* @param WC_Cart $cart
*/
function add_custom_fees( 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);
}
-
Thanks, this is a much better approach instead of using coupon codes to clutter the backend when using dynamic discounts applied to carts. – Solomon Closson Jun 12 '17 at 15:23
-
Does this also saves in the backend order when guest user is shopping ? – Ravi Soni Jul 22 '18 at 15:21
-
If you use VAT on the site then the VAT amount is added to the discount price too. like if you have 20% VAT and the Discount price is 10 then your final discount is 12 – Samir Karmacharya Aug 20 '22 at 14:17
4
This code should work:
add_action('woocommerce_before_cart_table', 'discount_when_produts_in_cart');
function discount_when_produts_in_cart( ) {
global $woocommerce;
if( $woocommerce->cart->cart_contents_count > 3 ) {
$coupon_code = 'maryscode';
if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
$woocommerce->show_messages();
}
echo '<div class="woocommerce_message"><strong>You have more than 3 items in your cart, a 10% discount has been added.</strong></div>';
}
}
The above will apply the coupon "maryscode" to the cart if there are 4 or more products in the customers cart.
EDIT: Add the following to your css
.coupon {
display: none !important;
}

Mukesh Panchal
- 1,956
- 2
- 18
- 31

Howli
- 12,291
- 19
- 47
- 72
-
Yes i have tried this is working but showing coupon field and apply button and messages on cart page. – user3714488 Nov 27 '14 at 09:15
-
@user3714488 Go to the Woocommerce settings -> Checkout, there is an option for "Coupons" untick that box and it won't show the coupon field/apply button on the cart page. – Howli Nov 27 '14 at 09:17
-
-
@user3714488, sorry about that. Retick that option and add the css I edited into my post. – Howli Nov 27 '14 at 09:31