Search code examples
phpwordpresswoocommercecheckoutsubscription

WooCommerce Add to cart URL not working with a Variation Subscription


In my WordPress Project, I created a couple of variable subscription products with WooCommerce. I created an attribute called "billing_period" for each, with values of "monthly | yearly", and generated variations. I changed each variation to virtual (as it's subscription), and set billing intervals with needed prices.

Before this, I had simple subscriptions with monthly billing with one price. And I had pulled those product posts with WordPress to my page template and had put a button to buy it which redirects to URL like following: https://example.com/checkout?add-to-cart=<<product-id>>

But when I'm doing it now, it's not working (with my variable subscriptions setup). I even tried putting the following URL to the button, but this one also doesn't work (it redirects to home page, instead of checkout). https://example.com/checkout?add-to-cart<<product-id>>&billing_period=<<monthly_or_yearly>>

From the shop page or from the product page itself, there is an option to select billing period and then sign up for it, but I don't have plans using WooCommerce own pages (styles, etc.). That's why I had my own page for that and would like to solve this issue.


Solution

  • Replace Product ID in "add-to-cart" parameter with Variation ID.

    I just replaced the Subscription Product ID in add-to-cart parameter with Variation ID and it worked.

    https://example.com/checkout?add-to-cart=<<variation-id>>
    

    Tip: You can get the variation id by exporting the products, and getting the id for the needed variation.