Code giảm giá, khuyến mãi, tặng kèm

Eric (kedochanh)
0
22/05/25
22/05/2025
// Add BOGO (Buy One Get One) Offers Based on WooCommerce Cart Total Range
//Ex. Spend between $50 and $100: Get Product A for free.
//Ex. Spend between $100 and $200: Get Product A and Product B for free.
function ts_add_products_to_cart() {
    global $woocommerce;
    $cart_total = $woocommerce->cart->total;
    if (!is_admin()) {
        // Define your cart total ranges and corresponding products
        $ranges_and_products = array(
            array('min' => 50, 'max' => 100, 'products' => array(470)),       // For $0 to $100, add Product A
            array('min' => 100, 'max' => 200, 'products' => array(457, 470)), // For $101 to $200, add Product A & Product B
        );
        foreach ($ranges_and_products as $range) {
            $min = $range['min'];
            $max = $range['max'];
            if ($cart_total >= $min && $cart_total <= $max) {
                $products_to_add = $range['products'];
                // Check if the products are already in the cart
                $found_products = array();
                foreach ($products_to_add as $product_id) {
                    foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
                        $_product = $values['data'];
                        if ($_product->get_id() == $product_id) {
                            $found_products[] = $product_id;
                        }
                    }
                }
                // If any product is not found, add it to the cart
                foreach ($products_to_add as $product_id) {
                    if (!in_array($product_id, $found_products)) {
                        WC()->cart->add_to_cart($product_id);
                    }
                }
                // Stop further processing to prevent adding multiple sets of products
                return;
            }
        }
    }
}
add_action('template_redirect', 'ts_add_products_to_cart');
//Add Free Gift based on Cart Quantity in WooCommerce
//Ex. range 3-6 free product 1
//Ex. rang 8-12 free product 2 (remove product 1)
add_action('template_redirect', 'ts_add_product_to_cart');
function ts_add_product_to_cart() {
    if (!is_admin()) {
        global $woocommerce;
      
        $free_product_1_id = 470; // Replace with your free product 1 id
        $free_product_2_id = 54; // Replace with your free product 2 id
        $cart_quantity = $woocommerce->cart->cart_contents_count;
        // Remove free product 1 if already in the cart
        ts_remove_free_product($free_product_1_id);
        // Remove free product 2 if already in the cart
        ts_remove_free_product($free_product_2_id);
        // Check if cart quantity is between 3 and 6
        if ($cart_quantity >= 3 && $cart_quantity <= 6) {
            // Get the product name for Free Product 1
            $free_product_2_name = get_the_title($free_product_2_id);
             wc_add_notice("If you select beyond 6, you will get $free_product_2_name for free.", 'notice');
            // Add Free Product 1
            ts_add_free_product($free_product_1_id);
        } elseif ($cart_quantity >= 8 && $cart_quantity <= 12) {
            // Add Free Product 2
            ts_add_free_product($free_product_2_id);
        }
    }
}
function ts_add_free_product($free_product_id) {
    global $woocommerce;
    // Check if free product not already in cart
    if (!is_free_product_in_cart($free_product_id)) {
        $woocommerce->cart->add_to_cart($free_product_id);
    }
}
function ts_remove_free_product($free_product_id) {
    global $woocommerce;
    // Check if free product is in the cart
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
        $_product = $values['data'];
        if ($_product->get_id() == $free_product_id) {
            // Remove the free product
            $woocommerce->cart->remove_cart_item($cart_item_key);
        }
    }
}
function is_free_product_in_cart($free_product_id) {
    global $woocommerce;
    // Check if free product is in the cart
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
        $_product = $values['data'];
        if ($_product->get_id() == $free_product_id) {
            return true;
        }
    }
    return false;
}
//Offer a WooCommerce BOGO Deal Based on the Minimum and Maximum Quantity Range of Specific Product
//Ex. range 5-10 free product
add_action('template_redirect', 'ts_add_product_to_cart');
function ts_add_product_to_cart() {
    if (!is_admin()) {
        global $woocommerce;
        $product_a_id = 922;
        $free_product_id = 929;
        // Set the targeted quantity range for the parent product A
        $min_targeted_qty = 5;
        $max_targeted_qty = 10;
        // Loop through cart items
        foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
            // Check if the current product is the parent product
            if ($cart_item['product_id'] == $product_a_id) {
                // Check if the quantity of the parent product A falls within the specified range
                if ($cart_item['quantity'] >= $min_targeted_qty && $cart_item['quantity'] <= $max_targeted_qty) {
                    // Check if the free product is already in the cart
                    $free_product_cart_id = WC()->cart->generate_cart_id($free_product_id);
                    $free_product_in_cart = WC()->cart->find_product_in_cart($free_product_cart_id);
                    // If the free product is not in the cart, add it
                    if (!$free_product_in_cart) {
                        WC()->cart->add_to_cart($free_product_id);
                    }
                } else {
                    // If the quantity condition is not met, remove the free product
                    ts_remove_free_product($free_product_id);
                }
            }
        }
    }
}
function ts_remove_free_product($free_product_id) {
    global $woocommerce;
    // Check if the free product is in the cart
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
        $_product = $values['data'];
        if ($_product->get_id() == $free_product_id) {
            // Remove the free product
            $woocommerce->cart->remove_cart_item($cart_item_key);
        }
    }
}
//Offer a 100% Discount on Category Z When Categories X and Y Are Added to the Cart in WooCommerce?
//Ex. Buy category X + y will discount category Z
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);
        }
    }
}

https://www.tychesoftwares.com/how-to-offer-a-100-discount-on-category-z-when-categories-x-and-y-are-added-to-the-cart-in-woocommerce/

Tags :
Tags: ,