I am trying to work out whether it might be possible to put a straightforward HTML URL link on a button in order to add a product to a woocommerce cart. I am using a wordpress theme to show digital downloadable products but the theme itself has no cart and checkout facility so I am trying to use woocommerce for this.
I have a button for each of my products which can contain a URL to link directly to the downloadable file but when clicked I want it instead to add the product to the woocommerce cart.
There appears to be a way to do this using a shortcode but the theme is stripping out the square brackets to I need a full URL instead.
To create a custom Add to Cart URL for that product, you will need to add ? add-to-cart=123 replacing 123 with the ID of your product. The result will be something like https://www.yourwebsite.com/?add-to-cart=123 . Whoever clicks on this link, will add the product 123 to their cart.
Similar to the previous term we mentioned, you need to navigate to WooCommerce > Settings > Products > General to redirect to any page. Then, enable the AJAX “Add to Cart” button and disable “Redirect to cart page after successful addition” at the same time.
Cart URLs are easily created from the WooCommerce Settings page. Simply enable Cart URLs, add a new Cart URL with the products you've previously determined. Save and share the cart link. The option to give each Cart URL a unique name and pretty permalink, (visible to the customer) is available.
You cannot link directly to the downloadable file as it is stored in a protected directory, if you simply want to have a URL that adds a specific Product to your Cart and open the Cart/Checkout page then this will help.
http://yourproducturl.com/checkout/?add-to-cart=%ID%
http://yourproducturl.com/cart/?add-to-cart=%ID%
Replace %ID%
with the Post ID of the specific Product so that when the link is clicked, the visitor will be redirected to the Cart/Checkout page with the above Product added to their Cart.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With