2017-07-15 4 views
1

私はWooCommerceを使用してオンラインの食品配送ウェブサイトを作成しており、購入するための「Make it a Meal」タイプのアップセルを作成する必要があります。商品カテゴリーに基づいて条件付きでWooCommerceカートアイテムを操作する

「ドリンクを作る」という1つの製品を作成しました。ユーザーはドリンクと好みのサイド(バリエーションを使用)を選択できます。しかし、私が必要とするのは、これらの製品のうちの1つだけが主要な食事ごとに購入できることです。

例:「それは第二ください

は「メインの食事」カテゴリーの製品がカートに追加され
  • ユーザーがカートに製品「それに食事を作る」ことができ
  • ユーザーが追加することはできません。 「食事ミール」カテゴリの2番目の製品、すなわち製品ごとに1つしかない場合を除き、「食事」製品を除きます。
  • 「メインミール」製品がカートから取り除かれた場合は、「ミールドミール」製品も取り除かれます。

誰もこの種の機能を以前に達成しましたか?私は様々なプラグインを試しましたが、現在は運がありませんでした。

答えて

0

更新:

をそれは(必要に応じてエラー通知を表示する)アドオンのカートイベントに最初にチェックすることが可能です:

add_filter('woocommerce_add_to_cart_validation', 'checking_products_added_to_cart', 10, 3); 
function checking_products_added_to_cart($passed, $product_id, $quantity) { 

    $cat_add = 'Make it a Meal'; 
    $cat_src = 'Main Meal'; 

    if(has_term($cat_add, 'product_cat', $product_id)): 

     $main_meal_count = 0; 
     $make_it_a_meal_count = 0; 

     foreach (WC()->cart->get_cart() as $cart_item){ 

      // Counting 'Main Meal' products in cart (with quantity) 
      if(has_term($cat_src, 'product_cat', $cart_item['product_id'])) 
       $main_meal_count += $cart_item['quantity']; 

      // Counting 'Make it a Meal' products in cart (with quantity) 
      if(has_term($cat_add, 'product_cat', $cart_item['product_id'])) 
       $make_it_a_meal_count += $cart_item['quantity']; 
     } 

     if($main_meal_count < ($make_it_a_meal_count + $quantity)) { 
      $passed = false; 

      // Displaying a message (optionnal) 
      wc_add_notice('my custom error message…', 'error'); 
     } 
    endif; 

    return $passed; 
} 

カート数量が変更されたとき、あなたはまた、確認する必要がありますまたはカートアイテムが削除されました:

add_action('woocommerce_calculate_totals', 'check_removed_cart_items', 10, 1); 
function check_removed_cart_items($cart_object) { 

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

    $cat_add = 'Make it a Meal'; 
    $cat_src = 'Main Meal'; 

    $main_meal_count = 0; 
    $make_it_a_meal_count = 0; 

    // First loop: Counting cart items 'Main Meal' and 'Make it a Meal' 
    foreach ($cart_object->get_cart() as $item_values){ 

     // Counting 'Main Meal' products in cart (with quantity) 
     if(has_term($cat_src, 'product_cat', $item_values['product_id'])) 
      $main_meal_count += $item_values['quantity']; 

     // Counting 'Make it a Meal' products in cart (with quantity) 
     if(has_term($cat_add, 'product_cat', $item_values['product_id'])){ 
      $make_it_a_meal_count += $item_values['quantity']; 
     } 
    } 

    $difference = intval($make_it_a_meal_count - $main_meal_count); 

     echo '<p>$main_meal_count is '.$main_meal_count.'</p>'; 
     echo '<p>$make_it_a_meal_count is '.$make_it_a_meal_count.'</p>'; 
     echo '<p>$difference is '.$difference.'</p>'; 

    if($main_meal_count < $make_it_a_meal_count) { 

     echo '<p>case1</p>'; 


     // Second Loop: Make necessary actions 
     foreach ($cart_object->get_cart() as $cart_item_key => $cart_item){ 

      // Targeting 'Make it a Meal' 
      if(has_term($cat_add, 'product_cat', $cart_item['product_id'])){ 

       $item_qty = intval($cart_item['quantity']); 

       if($item_qty == 1 && $difference == 1){ 
        $cart_object->remove_cart_item($cart_item_key); 
        break; 
       } else { 
        if($item_qty > 1 && $difference <= $item_qty){ 
         $cart_object->set_quantity($cart_item_key, ($item_qty - $difference)); 
         break; 
        } else { 
         $cart_object->remove_cart_item($cart_item_key); 
        } 
       } 
      } 
     } 
    } 
} 

この2つの機能を使用すると、完全な解決策が得られます。

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

このコードはテスト済みであり、動作します。

+0

素晴らしいありがとう!これは "シンプルな製品"と素晴らしい作品です。あなたはそれを "変数製品"と連携させる方法を知っていますか? – user2766888

+0

確認するだけで、変数商品(つまりXXX商品がカートに追加されたか、カスタムエラーメッセージ)に対してWooCommerceの通知が正しく表示されます。しかし、私がカートを見ると、Variable製品はうまく追加されていません。 – user2766888

関連する問題