Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to add a variation product to my cart directly through a link. I am setting the variation_id with the query string. As far as I can tell I am sending the data the exact same way as the default variation product form does.

Here's the code inside of my single-product page:

<a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>&variation_id=262" class="rounded-rect-button add-to-cart">Add to Cart</a>

Conversely, the simple product works fine. I have set prices to my product variations in the admin. Not sure why it's not as simple as sending the data through. Any help would greatly appreciated.

share|improve this question

put on hold as off-topic by Pieter Goosen, mrwweb, birgire, Johannes Pille, ialocin 2 days ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – Pieter Goosen, mrwweb, birgire, Johannes Pille, ialocin
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

up vote 7 down vote accepted

Figured this one out. I was missing a couple parameters needed to add a variable product to my cart. The missing params are the variation_id and the attribute type that the variation_id is referring to. The variation id can be found in the admin > woocomerce > products and under the variations tab next to the product variation you have created.

The attribute param is formed from the prefix attribute_ and then the sanitized attribute name. For example, my attribute is called Stock Colors, so my attribute type param is attribute_stock-colors. From what i can tell, you just set attribute_stock-colors=1 because the add to cart method just check to see if it exists.

Here's a simple example of a link that adds a product to my cart with variation id 261 of stock colors:

<a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>&variation_id=261&attribute_stock-colors=1">Add to Cart</a>

To use this in a real situation you would need to set the attribute type and variation id dynamically from the options the user picked in a drop down or something like that (unless every product has the same variation, which would basically make it a simpel product).

share|improve this answer

i currently work on a woocommerce v2.1.12 shop and it seems they chaged that behavior.

my ajax url to add items to the cart has to look like this:

?add-to-cart=[PRODUCT-ID]&variation_id=[VARIATION-ID]&attribute_[ATTRIBUTE-NAME]=[ATTRIBUTE-SLUG]&attribute_[ATTRIBUTE-NAME]=[ATTRIBUTE-SLUG]

example:

http://example.com/store/category/product/?add-to-cart=239&variation_id=240&attribute_pa_size=48-2&attribute_pa_color=gold

this adds a specific product to the cart and also sets the variation attributes in the cart. you can even set the quantity by simply adding &quantity=[VALUE]

in the end this comes in handy since all these values are set inside the submit form / select fields, you just have to look those up..

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.