I need to make discounts to certain user roles of some products, when a certain number of this product is purchased.
The discount should only be applied to the number of products that we have chosen in:
Between 1 and 9 products: no discount.
Product 10: 5% discount.
Between 11 and 19 products: no discount.
20 products: 5% discount.
Between 21 and 29 products, the discount does not apply,
30 products, the 5% discount is applied, etc…
and so on always in batches of 10 (10, 20, 30, 40, ..multiples of 10)
For example: Buy 12 TVs and a 5% discount is applied
So, a user buys 14 televisions, at a price of €19.00, a 5% discount will be applied to the first 12 units, the remaining 2 do not receive the discount.
I EDIT THE QUESTION WITH NEW EXAMPLES
They’ve refined two examples for me, but they don’t work.
The first of the examples applies the discount to all products after reaching 10 products, but when we go beyond 10, for example 11, 12 or 13, the discount to number 10 disappears.
Then, when reaching 20 products, the discount starts again, but it doesn’t work either because it applies the discount to all products from 20
Can you see what’s wrong?
function apply_discount_for_quantity($cart) {
if (is_admin()) return;
global $woocommerce;
$user = wp_get_current_user();
$user_roles = $user->roles;
$discount_products = array(383); // products eligible for discount
$discount_role = 'wholesale_customer'; // user role eligible for discount
$subtotal = $woocommerce->cart->cart_contents_total;
$count = $woocommerce->cart->cart_contents_count;
$discount_percent = 0;
if (in_array($discount_role, $user_roles) && $count > 0) {
$discount_eligible_products = array();
foreach ($cart->cart_contents as $product) {
if (in_array($product['product_id'], $discount_products)) {
$discount_eligible_products[] = $product;
}
}
$discount_eligible_product_count = count($discount_eligible_products);
if ($discount_eligible_product_count > 0) {
$discount_eligible_product_quantity = 0;
foreach ($discount_eligible_products as $product) {
$discount_eligible_product_quantity += $product['quantity'];
}
if ($discount_eligible_product_quantity >= 10) {
if ($discount_eligible_product_quantity % 10 == 0) {
$discount_percent = 5;
} else if ($discount_eligible_product_quantity >= 20 && $discount_eligible_product_quantity <= 29) {
$discount_percent = 5;
}
} else if ($discount_eligible_product_quantity == 20) {
$discount_percent = 5;
}
}
}
if ($discount_percent > 0) {
$discount_amount = $subtotal * ($discount_percent / 100);
$cart->add_fee('Descuento por cantidad', -$discount_amount);
}
}
add_action('woocommerce_cart_calculate_fees', 'apply_discount_for_quantity');
I have some other option that always displays a text for the discount, but I can’t get the echo discount to save. I want to say that, when you go from 10 products, for example, the discount disappears until I add more products until I reach 20 products, that the discount corresponding to that amount is added, but in products from 10 to 20, the discount disappears 5% made to the 10 products
The discount already obtained must be maintained if we increase the products until we reach the next number that is a multiple of 10, which we would add another 5%. I cannot find the correct form. This next function manages to keep the text in the checkout, but when we have quantities of products that are between 10 and 20, are between 20 and 30, are between 30 and 40, etc., the text is set to zero, without saving the discount already obtained
It’s the closest we’ve come to achieving the goal.
See what I’m doing wrong:
function apply_discount_for_quantity($cart)
{
if (is_admin()) return;
global $woocommerce;
$user = wp_get_current_user();
$user_roles = $user->roles;
$discount_products = array(383, 411, 412); // productos elegibles para descuento
$discount_role = 'wholesale_customer'; // rol de usuario elegible para descuento
$subtotal = $woocommerce->cart->cart_contents_total;
$count = $woocommerce->cart->cart_contents_count;
$discount_percent = 0;
$eligible_products_count = 0; // variable para contar la cantidad de productos elegibles para descuento
$discount_applied = 0; // variable para guardar el descuento aplicado
// cycle through the products in the cart to count thecantidad de productos elegibles
foreach ($cart->cart_contents as $product) {
if (in_array($product['product_id'], $discount_products)) {
$eligible_products_count += $product['quantity'];
}
}
if (in_array($discount_role, $user_roles) && $eligible_products_count > 0) {
// Change the conditions to apply the discount only in multiples of 10
if ($eligible_products_count % 10 == 0) {
$discount_percent = 5;
}
}
// Save the applied discount in a variable
$discount_applied = $subtotal * ($discount_percent / 100);
// Add the discount to the cart to always show it, even if it does not apply to quantities that are not multiples of 10
$cart->add_fee('Descuento por cantidad', -$discount_applied);
//Apply the discount if necessaryo
if ($discount_percent > 0) {
$cart->add_fee('Descuento por cantidad', -$discount_applied);
}
}
add_action('woocommerce_cart_calculate_fees', 'apply_discount_for_quantity');
```
2
Answers
This is the way I would go about this problem:
$required_quantity
I would also determine if they were a subscriber before passing the cart to the apply_discount function.
A simple version of this would be:
As for the €88 discount that is roughly 10x what it should be. My guess is
$discount = $cart_item['quantity'] * 10;
As I can see in your last exemple
First you can break the logic earlier for
As you will not apply discount for them
Then you do :
If you have 31 eligible products you will got 1 == 0 // false and your discount percent will not change and stay to 0
In your code, only some product can be eligible to trigger the discount, it’s look like you will apply the discount to the not eligible product. You should put some of then in your test cart.
So look like to apply more than one discount like Meechew do.
Here my try :
hope it will help you to found what’s you need
Here sample test (test it: https://3v4l.org/ot8A5h):
Result: