wordpresswoocommerceproductcartfee

Cart discount for product that cost less in WooCommerce unless that product is already on sale


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.


Solution

  • 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 );