2017-08-09 4 views
2

「これは注文を1つのカテゴリに限定する」のような同様の解決策を見つけました。私はコードを修正しようとしましたが、それは十分ではありません。Woocommerceの3社以上のベンダーからの注文を防ぐ

私の場合、各ベンダーは製品属性値によって定義されます。カートに3つ以上の異なる条件の商品が含まれている場合は、「申し訳ありませんが、一度に3つの異なる仕入先からのみ注文できます」というメッセージを設定する必要があります。

add_action('woocommerce_add_to_cart', 'three_vendors'); 
function three_vendors() { 

    if ATTRIBUTE = SELECT A VENDOR; NUMBER OF TERMS > 3 { 

    echo "Sorry! You can only order from 3 Vendors at a time.”; 

    } 
} 

真ん中のラインは私が非PHP言語を使用して空白を埋めている。

これは私が出発点として使用していますものです。

私はカートのバリエーションの量を定義する方法を探しています。属性ではできない場合は、代わりにカテゴリを使用しています。

誰でもこれを行う方法を知っていますか?

答えて

1

更新:woocommerce_add_to_cart_valisationフック

でバリエーションを管理することはできません代わりに、私たちは、ときに、より最後に追加されたカートのアイテムを削除し、woocommerce_add_to_cartフィルターフックに引っかけカスタム関数を使用することができます3つのベンダー(商品)がカートに入っています:

// Remove the cart item and display a notice when more than 3 values for "pa_vendor" attibute. 
add_action('woocommerce_add_to_cart', 'no_more_than_three_vendors', 10, 6); 

function no_more_than_three_vendors($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) { 

    // Check only when there is more than 3 cart items 
    if (WC()->cart->get_cart_contents_count() < 4) return; 

    // SET BELOW your attribute slug… always begins by "pa_" 
    $attribute_slug = 'pa_vendor'; // (like for "Color" attribute the slug is "pa_color") 

    // The current product object 
    $product = wc_get_product($variation_id); 
    // the current attribute value of the product 
    $curr_attr_value = $product->get_attribute($attribute_slug); 

    // We store that value in an indexed array (as key /value) 
    $attribute_values[ $curr_attr_value ] = $curr_attr_value; 

    //Iterating through each cart item 
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { 

     // The attribute value for the current cart item 
     $attr_value = $cart_item[ 'data' ]->get_attribute($attribute_slug); 

     // We store the values in an array: Each different value will be stored only one time 
     $attribute_values[ $attr_value ] = $attr_value; 
    } 
    // We count the "different" values stored 
    $count = count($attribute_values); 

    // if there is more than 3 different values 
    if($count > 3){ 
     // We remove last cart item 
     WC()->cart->remove_cart_item($cart_item_key); 
     // We display an error message 
     wc_clear_notices(); 
     wc_add_notice(__("Sorry, you may only order from 3 different Vendors at a time. This item has been removed", "woocommerce"), 'error'); 
    } 
} 

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

このコードはテスト済みであり、動作するはずです。あなたの特定の属性に関する属性値をチェックし、3つ以上の属性値について最後に追加されたカート項目を削除します。

唯一の厄介なことは、カートの通知に追加された古典を現時点では削除できないことです。 私は別の方法を見つけることを試みる...

+0

ありがとう!これはまさに私が探しているものです。 それは正しく見えますが、サイト上ではバグです。たとえば、バスケットが空で製品を追加しようとすると、Webサイトで要求を処理できないというエラーメッセージが表示されます。スニペットを無効にしてカートに追加すると、うまく動作します。もう一度スニペットを有効にして3番目のベンダーからアイテムを追加すると、「申し訳ありません、選択した商品と一致する商品はありません。別の組み合わせを選択してください」というポップアップメッセージが表示されます。 なぜこれを行うのですか? –

+0

ありがとう@LoicTheAztec クイックアップデート - 「Sorry no products ...」ポップアップが表示されなくなりました。私のカートが空のときは、「ウェブサイトはリクエストを処理できません」というエラーメッセージが表示されます。 PHPを無効にして製品を追加した後、コードを再アクティブ化すると、必要な数だけ商品を追加できます。何も起こりません。私はあなたが私に徹底的に送ったコードを見直しました。問題はサイトの他の場所にある可能性がありますか? –

+0

@CraigdeGouveia私はそれを行う別の方法を見つけました...私の更新された答えを見てください...ありがとう。 – LoicTheAztec

関連する問題