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

How to Offer a 100% Discount on Category Z When Categories X and Y Are Added to the Cart in WooCommerce?

The following code snippet will help WooCommerce store owners to provide BOGO discounts/ free products based on the combination of categories present in the cart. The code will implement a 100% discount on products from category Z when products from both categories X and Y are added to the cart.

add_action('woocommerce_cart_calculate_fees', 'category_bogo_discount', 20, 1);
add_action('woocommerce_checkout_update_order_review', 'remove_category_bogo_discount');

function category_bogo_discount($cart_object) {
    if (is_admin() && !defined('DOING_AJAX')) return;

    // Define the category IDs for products to buy (X and Y)
    $category_ids_to_buy = array(50, 65); // Categories X and Y

    // Define the category ID for the free product (Z)
    $free_product_category_id = 63; // Category Z

    // Check if both categories X and Y are present in the cart
    $x_category_present = false;
    $y_category_present = false;
    foreach (WC()->cart->get_cart() as $cart_item) {
        $cart_product_cats_ids = wc_get_product_term_ids($cart_item['product_id'], 'product_cat');
        if (in_array(50, $cart_product_cats_ids)) {
            $x_category_present = true;
        }
        if (in_array(63, $cart_product_cats_ids)) {
            $y_category_present = true;
        }
    }

    // If both categories X and Y are present, apply discount
    if ($x_category_present && $y_category_present) {
        $discount_percentage = 100; // 100% discount
        $discount_amount = 0; // Initialize discount amount

        // Calculate the discount amount
        foreach ($cart_object->get_cart() as $cart_item_key => $cart_item) {
            $cart_product_cats_ids = wc_get_product_term_ids($cart_item['product_id'], 'product_cat');
            if (in_array($free_product_category_id, $cart_product_cats_ids)) {
                $discount_amount += $cart_item['data']->get_price() * ($discount_percentage / 100);
            }
        }

        // Apply discount to cart
        if ($discount_amount > 0) {
            $cart_object->add_fee(__('Category Z Discount'), -$discount_amount);
        }
    }
}

Output

When customers add products from categories X,Y, and Z to the cart, the code checks for the presence of products from both categories of X and Y. If both categories are present, a 100% discount is applied for the product present in category Z.

How to Offer a 100% Discount on Category Z When Categories X and Y Are Added to the Cart in WooCommerce? - Tyche Softwares

Similarly, you can also offer a tiered discount based on some rules applied. Check out this post on how to apply a recursive BOGO rule for the same product in WwooCommerce.

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