Woocommerce discount based on quantity cart items / progressive percentage discount

CoderAuthor Position

This is a static text. Maybe you want to add a tagline or short message here?

woocommerce discount based on quantity
woocommerce discount based on quantity

woocommerce discount based on quantity / woocommerce discount for multiple items / woocommerce discount for quantity

Apply a Woocommerce TOTAL CART discount to all the items in the cart, depending on the quantity of items added.

Scenario:

  • If no of items in the cart are between 9-12, Apply 5% Discount to all items.
  • If no of items in the cart are between 13-16, Apply 10% Discount to all items.
  • The discount’s should not stack. e.g. when 12 items in cart, apply 5% discount…when 13 items added, remove 5% discount and apply 10% discount.

1) By cart item quantity:

// Utility function that give the discount percentage based on quantity argument
function get_discount_percent( $quantity ){
    if( $quantity < 9 )
        $percent = 0;  // 0 %  ( quantity from 1 to 8 )
    elseif( $quantity >= 9 && $quantity < 13 )
        $percent = 5;  // 5 %  ( quantity from 9 to 12 )
    else
        $percent = 10; // 10 % ( quantity up to 13 )

    return $percent;
}


add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){
    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    ## ----- YOUR SETTING ----- ##
    $percent_1 = 5; //  Discount (5%)
    $percent_2 = 10; // Discount (10%)

    // The WC_Product Object
    $product = wc_get_product($product_id);

    $price = (float) $product->get_price();

    // Set the Product default base price as custom cart item data
    $cart_item_data['discount'][0] = $price;

    // Set the Product discounted price of 5% as custom cart item data
    $cart_item_data['discount'][$percent_1] = $price * (100 - $percent_1) / 100;

    // Set the Product discounted price of 10% as custom cart item data
    $cart_item_data['discount'][$percent_2] = $price * (100 - $percent_2) / 100;

    return $cart_item_data;
}

// Display the product original price
add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );
function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['discount'][0]) ) {
        $product        = $cart_item['data'];
        $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['discount'][0] ) ) );
    }
    return $product_price;
}

// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3 );
function append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){
    // get the percent based on quantity
    $percent = get_discount_percent($cart_item['quantity']);

    if( isset($cart_item['discount'][$percent]) && isset($cart_item['discount'][0]) ) {
        if( $cart_item['data']->get_price() != $cart_item['discount'][0] )
            $product_name .= ' <em>(' . $percent . '% discounted)</em>';
    }
    return $product_name;
}

add_action( 'woocommerce_before_calculate_totals', 'set_custom_discount_cart_item_price', 25, 1 );
function set_custom_discount_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    foreach( $cart->get_cart() as $cart_item ){
        // get the percent based on quantity
        $percentage = get_discount_percent($cart_item['quantity']);
        
        // For items non on sale set a discount based on quantity as defined in
        if( $percentage != 0 && isset($cart_item['discount'][$percentage]) && ! $cart_item['data']->is_on_sale() ) {
            $cart_item['data']->set_price($cart_item['discount'][$percentage]);
        }
    }
}

Or all (discount based on quantity) in one function:

add_action( 'woocommerce_before_calculate_totals', 'custom_discounted_cart_item_price', 10, 1 );
function custom_discounted_cart_item_price( $cart_object ) {

    $discount_applied = false;

    // Set Here your targeted quantity discount
    $t_qty = 12;

    // Iterating through each item in cart
    foreach ( $cart_object->get_cart() as $item_values ) {

        ##  Get cart item data
        $item_id = $item_values['data']->id; // Product ID
        $item_qty = $item_values['quantity']; // Item quantity
        $original_price = $item_values['data']->price; // Product original price

        // Getting the object
        $product = new WC_Product( $item_id );


        // CALCULATION FOR EACH ITEM 
        // when quantity is up to the targetted quantity and product is not on sale
        if( $item_qty >= $t_qty && !$product->is_on_sale() ){
            for($j = $t_qty, $loops = 0; $j <= $item_qty; $j += $t_qty, $loops++);

            $modulo_qty = $item_qty % $t_qty; // The remaining non discounted items

            $item_discounted_price = $original_price * 0.9; // Discount of 10 percent

            $total_discounted_items_price = $loops * $t_qty * $item_discounted_price;

            $total_normal_items_price = $modulo_qty * $original_price;

            // Calculating the new item price
            $new_item_price = ($total_discounted_items_price + $total_normal_items_price) / $item_qty;


            // Setting the new price item
            $item_values['data']->price = $new_item_price;

            $discount_applied = true;
        }
    }
    // Optionally display a message for that discount
    if ( $discount_applied )
        wc_add_notice( __( 'A quantity discount has been applied on some cart items.', 'my_theme_slug' ), 'success' );
}

 

 

