[英]Avoid add to cart products from different product categories in WooCommerce
On WooCommerce, I have tried " Restricting cart items to be from the same product category " answer code, and it works. 在WooCommerce上,我尝试了“ 限制购物车商品来自同一产品类别 ”的回答代码,并且它可以正常工作。 But if the user adds the product from the product page, the product will be in the cart.
但是,如果用户从产品页面添加产品,则该产品将在购物车中。
Any advice or help please? 有什么建议或帮助吗?
Because woocommerce_add_to_cart_validation
hook is located on both WC_Cart
and WC_Ajax add_to_cart()
methods, so it's triggered when a product is added to cart via ajax or normally via single product pages… So the code works in all cases on add to cart event. 因为
woocommerce_add_to_cart_validation
钩子同时位于WC_Cart
和WC_Ajax add_to_cart()
方法上,所以当通过ajax或通常通过单个产品页面将产品添加到购物车时会触发它。因此,在添加到购物车事件中,代码在所有情况下均适用。
Now " Restricting cart items to be from the same product category in WooCommerce " doesn't handle parent product categories, as WordPress has_term()
conditional function doesn't handle parent terms, so parent product categories. 现在, “ 在WooCommerce中将购物车商品限制为来自同一产品类别 ”将无法处理父产品类别,因为WordPress的
has_term()
条件函数无法处理父条款,因此父产品类别也无法处理。
To make it work with parent product categories too, you will need something more elaborate: 为了使其也能与父产品类别一起使用,您将需要更详细的说明:
add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 );
function custom_checking_product_added_to_cart( $passed, $product_id, $quantity) {
// HERE your alert text message
$message = __( 'MY ALERT MESSAGE.', 'woocommerce' );
if( ! WC()->cart->is_empty() ) {
$term_ids = array(); // Initializing
// Loop through product category WP_Term objects set for the current the product
foreach( wp_get_post_terms( $product_id, 'product_cat') as $term ) {
$terms_ids[$term->term_id] = $term->term_id; // Add the term ID to the array
// Add the parent term ID to the array, if it exist
if( $term->parent > 0 )
$terms_ids[$term->parent] = $term->parent;
}
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item ){
if( ! has_product_categories( $product_id, $term_ids ) ) {
$passed = false;
wc_add_notice( $message, 'error' );
break;
}
}
}
return $passed;
}
// Custom conditional function that handle parent product categories too
function has_product_categories( $product_id, $categories ) {
// Initializing
$parent_term_ids = $categories_ids = array();
$taxonomy = 'product_cat';
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
if( is_numeric( $category ) ) {
$categories_ids[] = (int) $category;
} elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
$categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。
Code is tested and it works 代码经过测试,可以正常工作
In the first function, replace: 在第一个函数中,替换为:
if( ! has_product_categories( $cart_item['product_id'], $term_ids ) ) {
by 通过
if( has_product_categories( $cart_item['product_id'], $term_ids ) ) {
Related: Restricting cart items to be from the same product category in WooCommerce 相关: 在WooCommerce中将购物车项目限制为来自同一产品类别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.