在Woocommerce中,我正在尝试为未登录的用户禁用要添加到购物车中的特定产品类别。我正在寻找最近几天的解决方案,并在沮丧地删除我发现的最后一个代码之后但这也没有做。
我正在使用某个插件(TZ产品标签)在其他页面上显示产品(所以不仅在类别和产品页面上(我知道如何禁用))
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
// replace a_category and another_category with the slugs of the categories you'd like to have the button removed from
if( is_product_category( array( 'gekoelde-bier', 'bierkoerier'))) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
// add_filter( 'woocommerce_is_purchasable', false );
}
}
引用自https://gist.github.com/rynaldos/560c621714b9680433cddf18e6a50305
我最好的猜测是在按下“添加到购物车”按钮时检查产品的类别,并根据该产品是否可以添加到购物车来
。谢谢。
答案 0 :(得分:1)
条件标签is_product_category()
仅定位到产品类别存档页面。相反,您可以使用WordPress条件函数has_term()
。
有 2种方法可以避免没有登录用户的特定产品被添加到购物车 ...
1)使用添加到购物车验证挂钩:
// Avoid add to cart conditionally
add_filter( 'woocommerce_add_to_cart_validation', 'avoid_add_to_cart_conditionally', 20, 3 );
function avoid_add_to_cart_conditionally( $passed, $product_id, $quantity) {
// HERE your product categories (can be IDs, slugs or names terms)
$terms = array( 'gekoelde-bier', 'bierkoerier');
if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
// Displaying a custom notice (optional)
wc_add_notice( __('Only logged in users are allowed to purchase this item. Please register.'), 'error' );
$passed = false;
}
return $passed;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
2)使用is_purchasable
产品属性 (它将删除添加到购物车按钮):
add_filter('woocommerce_is_purchasable','conditional_purchasable_products', 20, 2);
function conditional_purchasable_products( $is_purchasable, $product ) {
// HERE your product categories (can be IDs, slugs or names terms)
$terms = array( 'gekoelde-bier', 'bierkoerier');
$product_id = $product->get_id(); // The product ID
if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
$is_purchasable = false;
}
return $is_purchasable;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
您将使用以下自定义条件函数替换has_term()
Wordpress函数:
// Custom conditional function that checks for parent product categories too
function has_product_categories( $categories, $product_id ) {
// 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;
}
然后对于这两个钩子函数,您将替换以下行:
if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
通过此行:
if( has_product_categories( $terms, $product_id ) && ! is_user_logged_in() ){
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。
答案 1 :(得分:0)
woocommerce_is_purchasable
过滤器可以完成这项工作。它检查产品是否可购买。如果无法购买,则会删除“添加到购物车”按钮,无法购买。
因此,在禁用购买功能之前,您需要在此处检查用户是否已登录以及产品属于特定类别。
这是您可以执行的操作:
function wpso9800_remove_cart_button( $is_purchasable, $product ) {
//when logged in
if ( is_user_logged_in() ) {
return $is_purchasable;
}
//get categories
$categories = get_the_terms( $product->id, 'product_cat');
$my_terms_ids = array ( 1, 2 );//product category IDs
if ($categories && !is_wp_error($categories)) {
foreach ($categories as $cat) {
if ( in_array($cat->term_id, $my_terms_ids ) ) {
return false;
}
return $is_purchasable;
}
}
}
add_filter('woocommerce_is_purchasable','wpso9800_remove_cart_button', 10, 2);
**这已经过测试,可以正常工作。
注意:默认情况下,$is_purchasable
设置为true
。要关闭购买功能,您需要返回false
。