[英]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:
但这完全破坏了我的设计,无法正常工作:
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.