• 21-04-2025, 00:57:44
    #1
    Başlıkta da belirtiğim gibi aşağıdaki kodu sepetteki 2 üründen hangisinin fiyatı düşükse indirim %'sini ona uygula şeklinde nasıl değiştirebilirim?

    add_action( 'woocommerce_cart_calculate_fees', 'discount_on_2nd_cart_item', 10, 1 );
    function discount_on_2nd_cart_item( $cart ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // baslangic
        $count = 0;
        $percentage = 50; // 50 % indirim orani
    
        // sepetteki her ogenin islenmesi
        foreach ( $cart->get_cart() as $cart_item ) {
            $count++;
            if( 2 == $count){ // yalnizca ikinci urun
                $price = $cart_item['data']->get_price(); // urun fiyati
                $discount = $price * $percentage / 100; // hesaplama
                $second_item = true;
                break; // donguyu durdur
            }
        }
        if( isset($discount) && $discount > 0 )
            $cart->add_fee( __("2. Ürüne %50 İndirim Kampanyası", 'woocommerce'), -$discount );
    }
  • Kabul Edilen Cevap
    • 1 Beğeni
      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 );
          }
      }
  • 21-04-2025, 00:58:48
    #2
    Kişisel Rütbe
    İndirim oranı bölünde ikisinde %50 göstermişsin hocam acaba oradan mı
  • 21-04-2025, 00:59:24
    #3
    Kurumsal PLUS
    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 );
        }
    }
  • 21-04-2025, 01:04:50
    #4
    Webx adlı üyeden alıntı: mesajı görüntüle
    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 );
        }
    }
    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.
  • 21-04-2025, 01:05:50
    #5
    Bu cevap, konu sahibi tarafından kabul edilebilir bir cevap olarak işaretlendi.
    Kurumsal PLUS
    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 );
        }
    }
  • 21-04-2025, 01:15:04
    #6
    Webx adlı üyeden alıntı: mesajı görüntüle
    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 );
        }
    }
    teşekkür ederim hocam,
    oldu.
  • 21-04-2025, 01:16:33
    #7
    Kurumsal PLUS
    Hattushil adlı üyeden alıntı: mesajı görüntüle
    teşekkür ederim hocam,
    oldu.
    Rica ederim.

    I Love ChatGPT