How to add a simple woocommerce product directly to the cart using a URL/link.
https://www.yourdomainhere.com/cart/?add-to-cart=3044
Here 3044 is your product id in woocommerce. You can find it by going to your product list and checking the below shown place. Also /cart/ can be replaced with the page slug of your cart page. e.g /mycart/
How to add a woocommerce variable product or woocommerce subscription product directly to the cart using the URL/Link
https://www.yourdomainhere.com/cart/?add-to-cart=2573&variation_id=2417&attribute_pa_nou=zfree-trial-2
add-to-cart parameter is again your product id, please check above to know how to find this.
variation_id parameter is the id of the product variant of the woocommerce variable products. You can find it by checking the product edit screen. Check the below example.
attribute_pa_nou is the special parameter which instructs the add to cart function to add the specific variation of the product to the cart.
To create this parameter, you need to use your product attribute which you used to create the product variation.
Like the color, size etc.
- First, we need to find the attribute slug. Go to Woocommerce > Attributes from the wp-admin.
Click edit link on your attribute.
- Now suffix the product attribute slug to the attribute_pa_ part and create your parameter name: i.e: attribute_pa_nou as per this example.
- The value of this parameter is bit difficult to find, its again the slug of the value entered for the parameter at product variation creation. To find it easily, use inspect element feature on your browser as shown in below example.
- Now add the value part of the option to the end of the url and you have a fully working variable product direct link which adds the product to the cart.
Check our ecommerce development services page should you need advanced woocommerce functionality developed in to your website.
3 Responses
Thank you for this article. This works only for my first value of the attribute. The second and third value gives the following error:
Invalid value posted for length
I’ve double and triple checked, the values are correct:
skillscoach.co.za/?add-to-cart=480&variation_id=533&attribute_pa_length=one – works fine
skillscoach.co.za/?add-to-cart=480&variation_id=533&attribute_pa_length=two – error
skillscoach.co.za/?add-to-cart=480&variation_id=533&attribute_pa_length=three – error
I’ve changed the order of the variables, same thing, only “one” works.
Hey Vernita, you are using the same variation id parameter for all URLs, each must be unique , Please check the instructions in the article again. Hope this helps.
When doing this be careful with http and https or when you click add to cart 2 products will be added in the cart.