'Woocommerce: calculation of the discount for several items of the same product? [duplicate]
I would like to change the code below to calculate the discount for 3 different product id's. If the customer buys 3 items of one product id, he will receive a 15% discount on everything, and if he buys 6 items, he will receive a 20% discount.
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Specific tag
$specific_tag = array( 'ashwagandha' );
// Discount
$discount = 20;
// Min quantity
$minimun_quantity = 6;
/* END SETTINGS */
// Counter
$current_quantity = 0;
// Loop though each cart item
foreach ( $cart->get_cart() as $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
// Has certain tag
if ( has_term( $specific_tag, 'product_tag', $product_id ) ) {
// Quantity
$product_quantity = $cart_item['quantity'];
// Add to total
$current_quantity += $product_quantity;
}
}
// Greater than or equal to
if ( $current_quantity >= $minimun_quantity ) {
// Add fee
$cart->add_fee( __( 'Rabat', 'woocommerce' ), -$discount, false );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
