Posted on

Creating a custom “Add to Cart” URL

Adding products to the cart is the first step to convert a user to a customer.
In WooCommerce you can add products to the cart from their details page or from the shop page, for some kind of products, like simple products in example.

But sometimes this is not enough. It would be a lot better to have a landing page featuring the awesome product you just created.
In this case you could use the shortcode [add_to_cart].

But you can also create your own button by using a custom Add to Cart URL. It could be particularly useful when you have a visual builder or a pricing table plugin.

In those cases the Add to Cart shortcode may not work properly, may have styling issues because it could be not compatible with the plugin you are using.

In those cases, you will need to create your own Add to Cart URL.
Doing so is easy as drinking a glass of water.

To begin, open your dashboard and go to Products.
In the products’ list, hover over the product you want to create a custom Add to Cart URL for. On the left, you will notice a text like ID: 123 where 123 is the ID of that product.

To create a custom Add to Cart URL for that product, you will need to add ?add-to-cart=123 replacing 123 with the ID of your product.
The result will be something like https://www.yourwebsite.com/?add-to-cart=123. Whoever clicks on this link, will add the product 123 to their cart.

What is awesome about this?

That you can use it anywhere. You could create a pricing table with different tiers for your product and people will be able to skip the product page and add it directly from the pricing table on your landing page which is already properly configured for a Google Ads campaign and well indexed on Google and other search engines.

Can I create a URL for a variable product too?

Of course. That needs more steps though since you need to create the URL including the specific variation you want to add to the cart.
The easiest way to create an Add to Cart URL for a variable product is by going to the variable product page and choosing the options that you want to use in your URL. Then add the product to the cart and visit the cart page.

In example, I have a T-Shirt for which the customer can choose the size and the color. I want to feature the black small T-shirt. So I’ll go to the product page and will choose Small for the size and Black for the color and then I’ll click the button Add to Cart.

By visiting the cart I’ll have this:

product-in-the-cart

Click on the product name with the right mouse button and click on Copy Link Address.
You will have something like this: https://www.yourwebsite.com/product/product-name/?attribute_size=Small&attribute_color=Black

Copy the part after the ? in the URL: attribute_size=Small&attribute_color=Black
Then start building your URL exactly like you did before, adding your product ID and the ?add-to-cart=123 in the URL. After this last part, you need the variation ID to specify exactly what the customer will add to the cart. To get the variation ID, open the product admin page, and click on the tab Variations. In the variations list, you will see their ID on the left, in this example you will see a variation like this in your admin:

variation-id

117 is the variation ID. So add &variation_id=117 in the URL.
You are not ready yet. After the variation ID you need to add the part of the URL I asked you to copy before from the cart page.
The complete URL will look like: https://www.yourdomain.com/?add-to-cart=123&variation_id=117&attribute_size=Small&attribute_color=Black

Now use this URL on your landing page, on your pricing table, or in a button, wherever you want.
By clicking on it, your customers will add a Small Black T-shirt to their cart.

