【发布时间】:2020-04-06 07:38:24
【问题描述】:
我正在寻找一种在添加到购物车按钮旁边添加自定义按钮的方法,但我可以将按钮链接 url 输入到产品自定义元字段中。
我为某些 woocommerce 产品页面创建了一个新的 Wordpress 自定义字段,我希望在附近有一个自定义 url 按钮:
自定义字段名称:notthanks_link_redirect
自定义字段值:https://yourcustomlink.com
更新:我找到了合适的解决方案: 根据需要进行调整并将其放入子主题的 functions.php 中:
/** WooCommerce custom field - 'No Thanks' Button **/
function nothanks_redirect_button() {
global $post;
$product_id = $post->ID;
$NoThanksLinkRedirectValue = get_post_meta($product_id,'nothanks_link_redirect',true);
if(!$NoThanksLinkRedirectValue) return;
echo '<a class="nothanks-button" style="margin-left: 20px" href="'.$NoThanksLinkRedirectValue.'" target="_self">No Thanks</a>';
}
add_action('woocommerce_after_add_to_cart_button','nothanks_redirect_button');
【问题讨论】:
-
你有什么问题?
标签: php wordpress woocommerce custom-fields