Dener misiniz ?
Not: Lütfen yedek alın ve sonra deneme yapın.
add_action( 'woocommerce_cart_calculate_fees', 'discount_on_cheaper_cart_item', 10, 1 );
function discount_on_cheaper_cart_item( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Sadece iki ürün varsa uygula
if ( $cart->get_cart_contents_count() != 2 ) {
return;
}
$percentage = 50; // %50 indirim oranı
$prices = array();
$items = array();
// Sepetteki ürünlerin fiyatlarını ve nesnelerini al
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
$price = $cart_item['data']->get_price();
$prices[$cart_item_key] = $price;
$items[$cart_item_key] = $cart_item;
}
// En düşük fiyatı bul
asort($prices); // artan sıraya göre sırala
$cheapest_key = key($prices); // en ucuz ürünün key'i
$cheapest_price = reset($prices);
// İndirim miktarını hesapla
$discount = $cheapest_price * $percentage / 100;
if ( $discount > 0 ) {
$cart->add_fee( __("Ucuz Ürüne %50 İndirim Kampanyası", 'woocommerce'), -$discount );
}
}