简体   繁体   English

如果用户未登录 Woocommerce,请避免将特定产品类别添加到购物车

[英]Avoid add to cart for specific product categories if user is unlogged in Woocommerce

In Woocommerce, I am trying to disable specific product categories to be added to the cart for users that aren't logged in. i'm looking for a solution the last couple of days and after frustrated deleting the last code I found this but also doesn't do the job.在 Woocommerce 中,我试图为未登录的用户禁用将特定产品类别添加到购物车的功能。最近几天我正在寻找解决方案,在删除最后一个代码后感到沮丧,我发现了这个,但也不做这项工作。

I'm using a certain plugin (TZ product tabs) to show products on other pages (so not only on the category and product page (this i know how to disable))我正在使用某个插件(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 );
    }
}

Referenced from https://gist.github.com/rynaldos/560c621714b9680433cddf18e6a50305参考自https://gist.github.com/rynaldos/560c621714b9680433cddf18e6a50305

My best guess is to check the category of the product when the "add to cart" button is pressed and based on that the product can be added to the cart or not.我最好的猜测是在按下“添加到购物车”按钮时检查产品的类别,并基于该产品是否可以添加到购物车。

Thanks in advance.提前致谢。

The conditional tag is_product_category() only target product category archive pages.条件标签is_product_category()仅针对产品类别存档页面。 Instead you can use WordPress conditional function has_term() .相反,您可以使用 WordPress 条件函数has_term()

There is 2 ways to avoid specific products being added to cart for no logged user…两种方法可以避免未登录用户将特定产品添加到购物车……

1) Using Add to cart validation hook: 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;
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

在此处输入图片说明


2) Using is_purchasable product property (It will remove add to cart button) : 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;
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。


Targeting the parent product category terms too.也针对父产品类别术语。

You will use the following custom conditional function to replace has_term() Wordpress function:您将使用以下自定义条件函数来替换has_term() Wordpress 函数:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // 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;
}

Then for both hooked functions, you will replace the following line :然后对于两个挂钩函数,您将替换以下行:

if( has_terms( $terms, 'product_cat', $product_id ) && ! is_user_logged_in() ){
    

By this line:通过这一行:

if( has_product_categories( $terms, $product_id ) && ! is_user_logged_in() ){

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

woocommerce_is_purchasable filter can do the job. woocommerce_is_purchasable过滤器可以完成这项工作。 It checks if a product is purchasable or not.它检查产品是否可购买。 If not purchasable, "add-to-cart" button is removed and can't be purchased.如果不可购买,“添加到购物车”按钮将被移除且无法购买。

So, here you will need to check if user is logged in or not, and product belongs to certain category, before disabling purchase ability.因此,在禁用购买功能之前,您需要在此处检查用户是否已登录,以及产品是否属于某个类别。

This is how you can do it:你可以这样做:

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);

**This is tested, and working. **这是经过测试和工作的。

Note: By default, $is_purchasable is set to true .注意:默认情况下, $is_purchasable设置为true You will need to return false , when you want to turn off purchasing.当您想关闭购买时,您需要返回false

I'm using option #2 because the being logged in for the listed parent categories is required.我正在使用选项 #2,因为需要登录列出的父类别。 I was very careful to transfer exactly but for my site, it gives me the white screen of death.我非常小心地准确转移,但对于我的网站,它给了我死亡的白屏。 I placed this code directly in 'My Custom Functions' plugin listed under Settings>PHP Inserter.我将此代码直接放在“设置”>“PHP 插入器”下列出的“我的自定义函数”插件中。 Can anyone see the problem?任何人都可以看到问题吗?

add_filter('woocommerce_is_purchasable','conditional_purchasable_products', 20, 2);
function conditional_purchasable_products( $is_purchasable, $product) {
$terms= array( 'memberships', 'our-vendors', 'programs', 'uncategoriezed', 'unpublished-products');
    
$product_id= $product->get_id(); // The product ID

if( has_product_categories ( $categories, $product_id= 0) && ! is_user_logged_in() ){
        $is_purchasable= false;
    }
return $is_purchasable;
}

function has_product_categories ( $categories, $product_id = 0) {
    $parent_term_ids = $categories_ids = array(); 
    $product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
        $categories = (array) $categories; 
foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy) ;
        if( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }
foreach( get_the_terms( $product_id, $taxonomy) as $term){
        if( $term->parent > 0){
            $parent_term_ids[] = $term->parent;
            $parent_term_ids[] = $term->term_id; 
    } else{
            $parent_term_ids[] = $term->term_id; 
    }
}
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true: false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 避免 Woocommerce 中特定产品类别的购物车项目价格更新 - Avoid cart items price update for specific product categories in Woocommerce 隐藏产品价格并禁用 Woocommerce 中特定产品类别的添加到购物车 - Hide product price and disable add to cart for specific product categories in Woocommerce 避免在WooCommerce中添加来自不同产品类别的购物车产品 - Avoid add to cart products from different product categories in WooCommerce 为 WooCommerce 中特定产品类别的购物车项目自动添加产品 - Auto add a product for cart item from specific product categories in WooCommerce 在特定的WooCommerce产品类别上自定义“添加到购物车”按钮 - Customize Add to Cart button on specific WooCommerce product categories WooCommerce中特定产品类别的最低购物车数量 - Minimum cart amount for specific product categories in WooCommerce 替换 WooCommerce 中未登录用户的添加到购物车按钮 - Replace add to cart button for unlogged users in WooCommerce Woocommerce 如果购物车中有特定产品,则将特定产品添加到购物车 - Woocommerce add to cart a specific product if a specific product is in the cart 自动将产品添加到购物车,部分 WooCommerce 产品类别除外 - Auto add product to cart except for some WooCommerce product categories 如何将特定类别添加到 woocommerce 产品变体 - How to add specific categories to woocommerce product variations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM