2016-05-17 6 views
1

に追加した場合、項目Bが購入可能である、項目Bは購入可能です。Woocommerce:アイテムAがカートに追加された場合の項目Aは私がするようにWooCommerce Is_Purchasableオプションを変更しようとしていたカート

私は以下のコードでアイテムBのためのアドオン・ツー・カートボタンを無効にするために管理。しかし、アイテムAがカートに追加されると、ページはロードされません。ここで

コードです:

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); 

私は方法の束を試してみたが、何も作業していないようです。私はこれをどのように続けるべきですか?

+0

Ahyat、 - > cart-> get_cart()'サークのコードでは、WC() 'でそれを置き換えるためにしてみてください - > cart-> cart_contents' ...あれば教えてくださいそれは働く...ありがとう。 – LoicTheAztec

答えて

0

はこのスニペットを試してみてください。代わりに、WC() `の

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

カートに追加]ボタンが削除されますが、それは500内部エラー時にチェックアウトを提供します。 PHPの致命的なエラー: – Ahyat

+0

そして、ここでは、エラー・ログにあるメンバ関数get_cartに呼び出し()はnullでのfunctions.php内のライン504上の – Ahyat

関連する問題