64 thoughts on “Creating a custom “Add to Cart” URL

  1. […] Mustone published a guide to creating WooCommerce add to cart links so that you can allow customers to add products from anywhere on your […]

  2. Awesome. Works good except for one problem, when I add my product via the link, it adds two instead of one. Any idea why?

    1. Maybe it happens for the same reason explained by @Pete below, or you are using a plugin to change the default minimum quantity added to the cart.

      The URL does not specify any quantity, so it’s coming from something else or simply you refresh/update the cart page.

  3. The only problem I find is if the customer refreshes the cart page it adds another one because the url is still present. It also adds another item if they click the “Update Cart” button. Any work around for this?

    1. Hi Pete,
      not something I can think about at the moment. You could check if the cart page URL already includes those GET variables, but then if you use again the URL intentionally, it won’t add the products to the cart.

      1. This may be late but might helped anyone…

        you can fixed this with woocommerce hook woocommerce_add_to_cart and wp function wp_safe_redirect. And esc_url( remove_query_arg( 'add-to-cart' ) ); would be helpful in the process.

        1. Reigel,
          Thanks for sharing your solution!

        2. Hello

          Here is my declaration:

          http://cbdpro.co/?add-to-cart=87&variation_id=276&?attribute_quantity=1+Bottle

          It does not seem to add anything to the cart.

        3. Hi Jason,
          There’s an extra ? before the attribute_quantity.

          Use this instead: http://cbdpro.co/?add-to-cart=87&variation_id=276&attribute_quantity=1%20Bottle

        4. This seemed to fix it for me.

          add_action(‘add_to_cart_redirect’, ‘resolve_dupes_add_to_cart_redirect’);
          function resolve_dupes_add_to_cart_redirect($url = false) {
          if(!empty($url)) { return $url; }
          return ‘https://XXXXX.XX’.add_query_arg(array(), remove_query_arg(‘add-to-cart’));
          }

        5. Where do you add these?

        6. @Samuel,
          That is added in functions.php.

  4. Hi, Is it possible to create a custom button that adds the product to cart, but does not leave the page?

    1. Hi Alexander,
      It is possible, but you need to write your own custom JavaScript code to handle it.

      I’d recommend contacting one of the following services if you need any help in adding this feature:

      1. Hey, I know this is late but Alexander, just add the the page to you want the user to go to before ?add-to-cart=123.

        So say you want the user to go straight to the cart from the homepage after they add your product to the cart. Change example.com?add-to-cart=123 to example.com/cart?add-to-cart=123

        this will add the product to their cart and take them to your desired location

        1. @This WordPress Guy

          You strike me as a very thoughtful person. Despite much time has passed since the question first came up, you still courteously took the time to inform the rest of us. Thank you & Bless you.

        2. Hi This WordPress Guy,
          Thanks for the great tip!
          Unfortunately the product is being added twice if I use this type of link: example.com/cart?add-to-cart=123.
          Do you have any ideas what could be causing this? I also tried example.com/cart/?add-to-cart=123.
          Cheers, Clay

        3. Hello,

          That thing is supposed to be making huge conversion rates with a simple thought, but i have this quantity of 2 thing as well.
          Anyone with a solution? This is really important 🙂

          Thank you

  5. How can you add a quantity of (2) to this Url?

  6. Hi, thanks for this great post. I have used it to solve a problem with single products.
    But I can’t get the URL for variable products, may be because of the WooCommerce 2.6 update?
    First, the cart page doesn’t show any product URL, even with mouse right click.
    Althouhg, I’ve constructed the path step by step, but WC continues saying “my attribute” is a required field.
    Any ideas on this? Thanks!

    1. Hi Daniel,
      Sorry for the late reply.

      I tested this on my dev set up with WooCommerce 2.6.1 and it still works properly.

      Try to disable the theme and test on a different one please!

  7. Hi Nicola,
    I used your solution a few months ago to add 1 item to a one page checkout page with this custom URL https://www.yourwebsite.com/checkout/?add-to-cart=123 worked like a charm. After the Woocommerce update it doesn’t work anymore. It now produces the https://www.yourwebsite.com/checkout/ page with 0 (zero) products. Is there a fix?

  8. Solved Nicola. The culprit seems to be the One Page Checkout plugin, not your magic url.

    1. Hey Hans,
      Happy you fixed it!

  9. Pretty section of content. I just stumbled upon your website and in accession capital to assert that I get actually enjoyed
    account your blog posts. Any way I will be subscribing to your augment
    and even I achievement you access consistently rapidly.

    1. Hi Lloyd,
      You can subscriber to the newsletter by filling in the form on the right on any page of this website.

      Then you will receive the posts directly via email.

  10. […] can create a custom add to cart link for any product to use this […]

  11. How do you specify quantity in the URL?

    1. You can pass the argument quantity=N in the URL, where N is the quantity you want to add.

  12. This works perfectly, do you know if there’s a way to add multiple items to the cart using this link structure i.e. ?add-to-cart=123&321 or something similar?

    1. Hi Simon,
      Yes, you can use ?add-to-cart=123&add-to-cart=321.

      Please note that it does not work with variable products or quantity. If you want to specify the quantity for each product, that won’t be possible.

  13. Thank you Nicola. This was just what I was looking for!
    Gave me the perfect workaround for a problem that was beyond my control to fix. Just in time for my book launch.

    Gratitude!

    Clare Josa
    Author of Dare To Dream Bigger

    1. Hi Clara,
      I’m happy it helped!

      Good luck with your book!

  14. Hello! Thank you for this post. I have done as described in the post, but when I click the button, it adds the product to the cart, but then sends the person to my homepage instead of to the cart. How would I go about getting them sent to the cart page? Thanks!

    1. Hey Ryan,
      Make sure to add the query parameters to your cart page URL.

      In example: https://domain.com/cart/?add-to-cart=123

      1. Hi Nicola, really helpful article – thanks!

        If I use the URL in this format – https://domain.com/?add-to-cart=123 – it adds 1 item to the cart and takes me to my homepage.

        With this format – https://domain.com/cart/?add-to-cart=123 – it does indeed take me to the cart but seems to add qty of 2. Do you know why this would be?

        Cheers
        Andy

        1. Hi Andy,
          It’s because you are triggering it on the cart page which adds products to the cart if the query add-to-cart is in the URL.

          So it first adds it because of the cart, then it adds it again because of the string.

  15. Hi, this dont work for me: ?add-to-cart=123&add-to-cart=321
    It only add the last one item.
    any idea how to add multiple items to the cart with only one URL?

    Thank you!

  16. I was suggested this website via my cousin. I’m now not certain whether or not this put up is written by means of
    him as nobody else recognize such exact approximately my
    difficulty. You’re wonderful! Thank you!

  17. This looks very useful.

    I see you have said you can specify N for product quantity. Is there a way of having it so tha the customer can enter a custom quantity?

  18. Hi
    I try to make link like http://mysite.com/?add-to-card=123
    but when I click on the link. It show me the my home page and didn’t add the product into the card
    please help me

    1. Hi,
      You are using add-to-card instead of add-to-cart.

  19. Ciao Nicola io ho un problema che si verifica solo su smartphone. Ho creato questo link che su desktop funziona mentre se si clicca da smartphone mi da errore che non è possibile aggiungere il prodotto. Come è possibile? Questo il link: [link removed by Nicola].

    Grazie se vorrai aiutarmi

    1. Ciao Paolo, Ho appena provato e a me funziona perfettamente da iPhone.

  20. Thanks, works perfectly!

    1. You’re welcome Marcos!

  21. Was there an answer given how to fix a product being added twice? I disabled all plugins except WordPress and used Twenty Sixteen as the theme, but it still continued to add two.

    1. Daniel above proposed this solution:

      add_action(‘add_to_cart_redirect’, ‘resolve_dupes_add_to_cart_redirect’);
      function resolve_dupes_add_to_cart_redirect($url = false) {
        if(!empty($url)) { return $url; }
        return ‘https://XXXXX.XX’.add_query_arg(array(), remove_query_arg(‘add-to-cart’));
      }
      
      1. Hi! Where to put this code? Thank you!!

        1. In the file functions.php in wp-content/themes/your-child-theme-name/.

  22. get_checkout_url() is deprecated as of 2.5.0 in favor to wc_get_checkout_url()

  23. Thank you guys.

  24. Hi Nicola,

    thank you for putting this together – at least I found a thread where there’s some life… 🙂 It seems to me that by adding through this method the response from the server is the whole page, which makes it a bit slow. Especially if we start putting more products into the cart continuously. I was wondering why it took so long and by checking the Response section under the Network tab under Dev tools I found that it somehow queries and loads the page again in the background… Maybe it’s because I’m using jQuery ajax method to trigger this?

    Regards,
    János

    1. Hi Janos,
      It’s normal that it loads the whole page. We are simply adding a GET variable to the URL. You can technically not load the whole page, but you’ll have to use a completely different code.

      I’d suggest you to look at how WooCommerce adds products to the cart via AJAX, and use that code to start with your own to add them to the cart via URL.

  25. Hi,
    In case this is helpful to anyone. I had the problem of the ‘add to cart’ adding 2 every time. I discovered I actually had 2 ‘Cart’ pages. This happened when importing a theme’s demo site. I unpublished one, moved to trash, and that seemed to fix my problem. I now use this format to go to the cart page once an item has been added:

    http://yoursite.com/cart/?add-to-cart=33&quantity=1

    1. Thanks for sharing Akaleo!

  26. Hi,

    This is perfect, thank you. I have only one issue;
    When I add a product to the cart like http://maltacode.com/cart/?add-to-cart=25809 it takes me to the cart, what I want.
    But it displays a box on the top of the cart-page “Basic Webhosting” has been added to your cart. View cart button”. Can I get rid of this, as the user is already on the cart page?

  27. Hi. I tried the code but its not working at all. Zero Product at the Cart.
    I am using this url: http://www.kelko.hu/?add-to-cart=29546

    1. Hi Zoltan,
      Are you sure that the ID is correct? If I visit yoursite.hu/?p=29546 I don’t land on a product page, as expected.

  28. This is very cool, however, Is there a way to make make the customer get directed to the cart in the same step? In other words, it adds the item to the cart AND takes them to the cart too. Is that possible?

  29. Hi Nicola,
    is possible send the price product to two different language woocommerce checkout pages?
    For example: one product to the italian checkout page and one product to the english checkout page?
    The translation plugin is WPML.

    1. Hi Luca,
      I guess it is possible, but I’m not 100% sure since I don’t know WPML that much. I’d suggest you to ask them.

Leave a Reply

Your email address will not be published. Required fields are marked *