1

I want to add a 30% discount on the cheapest item in the cart, except if it already has a discount.

Based on Cart discount for product that cost less in Woocommerce answer code, this is my code attempt:

add_action('woocommerce_cart_calculate_fees', 'discount_on_cheapest_cart_item', 20, 1 );
function discount_on_cheapest_cart_item( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

    // Only for 2 items or more
    if ( $cart->get_cart_contents_count() < 2 ) return;


    // Initialising
    $percentage = 50; // 10 %
    $discount = 0;
    $item_prices = array();



    // Loop though each cart items and set prices in an array
    foreach ( $cart->get_cart() as $cart_item ) {
        
        $product_prices_excl_tax[] = wc_get_price_excluding_tax( $cart_item['data'] );
        
    }

    sort($product_prices_excl_tax);

    if( ! $cart_item['data']->is_on_sale() ){
        $discount = reset($product_prices_excl_tax) * $percentage / 100;
    
        $cart->add_fee( "Discount on cheapest (".$percentage."%)", -$discount );
    }   
}

Is there a way to make it work so if the product with the lowest price is not on sale, then apply a 30%, if it is on sale, don't. But this applies only to the lowest, if any other product is on sale, we skip it.

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
whisper
  • 13
  • 3
  • In connection with the new question you had asked as a comment. That's certainly possible, but If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. P.s. Don't forget to add in your new question what you have tried so far to arrive at a solution – 7uc1f3r Feb 19 '22 at 10:00

1 Answers1

0

The use of if( ! $cart_item['data']->is_on_sale() ) in your code attempt will not be of any influence as this is used outside the foreach loop.

This anwer will apply a discount calculated on the basis of the product with the lowest price, this only if this product is not already on sale.

So you get:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;
    
    // Only for 2 items or more
    if ( $cart->get_cart_contents_count() < 2 ) return;

    // Setting
    $percentage = 30; // 30 %
    
    // Initialize
    $discount = 0;
    $product_prices_on_sale = array();
    $product_prices_excl_tax = array();

    // Loop though each cart items and set prices in an array
    foreach ( $cart->get_cart() as $cart_item ) {
        // When cart contains a on sale product
        if ( $cart_item['data']->is_on_sale() ) {
            // On sale, push to on sale array
            $product_prices_on_sale[] = wc_get_price_excluding_tax( $cart_item['data'] );
        }
        
        // Push to excl tax array
        $product_prices_excl_tax[] = wc_get_price_excluding_tax( $cart_item['data'] );
    }
    
    // Sort an array in ascending order
    sort( $product_prices_on_sale );
    sort( $product_prices_excl_tax );
    
    // Set the internal pointer of an array to its first element
    $p_o_s = reset( $product_prices_on_sale );
    $p_e_t = reset( $product_prices_excl_tax );
    
    // NOT equal
    if ( $p_o_s != $p_e_t ) {
        // Calculate discount
        $discount = $p_e_t * $percentage / 100;

        // Apply discount (negative fee)
        $cart->add_fee( 'Discount on cheapest (' . $percentage . '%)', -$discount );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50