With WooCommerce, setting up conditional discounts based on product categories and payment methods can transform your store’s promotional strategy. For instance, you could offer special discounts when customers purchase products from specific categories, combined with their choice of payment method.
Solution: Apply Discounts for Category Z Based on Category X & Y Products and Payment Method in WooCommerce
The provided code snippet adds a discount for products in category Z based on the presence of products from categories X and Y in the WooCommerce cart, along with a specific payment method selected.
Note: The code makes use of category IDs and payment method IDs. Check out this blog on how to get the ID of a payment method in WooCommerce.
add_action('woocommerce_before_calculate_totals', 'ts_apply_discount_based_on_payment_method'); function ts_apply_discount_based_on_payment_method($cart) { // Avoid running this function in admin and ensure AJAX compatibility if (is_admin() && !defined('DOING_AJAX')) return; // Define your targeted payment method $targeted_payment_method = 'cheque'; // Define the discount category IDs $category_id_x = 50; // Category ID for category X $category_id_y = 63; // Category ID for category Y $category_id_z = 16; // Category ID for category Z $discount_percentage = 10; // Replace with the desired discount percentage $chosen_payment_method = WC()->session->get('chosen_payment_method'); // Check if the chosen payment method is the targeted one if ($targeted_payment_method === $chosen_payment_method) { // Check if both category X and category Y items are present in the cart $category_x_present = false; $category_y_present = false; // Iterate through each item in the cart foreach ($cart->get_cart() as $cart_item_key => $cart_item) { $product_id = $cart_item['product_id']; // Check if the product belongs to category X if (has_term($category_id_x, 'product_cat', $product_id)) { $category_x_present = true; } // Check if the product belongs to category Y if (has_term($category_id_y, 'product_cat', $product_id)) { $category_y_present = true; } } // Apply the discount if both category X and category Y items are present if ($category_x_present && $category_y_present) { // Iterate through each item in the cart foreach ($cart->get_cart() as $cart_item_key => $cart_item) { $product_id = $cart_item['product_id']; // Check if the product belongs to category Z if (has_term($category_id_z, 'product_cat', $product_id)) { // Apply the discount to the product $original_price = $cart_item['data']->get_price(); $discount_amount = $original_price * $discount_percentage / 100; $new_price = $original_price - $discount_amount; $cart_item['data']->set_price($new_price); } } } } } // Add jQuery to refresh payment methods on the checkout page add_action('woocommerce_review_order_before_payment', 'ts_refresh_payment_methods'); function ts_refresh_payment_methods() { ?> <script type="text/javascript"> (function ($) { $(document.body).on('change', 'input[name^="payment_method"]', function () { $('body').trigger('update_checkout'); }); })(jQuery); </script> <?php }
This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…
BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?
Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.
Output
Scenario | Discounted/Free Product | Example |
Buy Products from Category X and Y in cart; Payment method should be ‘cheque’ | Discount applied to products from Category Z if both Categories X and Y are also in the cart | Add products from Category X and Y, choose ‘cheque’ as the payment method, and get a discount on Category Z products if they are in the cart. |
Let’s assume that the products from categories X , Y, and Z are added to the cart. The code checks if the products added to the cart belong to the categories X and Y. It also checks for the chosen payment method that matches the targeted payment method ‘cheque’ as specified in the code. When both conditions are met, the discount is applied to the products of category Z if present in the cart.
If the category X and Y are added to the cart, but since the chosen payment method doesn’t meet the targeted payment method the discount will not be applied to the products of category Z.
Similar to this customization, you can also consider implementing a straightforward discount strategy where a product is automatically added to the cart based solely on the selected payment method. This method simplifies the setup and ensures that customers receive a discount without needing to meet multiple criteria.