简体   繁体   English

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

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

I'm trying to remove the Add to Cart button when Current user is the logged in user and add the Edit Product link. 我试图在当前用户是登录用户时删除“添加到购物车”按钮,并添加“编辑产品”链接。 But this is totally breaking my design and not working: 但这完全破坏了我的设计,无法正常工作:

  • Showing only 2 of 12 products 仅显示12个产品中的2个
  • It keeps showing the Add to Cart button in the first product 它在第一个产品中一直显示“添加到购物车”按钮

     <?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'); } } ?> 

Any help? 有什么帮助吗? Thanks!! 谢谢!!

Try this code, 试试这个代码,

/* 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 );
    }
}

Hope this will helps you. 希望这对您有帮助。

For more details visit, 有关更多详细信息,请访问

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

Please try this code instead. 请改用此代码。 Place this on your current theme's functions.php 将此放置在当前主题的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.

相关问题 当产品在 Woocommerce 的购物车中时,更改添加到购物车按钮样式 - Change add to cart button style when product is in cart in Woocommerce 删除 Woocommerce 中特定产品类别的添加购物车按钮 - Remove add cart button in Woocommerce for a specific product category Woocommerce 滑动购物车 - 为每个产品添加删除按钮 - Woocommerce sliding cart - add remove button for each product woocommerce-删除“添加到购物车”按钮 - woocommerce - Remove “Add to cart” button 在WooCommerce中隐藏产品可见性时,隐藏“添加到购物车”按钮 - Hide add to cart button when product visibility is hidden in WooCommerce Woocommerce - 当产品处于缺货状态时禁用 add_to_cart 按钮 - Woocommerce - disable add_to_cart button when product is on backorder 删除 WooCommerce 中特定用户角色的添加到购物车按钮 - Remove add to cart button for a specific user role in WooCommerce 自动添加或删除 Woocommerce 购物车中的免费产品,但购物车上没有订阅产品 - Add or remove automatically a free product in Woocommerce cart but not with subscription product on the cart 如果产品在购物车中,则更改“添加到购物车”按钮文本,并在 WooCommerce 中满足条件时重定向到购物车页面 - Change "add to cart" button text if product is in cart and redirect to cart page when condition is met in WooCommerce 在 Woocommerce 购物车中自动添加或删除免费产品 - Add or remove automatically a free product in Woocommerce cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM