This code allows to you offer set discounts when a customer buys in bulk. The price is adjusted in the cart automatically. Change the thresholds and discount percentage to suit your purpose.
/**
* Set discount for bulk buying
*/
add_action('woocommerce_before_calculate_totals', 'cc_quantity_based_pricing', 9999);
function cc_quantity_based_pricing($cart) {
if (is_admin() && ! defined('DOING_AJAX')) return;
if (did_action('woocommerce_before_calculate_totals') >= 2) return;
// Define discount rules and thresholds
$threshold1 = 10; // Change price if items > 10
$discount1 = 0.05; // Reduce unit price by 5%
$threshold2 = 100; // Change price if items > 100
$discount2 = 0.1; // Reduce unit price by 10%
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$product_id = $cart_item['product_id'];
if ($cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2) {
$price = round($cart_item['data']->get_price() * (1 - $discount1), 2);
$cart_item['data']->set_price( $price );
} elseif ($cart_item['quantity'] >= $threshold2) {
$price = round($cart_item['data']->get_price() * (1 - $discount2), 2);
$cart_item['data']->set_price($price);
}
}
}
View Raw
Code ID: 91096