4

I'd like to add a button next to "Add to Cart" on the product page that adds "-sample" to the product URL when clicked.

Example:

You're viewing Product 1's page and the URL is "http://www.example.com/shop/product-1/"

When you click on the button, it adds "-sample" to the URL "http://www.example.com/shop/product-1-sample/"

How can I achieve this?

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Carlos Daniel
  • 127
  • 2
  • 11

2 Answers2

5

For woocommerce 3+ (only):

In woocommerce 3 you will use woocommerce_after_shop_loop_item action hook instead, as the hook woocommerce_after_add_to_cart_button will not work anymore.

add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
    global $product;

    $product_link = $product->get_permalink();

    $sample_link = substr($product_link, 0, -1) . '-sample/';
    echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" )  . '</a>';
}

Code goes on function.php file of your active child theme (or active theme). Tested and works.


Before woocommerce 3:

This is possible using hook woocommerce_after_add_to_cart_button to add your additional button on product pages, using this custom function:

add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
    global $product;

    $product_link = get_permalink( get_the_id() );

    $sample_link = substr($product_link, 0, -1) . '-sample/';
    echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" )  . '</a>';
}

This code goes on function.php file of your active child theme or theme.

This code is tested and fully functional.


Based on this: Add a button after add to cart and redirect it to some custom link in WooCommerce

And this: PHP - How to remove all specific characters at the end of a string?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • For some reason this stopped working after the new WooCommerce update, any idea why? – Carlos Daniel Aug 25 '16 at 01:07
  • Actually, it's this code that I added to only apply the button to a certain category, when I remove this code it works again. [Here's the Code](https://docs.woocommerce.com/document/remov-product-content-based-on-category/) – Carlos Daniel Aug 25 '16 at 02:17
  • [Link to new question](https://stackoverflow.com/questions/39135772/woocommerce-display-code-only-in-certain-category) – Carlos Daniel Aug 25 '16 at 02:25
  • Is it possible to add this to the shop page instead of the product page? – Diego M. Oct 27 '20 at 21:09
  • 1
    @DiegoM. see: https://stackoverflow.com/questions/38887186/additional-button-in-woocommerce-shop-page-and-archives/38888765#38888765 – LoicTheAztec Oct 27 '20 at 21:14
0

It's been a long time since the original question, but here's a recent update that works for me, WooCommerce 6.3.1:

/* WooCommerce customization */
add_action( 'woocommerce_after_shop_loop_item', 'custom_select_link', 11 );
function custom_select_link() {
     global $product;
     // Custom "Select" button.
     echo '<a class="custom-button" href="' . esc_url( get_permalink( $product->id ) ) . '"><button class="custom-button">   </button></a>';
} 

This answer cites an answer in wordpress.stackexchange.

TARKUS
  • 2,170
  • 5
  • 34
  • 52