2) Globally on cart item count (for non on sale products):

// Utility function that give the discount percentage based on quantity argument
function get_discount_percent( $quantity ){
    if( $quantity < 9 )
        $percent = 0;  // 0 %  ( quantity from 1 to 8 )
    elseif( $quantity >= 9 && $quantity < 13 )
        $percent = 5;  // 5 %  ( quantity from 9 to 12 )
    else
        $percent = 10; // 10 % ( quantity up to 13 )

    return $percent;
}

// Utility function that count cart items that are not on sale
function get_non_on_sale_cart_items_count(){
    $items_count = 0;
    foreach( WC()->cart->get_cart() as $cart_item ){
        if( ! $cart_item['data']->is_on_sale() ){
            $items_count += $cart_item['quantity'];
        }
    }
    return $items_count;
}


add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){
    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    ## ----- YOUR SETTING ----- ##
    $percent_1 = 5; //  Discount (5%)
    $percent_2 = 10; // Discount (10%)

    // The WC_Product Object
    $product = wc_get_product($product_id);

    $price = (float) $product->get_price();

    // Set the Product default base price as custom cart item data
    $cart_item_data['discount'][0] = $price;

    // Set the Product discounted price of 5% as custom cart item data
    $cart_item_data['discount'][$percent_1] = $price * (100 - $percent_1) / 100;

    // Set the Product discounted price of 10% as custom cart item data
    $cart_item_data['discount'][$percent_2] = $price * (100 - $percent_2) / 100;

    return $cart_item_data;
}

// Display the product original price
add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );
function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['discount'][0]) ) {
        $product        = $cart_item['data'];
        $product_price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $cart_item['discount'][0] ) ) );
    }
    return $product_price;
}

// Display the product name with the discount percentage
add_filter( 'woocommerce_cart_item_name', 'append_percetage_to_item_name', 20, 3 );
function append_percetage_to_item_name( $product_name, $cart_item, $cart_item_key ){
    // Get cart items count
    $items_count = get_non_on_sale_cart_items_count();

    // get the percent based on quantity
    $percent = get_discount_percent($items_count);

    if( isset($cart_item['discount'][$percent]) && isset($cart_item['discount'][0]) ) {
        if( $cart_item['data']->get_price() != $cart_item['discount'][0] )
            $product_name .= ' <em>(' . $percent . '% discounted)</em>';
        elseif ( $cart_item['data']->is_on_sale() && $percent != 0 ){
            $product_name .= ' <em>(Item on sale)</em>';
        }
    }
    return $product_name;
}

// Change cart item price
add_action( 'woocommerce_before_calculate_totals', 'set_custom_discount_cart_item_price', 25, 1 );
function set_custom_discount_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    // Get cart items count
    $items_count = get_non_on_sale_cart_items_count();

    // get the percent based on quantity
    $percentage = get_discount_percent($items_count);

    foreach( $cart->get_cart() as $cart_item ){
        // For items non on sale set a discount based on quantity as defined in
        if( $percentage != 0 && isset($cart_item['discount'][$percentage]) && ! $cart_item['data']->is_on_sale() ) {
            $cart_item['data']->set_price($cart_item['discount'][$percentage]);
        }
    }
}

3) add a discount to the whole cart by apply a negative fee

add_action( 'woocommerce_before_calculate_totals', 'custom_discounted_cart_item_price', 20, 1 );
function custom_discounted_cart_item_price( $cart ) {
  $total_cart_items_count = $cart->get_cart_contents_count();
  $discount = null;

  // Apply 5% discount between 9 - 12 items
  if( $total_cart_items_count >= 9 && $total_cart_items_count <= 12 ) {
    // Calculate 5% of cart total
    $discount = $cart->get_cart_contents_total() * 5 / 100;
  }

  // Apply 10% discount between 13 - 16 items
  if( $total_cart_items_count >= 13 && $total_cart_items_count <= 16 ) {
    // Calculate 10% of cart total
    $discount = $cart->get_cart_contents_total() * 10 / 100;
  }

  if( !empty( $discount ) ) {
    $cart->add_fee(
      __( 'Discount', 'woocommerce' ) . '($percent%)',
      -$discount,
      false // TAX
    );
  }
}

source https://stackoverflow.com/questions/52363715/cart-item-quantity-progressive-percentage-discount-in-woocommerce-3/52370789#52370789