Hattushil adlı üyeden alıntı: mesajı görüntüle
2 ürün ekleyince fiyatı düşük olanda indirim uyguluyor ama 3. ürün eklendiğinde indirimi kaldırıyor.
ilk kodda 3. ürün eklense de indirim uygulanıyordu.
Yeni hali

add_action( 'woocommerce_cart_calculate_fees', 'discount_on_cheapest_of_two_items', 10, 1 );
function discount_on_cheapest_of_two_items( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 50; // %50 indirim oranı

    $prices = [];
    $items = [];

    // Sepetteki tüm ürünleri topla
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $price = $cart_item['data']->get_price();
        // Her ürün için fiyat ve key'i sakla
        $prices[$cart_item_key] = $price;
        $items[$cart_item_key] = $cart_item;
    }

    // En az iki ürün varsa işleme devam et
    if ( count($prices) < 2 ) {
        return;
    }

    // Fiyatlara göre artan sırala
    asort($prices);

    // İlk iki en ucuz ürünü al
    $cheapest_two = array_slice($prices, 0, 2, true);
    $cheapest_key = key($cheapest_two); // ilk eleman key
    $cheapest_price = reset($cheapest_two); // en düşük fiyat

    // İndirim hesapla
    $discount = $cheapest_price * $percentage / 100;

    if ( $discount > 0 ) {
        $cart->add_fee( __("En Ucuz Ürüne %50 İndirim Kampanyası", 'woocommerce'), -$discount );
    }
}