douwen3127 2015-12-31 23:27
浏览 56
已采纳

woocommerce- hide添加到购物车按钮的产品作者

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 );
    }
}
  • 写回答

1条回答 默认 最新

  • dougang1965 2016-01-01 03:03
    关注

    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 );
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来