Woocommerce 根据产品类别单独销售功能
Woocommerce sold individually feature based on product categories
我有一个 woocommerce 网站,我使用助推器插件为每个产品单独销售,但现在我想为每个类别单独销售,如果有人将产品添加到某个类别的购物车,则无法添加其他产品将该类别加入购物车
你可以试试这个
function is_product_the_same_cat($valid, $product_id, $quantity) {
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, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$cat_ids[] = $term->term_id;
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $term->term_id;
}
}
$same_cat = array_intersect($cat_ids, $target_cat_ids);
if(count($same_cat) > 0) return $valid;
else {
wc_add_notice( 'This product is in another category!', 'error' );
return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
我从
找到的
在下面的代码中有 2 个函数:
- 第一个是检查购物车商品类别的条件函数
- 第二个将避免在购物车项目中已存在产品类别时将其添加到购物车,并将显示自定义消息。
代码:
// Conditional function: Checking product categories in cart items
function check_cats_in_cart( $product_id ) {
$taxonomy = 'product_cat';
$has_term = true;
foreach( WC()->cart->get_cart() as $item ){
foreach( wp_get_post_terms( $item['product_id'], $taxonomy ) as $term ){
// Allowing add to cart the same product
if( has_term( $term->slug, $taxonomy, $product_id ) ){
$has_term = false;
break; // stops the 2nd loop
}
}
// Allowing the same product to be added (not activated. you can uncomment lines below)
# if ( $item['product_id'] == $product_id )
# $has_term = true;
if( $has_term )
break; // stops the 1st loop
}
return $has_term;
}
// Avoid add to cart when a product category already exist in cart items, displaying a custom error message
add_filter( 'woocommerce_add_to_cart_validation', 'sold_individually_by_cats_valid', 10, 3 );
function sold_individually_by_cats_valid( $passed, $product_id, $quantity) {
$passed = check_cats_in_cart( $product_id );
if( ! $passed ){
// Displaying a custom message
$message = __("This product category is already in cart. Try another product", "woocommerce");
wc_add_notice( $message, 'error' );
}
return $passed;
}
代码进入活动子主题(或活动主题)的 function.php 文件。
已测试并有效。
If you want to allow adding to cart again the same product (that increases only the quantity of an existing product), you will uncomment this code in the function:
if ( $item['product_id'] == $product_id )
$has_term = true;
我有一个 woocommerce 网站,我使用助推器插件为每个产品单独销售,但现在我想为每个类别单独销售,如果有人将产品添加到某个类别的购物车,则无法添加其他产品将该类别加入购物车
你可以试试这个
function is_product_the_same_cat($valid, $product_id, $quantity) {
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, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$cat_ids[] = $term->term_id;
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $term->term_id;
}
}
$same_cat = array_intersect($cat_ids, $target_cat_ids);
if(count($same_cat) > 0) return $valid;
else {
wc_add_notice( 'This product is in another category!', 'error' );
return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
我从
在下面的代码中有 2 个函数:
- 第一个是检查购物车商品类别的条件函数
- 第二个将避免在购物车项目中已存在产品类别时将其添加到购物车,并将显示自定义消息。
代码:
// Conditional function: Checking product categories in cart items
function check_cats_in_cart( $product_id ) {
$taxonomy = 'product_cat';
$has_term = true;
foreach( WC()->cart->get_cart() as $item ){
foreach( wp_get_post_terms( $item['product_id'], $taxonomy ) as $term ){
// Allowing add to cart the same product
if( has_term( $term->slug, $taxonomy, $product_id ) ){
$has_term = false;
break; // stops the 2nd loop
}
}
// Allowing the same product to be added (not activated. you can uncomment lines below)
# if ( $item['product_id'] == $product_id )
# $has_term = true;
if( $has_term )
break; // stops the 1st loop
}
return $has_term;
}
// Avoid add to cart when a product category already exist in cart items, displaying a custom error message
add_filter( 'woocommerce_add_to_cart_validation', 'sold_individually_by_cats_valid', 10, 3 );
function sold_individually_by_cats_valid( $passed, $product_id, $quantity) {
$passed = check_cats_in_cart( $product_id );
if( ! $passed ){
// Displaying a custom message
$message = __("This product category is already in cart. Try another product", "woocommerce");
wc_add_notice( $message, 'error' );
}
return $passed;
}
代码进入活动子主题(或活动主题)的 function.php 文件。
已测试并有效。
If you want to allow adding to cart again the same product (that increases only the quantity of an existing product), you will uncomment this code in the function:
if ( $item['product_id'] == $product_id )
$has_term = true;