【问题标题】:Additional custom button for specific single product page in WooCommerceWooCommerce 中特定单个产品页面的附加自定义按钮
【发布时间】:2020-11-16 15:33:25
【问题描述】:
【问题讨论】:
标签:
php
html
wordpress
woocommerce
product
【解决方案1】:
根据additional add to cart button with fixed quantity in woocommerce single product pages的回答码,方法如下:
add_action( 'woocommerce_after_add_to_cart_button', 'additional_single_product_button', 20 );
function additional_single_product_button() {
global $product;
// Define your targeted product IDs in the array below
$targeted_product_ids = array( 37, 53 );
if( in_array( $product->get_id(), $targeted_product_ids ) ) {
$link = home_url('/contact-us/'); // <== Here set button link
$name = esc_html_( "Contact Us", "woocommerce" ); // <== Here set button name
$class = 'button alt';
$style = 'display: inline-block; margin-top: 12px;';
// Output
echo '<br><a rel="no-follow" href="'.$href.'" class="'.$class.'" style="'.$style.'">'.$name.'</a>';
}
}
代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。
其他相关答案:
【解决方案2】:
您可以使用一些第三方插件来提供在单个产品页面上添加按钮的功能。
或者您可以使用编码在单个产品文件中添加一个按钮。您可以在 WooCommerce 模板文件夹中的子主题中使用单个产品文件。
或者你也可以像这样使用钩子在商店循环中添加按钮:
add_action( 'woocommerce_after_shop_loop_item', 'new_add_to_cart_button' );
function new_add_to_cart_button() {
// Your button code.
}