Search code examples
phpwordpresswoocommercecartdiscount

WooCommerce promotional discount: Buy 10 Get 1 Free


I'm trying to set up a specific discount for three variable products (464, 465 and 466). If a customer buys ten products they get one for free.

Based on WooCommerce discount: buy one get one 50% off answer code, I've come up with the following code:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_11th_at_100', 10, 1 );
function add_custom_discount_11th_at_100( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;
    $items_prices = array();

    // Set HERE your targeted variable product ID
    $targeted_product_id = 464;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $qty = intval( $cart_item['quantity'] );
            for( $i = 0; $i < $qty; $i++ )
                $items_prices[] = floatval( $cart_item['data']->get_price());
        }
    }
    $count_items_prices = count($items_prices);
    if( $count_items_prices > 10 ) foreach( $items_prices as $key => $price )
        if( $key % 11 == 1 ) $discount -= number_format($price / 1, 11 );

    if( $discount != 0 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("Buy 10 Get 1 Free"), 'notice');

        // The discount
        $wc_cart->add_fee( 'Buy 10 Get 1 Free', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

But it only works for one product ID. How do I expand it to work for three product Ids?


Solution

  • To make it work for multiple products you could use in_array() php function as follow:

    add_action('woocommerce_cart_calculate_fees', 'buy_ten_get_one_free', 10, 1 );
    function buy_ten_get_one_free( $cart ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
        // Set HERE your targeted variable products IDs
        $targeted_product_ids = array( 464, 465, 466 );
    
        $each_n_items = 10; // Number of items required to get a free one
        $discount = 0; // Initializing
        $items_prices = array(); // Initializing 
    
        foreach ( $cart->get_cart() as $cart_item ) {
            if( in_array( $cart_item['product_id'], $targeted_product_ids ) ) {
                $qty = intval( $cart_item['quantity'] );
    
                for ( $i = 0; $i < $qty; $i++ ) {
                    $items_prices[] = floatval( $cart_item['data']->get_price() );
                }
            }
        }
        $count_items_prices = count($items_prices);
    
        if ( $count_items_prices > $each_n_items ) {
            foreach ( $items_prices as $key => $price ) {
                if ( $key % ($each_n_items + 1) == 1 ) {
                    $discount += number_format($price, 2 );
                }
            }
        }
    
        if ( $discount > 0 ) {
            // Displaying a custom notice (optional)
            wc_clear_notices();
            wc_add_notice( __("Buy 10 Get 1 Free"), 'notice');
    
            // The discount
            $cart->add_fee( __("Buy 10 Get 1 Free"), -$discount, true  );
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme), tested and works.