2017-04-03 13 views
0

私はwebshopのテストケースに取り組んでいます。私は、自動的に商品/商品を訪問時にカートに追加することをお勧めします。だから私はどこでも同じコードを何度も見つけたときにそのようなものを探しました。複数の商品をカートに自動的に追加訪問するWooCommerce

/* 
* goes in theme functions.php or a custom plugin 
**/ 
// add item to cart on visit 
add_action('template_redirect', 'add_product_to_cart'); 
function add_product_to_cart() { 
    if (! is_admin()) { 
     $product_id = 64; 
     $found = false; 
     //check if product already in cart 
     if (sizeof(WC()->cart->get_cart()) > 0) { 
      foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 
       $_product = $values['data']; 
       if ($_product->id == $product_id) 
        $found = true; 
      } 
      // if product not found, add it 
      if (! $found) 
       WC()->cart->add_to_cart($product_id); 
     } else { 
      // if no products in cart, add it 
      WC()->cart->add_to_cart($product_id); 
     } 
    } 
} 

から:

https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/ https://gist.github.com/kloon/2376300

ですから、1つの製品だけを持っている場合、これは正常に動作しますが、私はちょうど1製品以上のものを追加したいと思います。私を助けることができるいくつかのPHPの知識(といくつかのワードプレス)を持っている人はいますか?前もって感謝します!

答えて

2

少なくとも2つの方法があります。 add_to_cart関数を複数回呼び出すか、ループを作成することができます。どちらの方法がダウンして現在地:

方法1

add_action('template_redirect', 'add_product_to_cart'); 
function add_product_to_cart() { 
    if (! is_admin()) { 
     $product_id = 64; 
     $found = false; 
     //check if product already in cart 
     if (sizeof(WC()->cart->get_cart()) > 0) { 
      foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 
       $_product = $values['data']; 
       if ($_product->id == $product_id) 
        $found = true; 
      } 
      // if product not found, add it 
      if (! $found) 
       WC()->cart->add_to_cart($product_id); 
     } else { 
      // if no products in cart, add it 
      WC()->cart->add_to_cart($product_id); 
      WC()->cart->add_to_cart($product_id2); 
      WC()->cart->add_to_cart($product_id3); 
      WC()->cart->add_to_cart($product_id4); 
     } 
    } 
} 

方法2

foreach ($articles as $article) { 
    WC()->cart->add_to_cart($article); 
} 

あなたからすべてのIDを保持articlesと呼ばれる新しい配列を作成する必要があることに注意してください。所望の製品。カートが0以上のアイテムを保持しているかどうかをチェックし、それらのすべてがそこにあるかどうかをチェックすることです。

方法2は、このようになります:

add_action('template_redirect', 'add_product_to_cart'); 
function add_product_to_cart() { 
    if (! is_admin()) { 
     $articles = array(64); 
     $found = false; 

     // check if product already in cart 
     if (sizeof(WC()->cart->get_cart()) > 0) { 
      foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 
       $_product = $values['data']; 

       if (($key = array_search($_product->id, $articles)) !== false) 
        unset($articles[$key]); 
      } 

      // if product not found, add it 
      if (count($articles) > 0) { 
       foreach ($articles as $article) { 
        WC()->cart->add_to_cart($article); 
       } 
      } 
     } else { 
      // if no products in cart, add it 
      foreach ($articles as $article) { 
       WC()->cart->add_to_cart($article); 
      } 
     } 
    } 
} 
関連する問題