Programmatically add product to cart (by ID)

Submitted by deeperdata - 3 years ago

I used this outside of Magento but it can be used in PHTML files as well I believe.

<?php
$id = '226'; // Replace id with your product id
$qty = '1'; // Replace qty with your qty
$_product = Mage::getModel('catalog/product')->load($id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($_product, array('qty' => $qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
?>
affiliate_link
Learn PHP
comments powered by Disqus
Proudly hosted on Digital Ocean