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 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料