我试图限制用户一次只能将一个分组产品中的商品添加到购物车。
我尝试过用两种不同的方法编写代码,但都不起作用。
这是我的两次尝试。
尝试一:
function limit_cart_items_to_one_group ( $passed, $product_id, $quantity ) {
if (current_user_can('stallholder') ) {
if( WC()->cart->is_empty() ) return $passed;
$product_type = array('terms' => array('grouped'));
$products = wc_get_products( $args );
$found = $current = false;
foreach ( $products as $product ) {
$children = array();
foreach ( $product->get_children() as $child_id ) {
$children[] = $child_id;
}
}
if( has_term( $children, 'product_cat', $product_id ) ){
$current = true;
}
foreach ( WC()->cart->get_cart() as $cart_item ){
if( has_term( $children, 'product_cat', $cart_item['product_id'] ) ) {
$found = true;
$current = true;
break; // stop the loop.
}
}
if( $found && $current ){
$passed = false;
$cats_str = implode('" and "', $children );
wc_add_notice( sprintf( __('Only one Market date is allowed to be booked at a time. Thank you.', 'woocommerce' ), $cats_str ), 'error' );
}
return $passed;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'limit_cart_items_to_one_group', 10, 3 );
尝试2:
function is_product_the_same_group($valid, $product_id, $quantity) {
if (current_user_can('stallholder') ) {
$product_type = array('terms' => array('grouped'));
$products = wc_get_products( $args );
foreach ( $products as $product ) {
$children = array();
foreach ( $product->get_children() as $child_id ) {
$children[] = $child_id;
}
}
global $woocommerce;
if($woocommerce->cart->cart_contents_count == 0){
return true;
}
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, $children );
$target_terms = get_the_terms( $product_id, $children );
foreach ($terms as $term) {
$group_ids[] = $term->term_id;
}
foreach ($target_terms as $term) {
$target_group_ids[] = $term->term_id;
}
}
$same_group = array_intersect($group_ids, $target_group_ids);
if(count($same_group) > 0) return $valid;
else {
wc_add_notice( 'This product is in another group!', 'error' );
return false;
}
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_group',10,3);
基本上,我试图将这个特定的用户角色限制为一次只能从一组产品中预订。
在 Woocommerce 中,一个产品可以属于多个类别,因此我们假设
product1
属于 category1
和 category2
。
现在,如果您尝试添加属于
product2
的 category2
,但不添加到 category1
,但 product2
也属于 category3
不属于的 product1
,那么在您的第一个步骤,术语集是 (category1, category2)
,因为product1
属于两者,在第一步之后,在第二步中添加属于 product2
和 category2
的 category3
,因此 (category1, category2)
和 (category2, category3)
的交集将是 的非空集合(category2)
因此 product2
已成功添加现在您的类别集将是 (category1, category2, category3)
并且您接受的类别集将随着您添加产品而不断增加。
那么,你如何解决你的任务呢?
一种方法是检查新产品的类别集是否与旧产品的类别集完全相同。虽然这在技术上是有意义的,但这不太可能符合您的目标。示例:有人购买了属于(鞋子、运动)类别的鞋子,但随后想要添加一些属于(鞋子、时尚)类别的时尚鞋子。如果是这种方法,那么第二个产品将不会在第一个产品之后添加到购物车,因为即使它也是鞋子,但它不是运动用品。
第二种可能的方法是仅检查第一个项目的类别,并查看新项目是否与其中任何一个匹配。因此,如果我们回到运动鞋的例子,那么由于第一个产品既是鞋子也是运动用品,您接受任何既是鞋子又是运动用品的产品,但在后续产品中添加您忽略与第一个项目至少不共享类别的产品,并忽略后续项目的类别。
最后,一个非常严格的方法是选择第一个项目的主要类别,在我们的示例中,它可能主要是鞋子,因为它比运动更具体,但在这种情况下,您可能需要优先考虑第一产品的类别相对于第一产品的另一个类别在特异性上没有明显差异。假设您选择这种方法,基本上您将能够确定第一个产品的主要类别,并在随后要添加到购物车的任何产品的情况下寻找该类别。