0
ユーザーが特定のカテゴリの複数の商品をカートに追加できないようにしようとしています。私はその機能を見つけました:WooCommerce:カートのカテゴリごとに1つの製品
function cart_has_licence_in_it()
{
//Check to see if user has product in cart
global $woocommerce;
//assigns a default negative value
$product_in_cart = false;
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
$terms = get_the_terms($_product->id, 'product_cat');
if ($terms) {
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if ($_categoryid === 23) {
$product_in_cart = true;
}
}
}
}
return $product_in_cart;
}
私はこのためにカテゴリID番号を23に変更しました。私にはない、
add_filter('woocommerce_add_cart_item_data', 'woo_custom_add_to_cart');
function woo_custom_add_to_cart($cart_item_data) {
global $woocommerce;
$categorie = get_the_terms($cart_item_data->id, 'product_cat');
$_lacat = $categorie->term_id;
if (($_lacat===23)&&(cart_has_licence_in_it())) {
wc_add_notice('You cannot add this license to your cart because there is already another license in it. Please remove the other license from your cart first.', 'error');
$woocommerce->cart->remove_cart_item($cart_item_data->id);
}
else return $cart_item_data;
}
しかし、それは動作しません: 今私はカートがすでにそのカテゴリからアイテムを持っているかどうかを確認したい、そうであれば、メッセージをカートからアイテムを削除して表示しますメッセージを受け取ることさえできます。
WordPressとPHPを一般的にコーディングするのは初めてですが、私のコードには間違いがたくさんあります。
あなただけの最後の' if'文の行を変更する必要が(彼らがアイテムにthemelvesを削除持っているユーザーに警告)$のwoocommerce - >カート - > remove_cart_item($ cart_item_key); $ message = sprintf( 'そのカテゴリから2つのアイテムを注文することはできません;最初にカートから%sを取り除いてください。'、$ _ product-> get_title()); ' –