在“添加到购物车”按钮旁边添加自定义按钮

时间:2019-12-02 10:38:59

标签: wordpress button woocommerce

大家好,我试图在“添加到购物车”按钮旁边添加自定义按钮。我添加了它,但是我想更改每个产品的按钮链接,我该怎么做呢?这是我的代码:(我从另一个问题中拿出了,它可以正常工作)

add_action('woocommerce_after_shop_loop_item', 'add_a_custom_button', 5 );

function add_a_custom_button() {
   global $product;

   // Not for variable and grouped products that doesn't have an "add to cart" button
   if( $product->is_type('variable') || $product->is_type('grouped') ) return;

   // Output the custom button linked to the product
   echo '<div style="margin-bottom:10px;">
    <a class="button custom-button" href="' . esc_attr( $product->get_permalink() ) . '">' . __('View product') . '</a>
</div>';
}

2 个答案:

答案 0 :(得分:0)

将此代码段粘贴到活动子主题或主题文件夹中的function.php文件中:

function wc_shop_demo_button() {
     global $product;
     if( $product->is_type('variable') || $product->is_type('grouped') ) return;
     echo '<div style="margin-bottom:10px;">
    <a class="button custom-button" href="' . esc_attr( $product->get_permalink() ) . '">' . __('View product') . '</a>
</div>';
}
add_action( 'woocommerce_after_shop_loop_item', 'wc_shop_demo_button', 20 );
add_action( 'woocommerce_after_add_to_cart_button', 'wc_shop_demo_button', 20 );

答案 1 :(得分:0)

另一种方式:

为要在其上使用自定义网址链接的页面创建一个新的Wordpress自定义字段:

自定义字段名称:nothanks_link_redirect

自定义字段值:https://yourlink.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');

享受!