Magento – Constructing add-to-cart url from CMS page

cmsproductshopping-cart

So, I've got a CMS page that I'm using as a landing page. I want to add a button that lets customers add a product straight to the shopping cart, completely bypassing the main product page. The usual php methods for making an add-to-cart product link don't work from a CMS page. Is there some way to call these via the CMS interface with the replacement syntax {{foo}}? If not, how do I get the link I need?

Relevant info:

We are using the obfuscated add-to-cart format, so the normal query url doesn't seem to work.

Our product links and ids don't seem to be stable, so I can't just copy-paste something.

Yes, this landing page needs to be a CMS page, not hardcoded in the site.

Best Answer

I would just use the {{block}} directive to call your own .phtml file. You could do something such as:

{{block type="core/template" template="your_template.phtml" product="52"}}

Remember, any additional directive you set will get assigned to that block, so in this case, product="52" will basically do setProduct('52') on the block, allowing you to getProduct(). Alternatively, you could set a SKU or any other type of ID you would like to use.

Now, inside your template file, simply use:

<?php $product = Mage::getModel('catalog/product')->load($this->getProduct()) ?>
<?php echo Mage::helper('checkout/cart')->getAddUrl($product) ?>

It's important to use the helper method, as it will add the form key to the URL for you.

As another alternative, you could set up a widget to do this; though, personally I prefer using the {{block}} directive.