WooCommerce:为特定页面添加使用WordPress自定义字段添加到购物车旁边的自定义按钮

问题描述 投票:0回答:2

这对自定义链接重定向非常有用。

为要在其上使用自定义URL链接的某些woocommerce产品页面创建新的Wordpress自定义字段:

自定义字段名称:nothanks_link_redirect

自定义字段值:https://yourlinkforthispage.com

根据需要进行调整,并将其放入子主题的functions.php中:

php wordpress woocommerce custom-fields
2个回答
0
投票
/** 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');

享受


0
投票

请尝试在functions.php文件中的以下代码

function nothanks_link_redirect() {
 echo '<a class="button nothanks_link_redirect" style="padding-right: 0.75em;padding-left: 0.75em; margin-left: 8px; " href="https://yourlinkforthispage.com" target="_blank">no thanks</a>';
  }
add_action( 'woocommerce_after_shop_loop_item', 'nothanks_link_redirect', 20 );
add_action( 'woocommerce_after_add_to_cart_button', 'nothanks_link_redirect', 20 );
© www.soinside.com 2019 - 2024. All rights reserved.