woocommerce-隐藏产品作者的添加到购物车按钮 [英] woocommerce- hide add to cart button for product author

查看:36
本文介绍了woocommerce-隐藏产品作者的添加到购物车按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的自定义插件(基于 woocommerce)开发的一部分,我为产品分配了作者,并愿意为产品作者隐藏添加到购物车的按钮,以便我可以限制作者购买他们自己的产品.

As a part of my custom plugin (based on woocommerce) development I have assigned authors to products and willing to hide the add to cart button for product authors, so that I can restrict authors from buying their own product.

为此,我尝试了以下代码,但我无法向作者隐藏添加到购物车按钮.

For this I have tried the below code but i am not able to hide the add to cart button from authors.

add_action('after_setup_theme','user_filter_addtocart') ;
function user_filter_addtocart(){
    $user_id = get_current_user_id();
    $author_id = get_post_field('post_author', get_the_ID());
    if(get_current_user_id() === $author_id){
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30, 3 );
    }
}

推荐答案

你可以试试这个,如果你使用的是默认商店和单一产品页面它会工作,如果你为这些页面使用自定义模板那么请确保您在自定义模板中正确使用了默认挂钩和过滤器.

You can try this, if your are using default shop and single product page it will work, and if you are using custom template for those pages then please make sure you are using default hooks and filters correctly in your custom template.

/* remove add-to-cart from shop page for product author  */
add_action('woocommerce_after_shop_loop_item_title','user_filter_addtocart_for_shop_page') ;
function user_filter_addtocart_for_shop_page(){
    $user_id = get_current_user_id();
    $author_id = get_post_field('post_author', get_the_ID());
    if($user_id == $author_id){
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    }
}

/* remove add-to-cart from single product  page for product author  */
add_action('woocommerce_before_single_product_summary','user_filter_addtocart_for_single_product_page') ;
function user_filter_addtocart_for_single_product_page(){
    $user_id = get_current_user_id();
    $author_id = get_post_field('post_author', get_the_ID());
    if($user_id == $author_id){
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }
}

这篇关于woocommerce-隐藏产品作者的添加到购物车按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