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 on 10 products.
Between 11 & 19 products: 5% discount on first 10 products, no discount for product 11 to 19
20 products: 5% discount on 20 products.
Between 21 & 29 products: 5% discount on first 20 products, no discount for product 21 to 29
30 products : 5% discount on 30 products.
and so on always in batches of 10 (10, 20, 30, 40, ..multiples of 10)
For example: User buys 14 televisions, at a price of €19.00, a 5% discount will be applied to the first 10 units, the remaining 4 do not receive the discount.
I EDIT THE QUESTION
With the help of user @yash's answer, I've managed to get what I'm looking for working somewhat.
Only one type of discount can be applied and to specific products.
As I need to make several types of discounts (buy 10 products and get 5% discount - buy 15 products and get 7% discount - buy 12 products and get 4% discount)
I've tried to clone the function in this answer by changing the name and discount data, but I can't get it to work.
How can I do to achieve this?
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 I go up from 10 products, the discount disappears until I add more products until I reach 20 products, that the discount corresponding to that amount is added, but in between products from 10 to 20, the discount disappears 5% made to the first 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');
<?php
/**
* Applies discounts to specific products in the cart based on quantity and user role.
*
* @param WC_Cart $cart The cart object to apply discounts to.
*/
function apply_discount_for_quantity($cart)
{
// Check if user is an administrator and exit early if true
if (is_admin()) {
return;
}
// Get the current user's role
$current_user = wp_get_current_user();
$user_roles = $current_user->roles;
// Set the user role that is eligible for discounts
$discount_role = 'wholesale_customer';
// Product IDs of that has discount on bunch of 9 products
$discount_products_of_9 = array(383, 411, 412);
$discount_percentage_for_9 = 4; // change here for discount percentage
// Product IDs of that has discount on bunch of 10 products
$discount_products_of_10 = array(384, 421, 422);
$discount_percentage_for_10 = 5; // change here for discount percentage
// Product IDs of that has discount on bunch of 12 products
$discount_products_of_12 = array(385, 432, 432);
$discount_percentage_for_12 = 6; // change here for discount percentage
// Initialize arrays to hold eligible product data for each discount category
$eligible_products_data_of_9 = $eligible_products_data_of_10 = $eligible_products_data_of_12 = array();
$total_discount_amount = 0;
// Loop through cart items to find eligible products
foreach ($cart->cart_contents as $item) {
if (in_array($item["product_id"], $discount_products_of_9 )) {
// Calculate eligible quantity and price for discount
$eligible_products_data_of_9[$item["product_id"]]["price"] = $item[ "data" ]->get_price();
$eligible_products_data_of_9[$item["product_id"]]["qty"] = $item["quantity"] - ($item["quantity"] % 9); // change 9 with different number you want
}
if (in_array($item["product_id"], $discount_products_of_10 )) {
// Calculate eligible quantity and price for discount
$eligible_products_data_of_10[$item["product_id"]]["price"] = $item[ "data" ]->get_price();
$eligible_products_data_of_10[$item["product_id"]]["qty"] = $item["quantity"] - ($item["quantity"] % 10); // change 10 with different number you want
}
if (in_array($item["product_id"], $discount_products_of_12 )) {
// Calculate eligible quantity and price for discount
$eligible_products_data_of_12[$item["product_id"]]["price"] = $item[ "data" ]->get_price();
$eligible_products_data_of_12[$item["product_id"]]["qty"] = $item["quantity"] - ($item["quantity"] % 12); // change 12 with different number you want
}
}
// Apply discount if user is eligible and there are eligible products for each discount category
if ( in_array($discount_role, $user_roles ) && !empty($eligible_products_data_of_9) ) {
foreach ($eligible_products_data_of_9 as $eligible_product) {
$total_discount_amount += ($eligible_product["qty"] * $eligible_product["price"] * $discount_percentage_for_9) / 100;
}
}
if ( in_array($discount_role, $user_roles ) && !empty($eligible_products_data_of_10) ) {
foreach ($eligible_products_data_of_10 as $eligible_product) {
$total_discount_amount += ($eligible_product["qty"] * $eligible_product["price"] * $discount_percentage_for_10) / 100;
}
}
if ( in_array($discount_role, $user_roles ) && !empty($eligible_products_data_of_12) ) {
foreach ($eligible_products_data_of_12 as $eligible_product) {
$total_discount_amount += ($eligible_product["qty"] * $eligible_product["price"] * $discount_percentage_for_12) / 100;
}
}
// Add discount fee to the cart
$cart->add_fee("Descuento por cantidad", -$total_discount_amount);
}
// Hook the function to apply the discount on cart calculation
add_action( "woocommerce_cart_calculate_fees", "apply_discount_for_quantity" );
change in product count and discount percentage as per your requerements
This are test result of my answer for discount(5%) on different quantity :