繁体   English   中英

Woocommerce-当用户是产品作者时,删除添加到购物车按钮

[英]Woocommerce - Remove add to cart button when user is the product author

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

  • 仅显示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'); } } ?> 

有什么帮助吗? 谢谢!!

试试这个代码,

/* 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-隐藏产品作者的添加到购物车按钮

请改用此代码。 将此放置在当前主题的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');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM