there’s a WooCommerce builtin shortcode to add a product to cart, by default the shortcode works only for simple product, not for variable products, as when using this shortcode you can’t define variations. You can use these two shortcodes [add_to_cart id=”XX”] and [add_to_cart_url id=”XX”] for simple products.

Create the URL

But what if you want to create a “add to cart” url for variable products? Let’s say you want to create a add to cart url for a t-shirt with size set XL?

Well, you need to add variables to the URL and create a url similar to this:

http://mysite.com/cart/?add-to-cart=PRODUCT_ID&variation_id=VARIATION_ID&attribute_pa_colour=ATTRIBUTE_SLUG

Basically, this url will lead you directly to the cart page and will add to cart product having the “product_id” ID, and the “colour” attribute set to “attribute_slug”. A real example would something like this:

http://mysite.com/cart/?add-to-cart=123&variation_id=456&attribute_pa_colour=black

How to find the product ID?

To find the product ID, you can just place your cursor over the product line or use my little plugin:

woocommerce购物车GET方法在购物车添加商品-爱程序网

How to find the variation ID?

To find the variation id you need to edit a product that has this variation enabled:

woocommerce购物车GET方法在购物车添加商品-爱程序网

How to find the correct variation name?

The attribute name must start by “attribute_pa_” and should then contain the variation slug.

woocommerce购物车GET方法在购物车添加商品-爱程序网

How to find the attribute slug?

To find the attribute slug, click on Products > Attributes > Then configure terms.

woocommerce购物车GET方法在购物车添加商品-爱程序网

Final result

Here the final result when clicking on the URL we just created:

woocommerce购物车GET方法在购物车添加商品-爱程序网