Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

discount per product

$
0
0

Replies: 0

Hi
Now I have a discout of 10% on the total card when buying 3 and more product in one card
I did it by the following code placing in function.php

add_action('woocommerce_cart_calculate_fees' , 'custom_cart_discount', 20, 1);
function custom_cart_discount( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    // Limitations: Only when there is 5 or more non on sale items in cart
    $starting_limit = 3;

    // Initialising variables
    $not_on_sale_subtotal = $discount = $items_count = 0;

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

        // For cart items is not on sale
        if( ! $cart_item['data']->is_on_sale() ){
            $not_on_sale_subtotal += (float) $cart_item['line_subtotal'];
            $items_count += $cart_item['quantity'];
        }
    }
    // Discount calculation
    $discount = $not_on_sale_subtotal * 0.1;
    // Applied discount only cart items that are not on sale
    if( $discount && $items_count >= $starting_limit )
        $cart->add_fee( 'הנחת כמות 10%', -$discount );

There is a need now for a different discount:
A gradual discount on the product itself (and not on total) according to the quantity of products and the price of the product
When buying one product: 10%
On the purchase of 2 products: 10% on the expensive product and 20% for the cheapest product
When buying 3 products: the cheapest product will receive the biggest discount (30%) and the most expensive product will receive the smallest discount (10%)


Viewing all articles
Browse latest Browse all 59525

Trending Articles