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

How to Apply Discounts for Category Z Based on Category X & Y Products and Payment Method in WooCommerce?

The code is implemented to provide a discount based on the combination of categories and payment method conditions. 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.

We have used the category IDs and the payment method IDs in the following code snippet. Check out this blog on how to get the ID of a payment method in WooCommerce.

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

function ts_add_discount_for_category_products($cart_object) {
    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 (WC()->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 (WC()->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
                    $discount_amount = $cart_item['data']->get_price() * $discount_percentage / 100;
                    $new_price = $cart_item['data']->get_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
}

Output

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.

How to Apply Discounts for Category Z Based on Category X & Y Products and Payment Method in WooCommerce? - Tyche Softwares

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.

How to Apply Discounts for Category Z Based on Category X & Y Products and Payment Method in WooCommerce? - Tyche Softwares

Similarly, you can also automatically add a product to the cart and offer a discount based on the chosen 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