Ask SkyVerge

This is an Ask SkyVerge question from Manuel:

How can I change the add to cart button for external products? I need to use something besides “Buy product” for my external products.


The good news here is that there’s already a setting for your WooCommerce external products to change the button text. When you edit the product, you can enter the button text while creating it, right after you enter the product link.

WooCommerce external product button

This will adjust the button on your product page to use the entered text instead:

WooCommerce external product

This button will also be adjusted on the shop page and category pages.

If you don’t set the button text manually here, the default “Buy product” text will be used instead.

WooCommerce external product button defaults

Modify Default button text

So what if you want to modify that default text instead so you don’t have to change every single button’s text while creating external products? There’s a really useful filter in place that we can use here to change this text instead.

The woocommerce_product_single_add_to_cart_text filter will let us filter the add to cart button text for our products, which is used in both archive loops and on the single product page.

However, we can’t just blindly change this button:

function sv_wc_external_product_button( $button_text ) {
    return 'Buy at Amazon';
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'sv_wc_external_product_button' );

As this would change the button for all products in the store. Instead, we want to scope this change to only external products. We can do so with a tiny bit more code to check the product type before changing the button.

/**
 * Changes the external product button's add to cart text
 *
 * @param string $button_text the button's text
 * @param \WC_Product $product
 * @return string - updated button text
 */
function sv_wc_external_product_button( $button_text, $product ) {
    
    if ( 'external' === $product->get_type() ) {
        // enter the default text for external products
        return $product->button_text ? $product->button_text : 'Buy at Amazon';
    }

    return $button_text;
}
add_filter( 'woocommerce_product_single_add_to_cart_text', 'sv_wc_external_product_button', 10, 2 );

Now we’re cooking 🙂 This snippet adds a few nice tweaks for us:

  • If the product is an external product, the default text is now “Buy at Amazon” instead of “Buy product”.
  • Other product types will use their default button text, as it’s unchanged
  • Since we check if button text is set before using the default, you can still manually set button text for some products as desired.

Now the default behavior is changed for the WooCommerce external product button text, but it can still be overridden using the product settings.

Published by Beka Rice

Beka leads product direction for SkyVerge and technical documentation. She spends a lot of time on research and interviews, but likes to write so she has an excuse to spend more time jamming out to anything from The Clash to Lady Gaga.

16 Comments

  1. […] has a quick guide to changing the default button text for WooCommerce external […]

  2. I’m having a hard time to configure the external button. I like the default layout of ShopIsle (the one you have in your example). But for some reason, all external products in the category page show as a text-only link (no black background). Any help? Thanks a lot!

  3. Hi Beka,

    This works great! Thank you for the solution. However it only works on the product page. When you go to the storefront (viewing all products on one page) it does not work on product thumbnails. It only changes the External Product Button name on the product page itself. Any ideas?

    Best,
    Alex

    • Haven’t tested this myself, but swapping out woocommerce_product_single_add_to_cart_text in favor of woocommerce_product_add_to_cart_text should do the trick 🙂

  4. Hi Beka,

    When I swapped out woocommerce_product_single_add_to_cart_text in favor of woocommerce_product_add_to_cart_text it did the trick. Now when you go to the storefront (viewing all products on one page) it indeed does work on product thumbnails and shows custom text. But as soon as I have swapped out the above on now on the product page itself it is missing the custom text.

    Hence it seems both of these lines are doing what they are suppose to but in different places. Is it possible to include both of these lines? Such as:

    add_filter( ‘woocommerce_product_add_to_cart_text’, ‘woocommerce_product_single_add_to_cart_text’, ‘sv_wc_external_product_button’, 10, 2 );

    Best,
    Alex

    • Gotcha, if you want to use both you’d need to add both filters like this:

      add_filter( 'woocommerce_product_add_to_cart_text', 'sv_wc_external_product_button', 10, 2 );
      add_filter( 'woocommerce_product_single_add_to_cart_text', 'sv_wc_external_product_button', 10, 2 );
      

      You can hook the same method into both filters. Cheers!

  5. You are a superstar Beka! Thank you very much.

  6. This works! Thanks a lot.. I was struggling with this problem all day!

    Is there a way to add an image next to the “Buy at Amazon” button? By adding some kind of custom field for an image URL in the external product tab?

  7. Hey Beka,
    Thank you so much for the helpful post. I’m trying to figure out a way to add multiple links to different external stores. Eg: Buy from Amazon, Buy from eBay, Buy from BestBuy. Is there a simple way to do this, or will it involve a lot of code? Grateful for any advice. Thanks again!

  8. It doesn’t change anything on my website at all, could this be a theme issue?

  9. Thank you for this!

    Is there any way to do the same thing for the external button url?

    Cheers
    Peter

  10. Thank you for your help

    could you please tell me, how to edit function PHP and where put this code exactly, because I want to change all my website products Button to “By At Amazon” I tried to edit External Product manually and it’s work in the product page and when I press By At Amazon, work fine and transfer to Amazon product website, But in my website main page the product changed to By At Amazon, but when I press the Button it doesn’t make any transfer to Amazon, Only reload my main website page! I tried to edit my function by FTP, But the site gets crashed!
    please help me to edit All products Button!

    Thank you

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.