2016-05-17 9 views
1

Ich habe versucht, die WooCommerce Is_Purchasable Option zu ändern, so dass Artikel B käuflich ist, wenn Artikel A in den Warenkorb hinzugefügt wird.Woocommerce: Artikel B ist käuflich, wenn Artikel A zum Warenkorb hinzugefügt wurde

Ich habe es geschafft, Add-to-Cart-Taste für Artikel B mit dem Code unten zu deaktivieren. Wenn jedoch Element A zum Warenkorb hinzugefügt wird, wird die Seite nicht geladen.

Hier ist der Code:

function wc_product_is_in_the_cart($ids) { 
    $cart_ids = array(); 
    foreach(WC()->cart->get_cart() as $cart_item_key => $values) { 
     $cart_product = $values['data']; 
     $cart_ids[] = $cart_product->id; 
    } 
    if (! empty(array_intersect($ids, $cart_ids))) { 
     return true; 
    } else { 
     return false; 
    } 
} 
function wc_product_is_purchasable ($is_purchasable, $product) { 
     $product_ids = array('249'); 
    if (! wc_product_is_in_the_cart($product_ids)) { 
     return ($product->id == 2983 ? false : $is_purchasable); 
    } 
    return $is_purchasable; 
} 
add_filter('woocommerce_is_purchasable', 'wc_product_is_purchasable', 10, 2); 

ich eine Reihe von Möglichkeiten versucht haben, aber nichts scheint zu arbeiten. Wie soll ich weitermachen?

+0

Ahyat, statt 'WC() -> cart-> get_cart()' im Code von Sark, versuchen, sie zu ersetzen durch 'WC() -> cart-> cart_contents' ... Bitte sagen Sie mir, wenn es funktioniert ... danke. – LoicTheAztec

Antwort

0

Versuchen Sie dieses Snippet.

function wc_product_is_purchasable ($is_purchasable, $product) { 
    /* List of product ids that must be in the cart 
    * in order to make the particular product purchasable */ 
    $product_ids = array('249'); 
    // The actual product, on which the purchasable status should be determined 
    $concerned_pid = 2983; 

    if($product->id == $concerned_pid) { 
     // make it false 
     $is_purchasable = false; 
     // get the cart items object 
     $cart_items = WC()->cart->get_cart(); 
     foreach ($cart_items as $key => $item) { 
      // do your condition 
      if(in_array($item["product_id"], $product_ids)) { 
       // Eligible product found on the cart 
       $is_purchasable = true; 
       break; 
      } 
     } 
    } 

    return $is_purchasable; 
} 

add_filter('woocommerce_is_purchasable', 'wc_product_is_purchasable', 99, 2); 
+0

Die Schaltfläche zum Hinzufügen zum Einkaufswagen wurde entfernt, aber es gibt 500 interne Fehler beim Auschecken. – Ahyat

+0

Und hier ist im Fehlerprotokoll: PHP Schwerwiegender Fehler: Aufruf einer Memberfunktion get_cart() auf Null in functions.php in Zeile 504 – Ahyat

Verwandte Themen