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

How to Apply Discounts to Specific Category Products Based on Selected Payment Method in WooCommerce?

This post will guide you to offer targeted discounts for a specific category of products added to cart during the checkout process. The code snippet works in a scenario to apply discounts to products within a specific category when customers choose particular payment methods during checkout.

add_action('woocommerce_cart_calculate_fees', 'ts_add_discount_for_category_y_products', 20, 1);

function ts_add_discount_for_category_y_products($cart_object) {
    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 (WC()->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)) {
                // Apply the discount to the product
                $discount_amount = $cart_item['data']->get_price() * $discount_percentage / 100;
                $cart_item['data']->set_price($cart_item['data']->get_price() - $discount_amount);
            }
        }
    }
}

// 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
}

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 Discounts to Specific Category Products Based on Selected Payment Method 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 Discounts to Specific Category Products Based on Selected Payment Method 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 Discounts to Specific Category Products Based on Selected Payment Method in WooCommerce? - Tyche Softwares


And, here is an easy way to fetch the ID of the payment methods on the checkout page. Refer to this post to learn how to get the id of a payment method in WooCommerce that will bring in the payment ids alongside each payment method.

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

Share It:

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