skip to Main Content

I’m looking for a way to add a custom button next to the add to cart button, but whereby I can enter the button link url into a product custom meta field.

I’ve created a new WordPress custom field for the certain woocommerce product pages I want to have a nearby custom url button on:

Custom field name: nothanks_link_redirect

Custom field value: https://yourcustomlink.com

UPDATE: I’ve found a suitable solution:
Tweak as you need and put this in your child theme’s 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');

2

Answers


  1. Chosen as BEST ANSWER
    /** 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');
    

    Enjoy


  2. plz try below code in functions.php file

    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 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search