【问题标题】:WooCommerce: Add for specific pages a custom button next to Add to Cart using WordPress Custom FieldsWooCommerce:为特定页面添加使用 WordPress 自定义字段添加到购物车旁边的自定义按钮
【发布时间】: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


【解决方案1】:
/** 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');

享受

【讨论】:

    【解决方案2】:

    请在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 );
    

    【讨论】:

      猜你喜欢
      • 2016-09-05
      • 1970-01-01
      • 2018-05-12
      • 2020-09-01
      • 2023-04-03
      • 1970-01-01
      • 2016-01-14
      • 2019-01-24
      • 2016-09-10
      相关资源
      最近更新 更多