In the competitive world of online retail, offering attractive discounts can be a powerful strategy to attract customers and boost sales. However, setting up dynamic discounts for specific variations of a product in WooCommerce might require some custom code. In this tutorial, we’ll explore how to programmatically add a discount feature called “Bulk Savings” for pairs of variations in your WooCommerce store. By offering this variation-based discount, you can encourage customers to purchase multiple pairs of variations and enjoy significant savings. Let’s dive in!
Before we delve into the code, let’s clarify how the Bulk Savings discount works. Consider a product with ID 656 that has two variations: 657 and 658. If a customer adds a pair of 657 and a pair of 658 to their cart, the code will automatically deduct $10 for each pair, resulting in a Bulk Savings discount. For instance, if the customer adds 3 of 657 and 3 of 658 to their cart, the discount will be applied to the three pairs, totaling a $30 reduction.
/**
* Apply "Bulk Savings" discount in WooCommerce cart.
*/
function apply_bulk_savings_discount() {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
// Set the product IDs for checking the conditions
$main_product_id = 656;
$variation_product_ids = array(657, 658);
// Initialize the counters for each variation
$variation_counts = array();
foreach ($variation_product_ids as $variation_product_id) {
$variation_counts[$variation_product_id] = 0;
}
// Calculate the quantity of each variation in the cart
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
if ($product_id === $main_product_id) {
$variation_id = $cart_item['variation_id'];
if (in_array($variation_id, $variation_product_ids)) {
$variation_counts[$variation_id] += $cart_item['quantity'];
}
}
}
// Calculate the discount amount based on the variation counts
$discount_amount = 0;
$pair_count = min($variation_counts[657], $variation_counts[658]);
$discount_amount = $pair_count * 10;
// Apply the discount if it is greater than 0
if ($discount_amount > 0) {
WC()->cart->add_fee('Bulk Savings', -$discount_amount, false, '');
}
}
add_action('woocommerce_cart_calculate_fees', 'apply_bulk_savings_discount');
To achieve this dynamic discount functionality, we need to implement a custom code snippet. The code hooks into the WooCommerce cart and applies the Bulk Savings discount based on the specified conditions. Here’s a breakdown of the code:
$main_product_id = 656;
$variation_product_ids = array(657, 658);
In this part, we set the main product ID (656) and the variation product IDs (657 and 658) as variables to check the conditions.
foreach (WC()->cart->get_cart() as $cart_item) {
// ...
}
This loop iterates through the cart items and counts the quantity of each variation in the cart, considering only the main product and the specified variations.
foreach ($variation_counts as $variation_product_id => $variation_count) {
$discount_amount += floor($variation_count / 2) * 10;
}
Here, we iterate over the variation counts and deduct $10 for each pair of variations found. The discount amount is accumulated accordingly.
if ($discount_amount > 0) {
WC()->cart->add_fee('Bulk Savings', -$discount_amount, false, '');
}
If the discount amount is greater than 0, we apply the Bulk Savings discount to the cart by adding a fee labeled “Bulk Savings” with the calculated discount amount.
To implement the Bulk Savings discount in your WooCommerce store, follow these steps:
functions.php
file or create a custom plugin file.After adding the code, it’s crucial to test the functionality thoroughly to ensure it works as expected. Add products with the specified variations to the cart and verify that the Bulk Savings discount is applied correctly. You can also customize the discount amount or the product and variation IDs to fit your specific requirements.
Offering dynamic discounts for specific variations of a product can significantly enhance your WooCommerce store’s appeal and increase customer engagement. By implementing the Bulk Savings discount programmatically, you provide an incentive for customers to purchase multiple pairs of variations, resulting in higher sales and customer satisfaction. The code snippet provided in this tutorial allows you to effortlessly integrate this powerful discount feature into your WooCommerce store.
Remember, implementing custom code should be approached with caution. Backup your files before making any changes, and if you’re not comfortable with coding, consult a developer to assist you. With the Bulk Savings discount in place, you’ll be able to drive more conversions, attract loyal customers, and stand out in the competitive e-commerce landscape.