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

Avoid add to cart for specific product categories if user is unlogged in Woocommerce(如果用户未登录 Woocommerce,请避免将特定产品类别添加到购物车)
本文介绍了如果用户未登录 Woocommerce,请避免将特定产品类别添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 Woocommerce 中,我正在尝试为未登录的用户禁用要添加到购物车的特定产品类别.最近几天我正在寻找解决方案,但在删除我找到的最后一个代码后感到沮丧这样做但也不起作用.

我正在使用某个插件(TZ 产品标签)在其他页面上显示产品(所以不仅在类别和产品页面上(我知道如何禁用))

add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );函数 remove_add_to_cart_buttons() {//将 a_category 和 another_category 替换为您希望删除按钮的类别的 slug如果(is_product_category(数组('gekoelde-bier','bierkoerier'))){remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');//add_filter( 'woocommerce_is_purchasable', false );}}

引用自


2) 使用 is_purchasable 产品属性 (它将删除添加到购物车按钮):

add_filter('woocommerce_is_purchasable','conditional_purchasable_products', 20, 2);功能conditional_purchasable_products($is_purchasable,$product){//这里是您的产品类别(可以是 ID、slug 或名称术语)$terms = array('gekoelde-bier', 'bierkoerier');$product_id = $product->get_id();//产品编号if( has_terms( $terms, 'product_cat', $product_id ) && !is_user_logged_in() ){$is_purchasable = false;}返回 $is_purchasable;}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.


也定位到父产品类别字词.

您将使用以下自定义条件函数来替换 has_term() WordPress 功能:

//也处理父产品类别的自定义条件函数函数 has_product_categories( $categories, $product_id = 0 ) {$parent_term_ids = $categories_ids = array();//初始化$taxonomy = 'product_cat';$product_id = $product_id == 0 ?get_the_id() : $product_id;if( is_string( $categories ) ) {$categories = (array) $categories;//将字符串转换为数组}//将类别术语名称和 slug 转换为类别术语 IDforeach ( $categories 作为 $category ){$result = (array) term_exists( $category, $taxonomy );如果(!空($结果)){$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;//(和孩子)} 别的 {$parent_term_ids[] = $term->term_id;//这是主要类别术语,我们设置了它.}}返回 array_intersect( $categories_ids, array_unique($parent_term_ids) ) ?真假;}

然后对于两个挂钩函数,您将替换以下行:

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

通过这一行:

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

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

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.

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

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

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. Instead you can use WordPress conditional function 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:

// 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). Tested and works.


2) Using is_purchasable product property (It will remove add to cart button):

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). Tested and works.


Targeting the parent product category terms too.

You will use the following custom conditional function to replace has_term() Wordpress function:

// 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). Tested and works.

这篇关于如果用户未登录 Woocommerce,请避免将特定产品类别添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)