为作者禁用 WooCommerce 产品页面中的 "bid" 按钮
Disable the "bid" button from WooCommerce product pages for the author
我想要 post 的 post 作者的 WooCommerce 产品页面中的 Removal/disable/hide 个 "bid" 按钮。
我正在使用 WC vendors pro + Woocommerce + Wp Geine Auctions + WC Vendors Auction。
请找到以下屏幕截图的 link:
请问我该怎么做?
由于此按钮已由您或某些插件自定义,因此我不能 100% 确定它是否适合您,即使它在我的测试服务器上有效。
第一个函数是一个条件函数,用于检测当前用户是否是该产品的作者(供应商)。
然后在商店和档案页面上,添加到购物车按钮将替换为喜欢该产品的自定义按钮。
要在单个产品页面上完成,该按钮将替换为带有自定义文本的假按钮 (此处 "Not Allowed")…
代码如下:
// 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;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且可以工作。你会得到这个:
还有这个:
我想要 post 的 post 作者的 WooCommerce 产品页面中的 Removal/disable/hide 个 "bid" 按钮。
我正在使用 WC vendors pro + Woocommerce + Wp Geine Auctions + WC Vendors Auction。
请找到以下屏幕截图的 link:
请问我该怎么做?
由于此按钮已由您或某些插件自定义,因此我不能 100% 确定它是否适合您,即使它在我的测试服务器上有效。
第一个函数是一个条件函数,用于检测当前用户是否是该产品的作者(供应商)。
然后在商店和档案页面上,添加到购物车按钮将替换为喜欢该产品的自定义按钮。
要在单个产品页面上完成,该按钮将替换为带有自定义文本的假按钮 (此处 "Not Allowed")…
代码如下:
// 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;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且可以工作。你会得到这个:
还有这个: