Sebas
Sebas

Reputation: 41

WooCommerce bulk discount based on product ids

I am trying to program a bulk discount in WooCommerce.

For now, I have this

function se_bulkdiscount_on_ids( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    // Set special prices
    $special_price = array(
        2 => '1.2',
        3 => '1.3',
        4 => '1.4',
        5 => '1.5',
        6 => '1.6',
        7 => '1.7',
        8 => '1.8',
    );

    // Set product ids
    $specific_product_ids = array( 1465, 1785 );

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) { 
        // Get product id
        $product_id = $cart_item['product_id'];
        // Compare
        if ( in_array( $product_id, $specific_product_ids ) ) {
            foreach($special_price as $quantity => $price){
                if($cart_item['quantity'] >= $quantity){
                    $cart_item['data']->set_price( $price );
                }
            }          
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'se_bulkdiscount_on_ids', 10, 1 );

But how can I set this discount only on specific product IDs?

If I have ID 1300 1x and 1403 2x this has a quantity of 3 together than the price is 1.62 per piece

Upvotes: 2

Views: 360

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

Assuming you mean this? comment with explanation added in the code

function se_bulkdiscount_on_ids( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    /* SETTINGS */

    // Set special prices
    $special_price = array(
        1 => 1.1,   
        2 => 1.2,
        3 => 1.3,
        4 => 1.4,
        5 => 1.5,
        6 => 1.6,
        7 => 1.7,
        8 => 1.8,
    );

    // Set product ids
    $specific_product_ids = array( 30, 813 );

    /* END SETTINGS */

    // total items
    $count = 0;

    // Loop through cart items (count)
    foreach ( $cart->get_cart() as $cart_item ) {    
        // Get product id
        $product_id = $cart_item['product_id'];

        // Quantity
        $product_quantity = $cart_item['quantity'];

        // Compare
        if ( in_array( $product_id, $specific_product_ids ) ) {
            $count += $product_quantity;
        }
    }

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {    
        // Get product id
        $product_id = $cart_item['product_id'];

        // Compare
        if ( in_array( $product_id, $specific_product_ids ) ) {
            // If count is in range of the array
            if ( $count >= 2 & $count <= count( $special_price ) ) {
                // set new price
                $cart_item['data']->set_price( $special_price[$count] );                    
            } elseif ( $count > count( $special_price ) ) {
                // set new price
                $cart_item['data']->set_price( end($special_price) );       
            }             
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'se_bulkdiscount_on_ids', 10, 1 );

Upvotes: 1

Related Questions