woocommerce change add to cart text and url

WooCommerce: Change Add To Cart Button Text and URL

Replace the default WooCommerce Add To Cart button with a new button with custom text and URL link with product parameters.

The code snippet below will remove the existing WooCommerce Add To Cart button and replace it with a new button that you can change the button text and have a custom URL link when a customer clicks on it.

I’ve also added some WooCommerce URL parameters which can come in handy of you are redirecting to a contact form.

You can modify the style using CSS.

https://gist.github.com/DeveloperWil/7a88086d8eea1be94dfe4f391b5201a1
replace woocommerce add to cart button
Replaced Add To Cart button with a custom button title and url
woocommerce add to cart button custom url with parameters
Button click redirects to a custom url with product parameters

URL Product Parameters

The following product parameters area added to the custom URL

wc_id = the product ID

wc_price = the product display price without currenct symbol

wc_title = the product title

wc_product_link = the URL link to the product

Where To Put The Code Snippet?

This code snippet should go in your active theme’s functions.php file.

Add it to the bottom of the file, before any closing ?> characters.

It’s always a good idea to take a backup of your functions.php file in case anything goes wrong – you can just copy the old code back in again.

Looking to level up your WordPress development skills?

WordPress Plugin Development book

I self-taught myself WordPress development, but this book took it to another level.

Each chapter builds upon the next, but you can also use it to jump into specific areas like WP Cron, WP REST endpoints, building a custom plugin repository etc.

If you’re like me and like having reference books at hand, this is the one that sits on my desk all the time.

Was this article helpful?
YesNo

8 thoughts on “WooCommerce: Change Add To Cart Button Text and URL”

  1. I’ve been searching for this solution for a few hours now, trying various code snippets to no avail. This code actually works.

    Is there a way to modify it so that when they click the button, instead of taking them to /contact in this line:

    $link = get_bloginfo( ‘url’ ) . ‘/contact’ . $product_link_params;

    Can they be routed to the appropriate product details page for the product they clicked the button from?

  2. I really like this approach to changing the add-to-cart button. My only issue is that it does not change all instances of the add-to-cart. When I implemented it, it only changed the add-to-cart button on the shop page but not in the individual product page and any where else. Would it be possible to request that write up that as well? Please and thank you.

    1. Hi Alejandro

      I’ve updated the code to remove the Add To Cart button on the shop product loop and single product pages.

      I’ve also revised the code to remove a PHP warning.

      Hope that helps.
      Wil.

      1. Thank you so much. That worked perfectly.

        I modified your code a bit to add the details of the item to the contact form directly using a single key pair.

  3. Hi Will

    a big thank you for this code : it works perfectly.

    I just have a little problem with style : i try to customize with this code
    p.zpd-wc-reserve-item-button {background-color: #006d77;} in additionnal css in my theme.

    But it doesn’t have any effect.
    What am i doing wrong ?

    1. Hi Alan – you’re welcome!

      The best thing to do is to try using your browser’s inspect element and clicking on the < a > button link to see where the background colour is being set from. Sometimes themes do use !important to force a CSS override.

      You could also give the CSS a bit more specificity by using
      [codelet]p.zpd-wc-reserve-item-button a.button.addtocartbutton{background-color:#006D77;}[/codelet]

      Lastly, try adding an [codelet]!important[/codelet] to the background colour to force a CSS override. It’s not the preferred method but it may be your only choice.
      [codelet]p.zpd-wc-reserve-item-button a.button.addtocartbutton{background-color:#006D77!important;}[/codelet]

Comments are closed.