2017-02-03 37 views
2

WooCommerceでは複数の商品を一度に追加できるように、@ jtsternbergのWooCommerce: Allow adding multiple products to the cart via the add-to-cart query stringを実装しましたが、実際に複数の商品を使用しようとしている顧客から多くの苦情がありました。複数の製品を含むリンク。カートに複数の商品を追加する

まず、顧客がチェックアウトをクリックしてブラウザの「戻る」ボタンをクリックすると、すべての商品数が増えます。私はカートに追加動作が完了した後に追加のパラメータを削除したカートURLにユーザーをリダイレクトすることでこれを解決しましたが、それは理想的ではありません。

私が本当に欲しいのは、商品がカートに入っていないかどうかを確認し、商品がまだなければカートに追加することです。誰かが似たようなことをやったのですか

ワーキング更新: 私は、デフォルトの追加・トゥ・カートの挙動との競合を避けるために、完全に別のparamの名前を使用するように@jtsternbergからコードを変更することになりました。それから、@ LoicTheAztecの提案するコードを以下のコードを使ってチェックして、その新しいparamが存在するかどうかを調べることができました。

function custom_product_link() { 
    if (empty($_REQUEST['multi-product-add'])) { 
    return; 
    } 

    $product_ids = explode(',', $_REQUEST['multi-product-add']); 

    foreach ($product_ids as $product_id) { 
    $product_id  = apply_filters('woocommerce_add_to_cart_product_id', absint($product_id)); 
    $was_added_to_cart = false; 
    $adding_to_cart = wc_get_product($product_id); 

    if (! $adding_to_cart) { 
     continue; 
    } 

    $add_to_cart_handler = apply_filters('woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart); 

    if ('simple' !== $add_to_cart_handler) { 
     continue; 
    } 

    // For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature. 
    $quantity   = empty($_REQUEST['quantity']) ? 1 : wc_stock_amount($_REQUEST['quantity']); 
    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity); 

    if ($passed_validation && false !== WC()->cart->add_to_cart($product_id, $quantity)) { 
     wc_add_to_cart_message(array($product_id => $quantity), true); 
    } 
    } 
    if (wc_notice_count('error') === 0) { 
    // If has custom URL redirect there 
    if ($url = apply_filters('woocommerce_add_to_cart_redirect', false)) { 
     wp_safe_redirect($url); 
     exit; 
    } elseif (get_option('woocommerce_cart_redirect_after_add') === 'yes') { 
     wp_safe_redirect(wc_get_cart_url()); 
     exit; 
    } 
    } 
} 

function check_product_added_to_cart($passed, $product_id, $quantity) { 
    if (!empty($_REQUEST['multi-product-add'])) { 
    foreach (WC()->cart->get_cart() as $cart_key => $cart_item){ 
     // if products are already in cart: 
     if($cart_item['product_id'] == $product_id) { 
     // Set the verification variable to "not passed" (false) 
     $passed = false; 
     // (Optionally) Displays a notice if product(s) are already in cart 
     // wc_add_notice('<strong>' . $btn['label'] . '</strong> ' . __('This product is already in your cart.', 'woocommerce'), 'error'); 
     // Stop the function returning "false", so the products will not be added again 
     return $passed; 
     } 
    } 
    } 
    return $passed; 
} 
add_action('wp_loaded', 'custom_product_link', 15); 
add_action('woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3); 

答えて

2

あなたはその中woocommerce_add_to_cart_validationフィルターフックを持って使用しているコードのように、あなたのようなもので、カート内の既存の製品珍しいかどうかを確認するために、カスタムフック機能でそれを使用することができます。ここでは完全なセクションがあります:

add_action('woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3); 
function check_product_added_to_cart($passed, $product_id, $quantity) { 
    foreach (WC()->cart->get_cart() as $cart_key => $cart_item){ 
     // if products are already in cart: 
     if($cart_item['product_id'] == $product_id) { 
      // Set the verification variable to "not passed" (false) 
      $passed = false; 
      // (Optionally) Displays a notice if product(s) are already in cart 
      wc_add_notice('<strong>' . $btn['label'] . '</strong> ' . __('This product is already in your cart.', 'woocommerce'), 'error'); 
      // Stop the function returning "false", so the products will not be added again 
      return $passed; 
     } 
    } 
    return $passed; 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルやも任意のプラグインファイルになります。

コードがテストされ、これは通常...あなたのカスタマイズと製品の数量については

に動作する必要があり、あなたは...いくつかの条件で$cart_item['quantity']

+0

うわーを$quantity引数を使用することができ、これは私が取り組んでいたものよりずっと簡単です!私はいくつかのテストを実行する必要がありますが、これは通常のカートへの追加動作を妨害しますか?ユーザーが商品をクリックしたとき – bhamrick

+0

@bhamrickあなたはテストしなければなりません...あなたの製品、設定に依存しているので、私は言うことができません...このフィルタコードは、顧客がカートに同じアイテムを2回追加することを避けるだけです。一般的に、顧客は2倍の同じ商品をカートに入れないので、数量を設定/更新する...それは問題だとは思わない。 – LoicTheAztec

+1

ありがとうございます!これはうまくいった。私はデフォルトの 'add-to-cart'から' multi-product-add'にparam名を変更してしまいました。そして 'foreach'文を' if(!empty($ _REQUEST ['multi -product-add ']))) 'ブロックを使用します。 – bhamrick

関連する問題