Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Apply Payment Method-Based Discounts for Category-Specific Products in WooCommerce?

Most stores offer discounts based on either payment methods or product categories, but what if you could combine both for maximum impact? In this guide, we’ll show you how to set up a dynamic discount in WooCommerce that activates when customers choose a specific payment method, applying a special discount to products from a selected category. Let’s dive in to see how this simple yet effective code snippet can enhance your checkout process and make your promotions

Solution: Apply Payment Method-Based Discounts for Category-Specific Products in WooCommerce

This code snippet customizes the WooCommerce checkout process by applying a discount to products from a specific category if the targeted payment method (specified as ‘cheque’ in the code) is selected by the customer.
Note: Using simple snippets you can also get the id of a payment method in WooCommerce that will bring in the payment ids alongside each payment method.

add_action('woocommerce_before_calculate_totals', 'ts_adjust_product_prices_based_on_payment_method');

function ts_adjust_product_prices_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 ID
    $discount_category_id = 63; // Replace with your actual discount category ID
    $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) {
        // 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 the specified category
            if (has_term($discount_category_id, 'product_cat', $product_id)) {
                // Calculate the discount amount for the product
                $original_price = $cart_item['data']->get_price();
                $discount_amount = $original_price * $discount_percentage / 100;
                $new_price = $original_price - $discount_amount;

                // Set the new price for the product in the cart
                $cart_item['data']->set_price($new_price);
            }
        }
    }
}

// Ensure that payment method change triggers a recalculation of the checkout
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
}
flexi bogo cta banner image


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

Let’s imagine that the customer has added items from various categories, including the specific category targeted in the code, to the cart as shown below.

How to Apply Payment Method-Based Discounts for Category-Specific Products in WooCommerce? - Tyche Softwares


When the customer proceeds to checkout and selects the targeted payment method ‘cheque’, the discount will only be applied to products belonging to the category with ID 63, which is the category for Electronic Accessories.

How to Apply Payment Method-Based Discounts for Category-Specific Products in WooCommerce? - Tyche Softwares


If the customer selects any payment method other than the specified one, the discount will not be applied to the items belonging to the specific category, as shown below.

How to Apply Payment Method-Based Discounts for Category-Specific Products in WooCommerce? - Tyche Softwares

You can build on this customization to offer discounts based on products from multiple categories, like X and Y, and the chosen payment method. This flexible approach allows you to create dynamic promotions that cater to various customer needs and purchasing habits.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x