禁用“出价”作者的WooCommerce产品页面上的按钮 [英] Disable the "bid" button from WooCommerce product pages for the author
问题描述
我想从WooCommerce的产品页面中删除/禁用/隐藏出价按钮,以便发布该帖子的作者。
我正在使用WC供应商专业版+ Woocommerce + Wp Geine拍卖+ WC供应商拍卖。
请找到屏幕截图的链接如下:
实时
这:
I would like to Removal/disable/hide of "bid" button from product pages in WooCommerce for post authors of the post.
I am using WC vendors pro + Woocommerce + Wp Geine Auctions + WC Vendors Auction.
Please find the link of the screen shot below:
The Live Link to the product
How can I do it please?
As this buttons are already customized by you or some plugins, I am not sure at 100% that it will work for you, even if it works on my test server.
The first function is a conditional function that detects for a product if the current user is the author (the vendor) of this product.
Then on shop and archives pages the add to cart button is replace by a custom button liked to the product.
To finish on single product page the button is replace by a fake button with a custom text (here "Not Allowed")…
Here is the code:
// Custom conditional function (detecting the vendor of a product)
if( ! function_exists( 'is_the_vendor' ) ){
function is_the_vendor( $product ){
$current_user_id = get_current_user_id();
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Get the product post object to get the post author
$post_obj = get_post( $product_id );
$post_author = $post_obj->post_author;
if( $post_author == $current_user_id ) return true;
else return false;
}
}
// Shop and archives pages: we replace the button add to cart by a link to the product
add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_text_replace_button', 10, 2 );
function custom_text_replace_button( $button, $product ) {
if( is_the_vendor( $product ) ){
$button_text = __("View product", "woocommerce");
return '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
} else {
return $button;
}
}
// replacing add to cart button and quantities by a custom inactive button
add_action( 'woocommerce_single_product_summary', 'replacing_template_single_add_to_cart', 1, 0 );
function replacing_template_single_add_to_cart() {
global $product;
if( is_the_vendor( $product ) ):
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// The text replacement
add_action( 'woocommerce_single_product_summary', function(){
// set below your custom text
$text = __('Not allowed', 'woocommerce');
// Temporary style CSS
$style_css = 'style="border: solid 1px red; padding: 0 6px; text-align: center;"';
// Output your custom text
echo '<a class="button custom-button" style="background-color: grey !important;">'.$text.'</a>';
}, 30 );
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works. you will get this:
And this:
这篇关于禁用“出价”作者的WooCommerce产品页面上的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!