This customization is useful in implementing promotions targeting the product quantity as well as certain categories. For instance, an online electronic store might have a variety of categories such as SmartPhone(X), Laptop(Y), Electronic(Z), etc. The promotion works when the following condition is met. i.e. when products from both categories X and Y are bought in a fixed quantity number, a free product from the category Z is offered.
add_action('woocommerce_add_to_cart', 'ts_add_product_cat', 10, 2); function ts_add_product_cat($cart_item_key, $product_id) { $category_ids_to_buy = array(50, 65); // Categories X and Y $free_category_id = 63; // Category Z $required_quantity = 4; // Total combined quantity required to trigger free product // Initialize counters for each category $category_counts = array_fill_keys($category_ids_to_buy, 0); // Update counters based on items in the cart foreach (WC()->cart->get_cart() as $cart_item) { $cart_product_cats_ids = wc_get_product_term_ids($cart_item['product_id'], 'product_cat'); foreach ($category_ids_to_buy as $cat_id) { if (in_array($cat_id, $cart_product_cats_ids)) { $category_counts[$cat_id] += $cart_item['quantity']; } } } // Check if the combined count of products from categories X and Y meets or exceeds the required quantity $total_quantity = array_sum($category_counts); if ($total_quantity >= $required_quantity) { // Check if there's a free product in the cart already, if not, add one $free_product_in_cart = 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($free_category_id, $cart_product_cats_ids)) { $free_product_in_cart = true; break; } } // If there's no free product in the cart, add one if (!$free_product_in_cart) { // Get a free product from the specified category $free_product_id = ts_get_free_product_from_category($free_category_id); // If the free product is available, add it to the cart if ($free_product_id) { WC()->cart->add_to_cart($free_product_id); } } } } function ts_get_free_product_from_category($category_id) { // Implement this function to dynamically get a free product ID from the specified category $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $category_id, ), ), ); $products_query = new WP_Query($args); if ($products_query->have_posts()) { $products_query->the_post(); $free_product_id = get_the_ID(); wp_reset_postdata(); return $free_product_id; } return null; // Return null if no free product is found }
Output
When customers purchase items meeting the total combined quantity of 4 as specified in the code($required_quantity = 4;) from both categories X and Y (Smartphone+Laptop), the free product from Category Z (Electronic Accessories) will be automatically added to the cart.
If the total quantity of products from the specified categories of X and Y is not reached, then the free product will not be added.
Likewise, you can also offer discounts on category Y when purchasing category X within a specified quantity range in WooCommerce.