Add WooCommerce Product to Cart From URL Using Products SKU

I am working on a personal project for Automattic, and in this project I needed to tweak the way WooCommerce adds products to cart.

Basically, when you add a product to cart, you probably noticed it if Ajax isn’t enabled in your shop, your are redirected to page having the following URL structure:

http://mysite.com/?add-to-cart=XX

Where XX is the product added to cart ID. I wanted to be able to use the SKU in the URL instead of the product ID, in other words I wanted this:

http://mysite.com/?add-to-cart=SKU

So, I wrote this plugin:

Now, I can type, let’s say http://mysite.com/?add-to-cart=5678 and the product having the SKU equal to 5678 will be added to cart. If no product has this SKU, then the product having 5678 as its ID will be added to cart.

Please note: due to a security check within WooCommerce, that method will work only with SKU containing numbers only. Letters aren’t accepted.

About the author

Remi Hey! My name is Rémi Corson, I'm a French WordPress Developer, I code stuff and I write from time to time ;-) Buy a coffee
Show your support

Related Posts

1 comment

  1. greg:

    Hi,
    in the same idea, is it possible to link a variation to an other Url (product)?
    Thanks in advance

    best Regards

Leave a Reply