【问题标题】:Woocommerce - Remove add to cart button when user is the product authorWoocommerce - 当用户是产品作者时删除添加到购物车按钮
【发布时间】:2018-04-25 13:25:17
【问题描述】:

当当前用户是登录用户时,我正在尝试删除“添加到购物车”按钮并添加“编辑产品”链接。但这完全破坏了我的设计并且不起作用:

  • 仅显示 12 个产品中的 2 个
  • 它一直在第一个产品中显示“添加到购物车”按钮

    <?php
    global $current_user;
    get_currentuserinfo();
    
    if (is_user_logged_in() && $current_user->ID == $post->post_author)  {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 10 );
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    add_action( 'woocommerce_after_shop_loop_item', 'btn_edit_own_product', 10 );
    
    function btn_edit_own_product() {
        edit_post_link('Edit Product');
    }
    }
    ?>
    

有什么帮助吗? 谢谢!!

【问题讨论】:

  • 您将这段代码放在哪里?这是原样的代码还是只是其中的一部分?

标签: php wordpress woocommerce


【解决方案1】:

试试这个代码,

/* 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- hide add to cart button for product author

【讨论】:

    【解决方案2】:

    请改用此代码。把它放在你当前主题的functions.php上

    add_action( 'woocommerce_shop_loop', 'custom_woocommerce_shop_loop' );
    
    function custom_woocommerce_shop_loop() {
    
        global $post;
        $current_user = wp_get_current_user();
    
        if (is_user_logged_in() && $current_user->ID == $post->post_author)  {
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );;
            add_action( 'woocommerce_after_shop_loop_item', 'btn_edit_own_product', 10 );
    
        }
    }
    function btn_edit_own_product() {
        edit_post_link('Edit Product');
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-05
      • 2014-02-06
      • 2018-05-21
      • 1970-01-01
      • 2019-08-04
      • 2014-05-26
      • 1970-01-01
      • 2012-08-21
      • 2017-05-30
      相关资源
      最近更新 更多