2017-01-20 6 views
4

In WooCommerce, möchte ich einen Rabatt von 10% speziell für die Produkte, die nicht im Verkauf sind. Wenn der Warenkorb 5 oder mehr Artikel enthält und nicht im Angebot ist, erhalte ich einen Rabatt von 10%.Warenkorb Rabatt basierend auf Warenkorb Anzahl Artikel und nur für Artikel, die nicht im Verkauf sind

Ich verwende den folgenden Code einen Rabatt zu erhalten, basierend auf Warenkorb Artikelanzahl Einschränkung hier:

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees'); 

/** 
* Add custom fee if more than three article 
* @param WC_Cart $cart 
*/ 

function add_custom_fees(WC_Cart $cart){ 
    if($cart->cart_contents_count < 5){ 
     return; 
    } 
    // Calculate the amount to reduce 
    $discount = $cart->subtotal * 0.1; 
    $cart->add_fee('10% discount', -$discount); 
} 

Aber ich weiß nicht, wie der Rabatt nur für Elemente anzuwenden, die nicht in Verkauf. Wie kann ich es erreichen?

Danke.

+0

'mehr als 5 Produkte' entspricht $ cart-> cart_contents_count <= 5' – JustOnUnderMillions

+0

Haben Sie ein Problem? Funktioniert das? Was ist die Frage genau? –

+0

Ich denke, es ist besser, Sie fragen es in Code Review. –

Antwort

4

Hier ist eine benutzerdefinierte Funktion, die süchtig den Warenkorb ein Rabatt gilt, wenn es 5 oder mehr Artikel im Warenkorb ist und keine Produkte im Angebot:

add_action('woocommerce_cart_calculate_fees' , 'custom_discount', 10, 1); 
function custom_discount($cart_object){ 

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

    // Only when there is 5 or more items in cart 
    if($cart_object->get_cart_contents_count() >= 5): 

     // Initialising variable 
     $is_on_sale = false; 

     // Iterating through each item in cart 
     foreach($cart_object->get_cart() as $cart_item){ 
      // Getting an instance of the product object 
      $_product = new WC_Product($cart_item['product_id']); 

      // If a cart item is on sale, $is_on_sale is true and we stop the loop 
      if($_product->is_on_sale()){ 
       $is_on_sale = true; 
       break; 
      } 
     } 

     ## Discount calculation ## 
     $discount = $cart_object->subtotal * -0.1; 

     ## Applied discount (no products on sale) ## 
     if(!$is_on_sale) 
      $cart_object->add_fee('10% discount', $discount); 

    endif; 
} 

Dieser Code in function.php geht Datei Ihres aktiven untergeordneten Themas (oder Themas) oder auch in einer beliebigen Plugin-Datei.

Dieser Code ist getestet und funktioniert einwandfrei.

+0

Vielen Dank für Ihre Hilfe! – Osman

+0

Immer ein Vergnügen Herr @LoicTheAztec – mysticalghoul

+0

Wie targe ich bestimmte Kategorie für Warenkorb Rabatt? @LoicTheAztec – mysticalghoul

Verwandte Themen