how can i add variation product to cart from custom link in Woocommerce ?

To discuss the 3 major blog CMS options post here. Topics include Wordpress themes, CMS plugins, and installation and setup.
Post Reply
Posts: 5
Joined: Tue Mar 12, 2019 1:45 am

Sun Mar 31, 2019 1:15 pm


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:

Code: Select all

<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 :)

Junior Member
Posts: 18
Joined: Wed Jul 01, 2015 9:09 pm


Mon Apr 01, 2019 4:47 pm

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

Code: Select all

<a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>&variation_id=262&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 simple product).
Post Reply