Add Product in to cart programmatically. Let see code
<?php
require_once 'app/Mage.php';
Mage::app('default');
Mage::getSingleton("core/session", array("name" => "frontend"));
$productId = $_REQUEST['productId'];
$qty = $_REQUEST['qty'];
if($productId!='' && $qty!='')
{
$_product = Mage::getModel('catalog/product')->load($productId);
if ($_product->getId()):
try {
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($_product, array('qty' => $qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
//session Not working so we use
$data['responseCode']='1';
$data['msg']='Product has been added to basket';
}
catch (Exception $exception) {
$data['responseCode']='0';
$data['msg']="There is an error ".$exception->getMessage();
}
else:
$data['responseCode']='0';
$data['msg']="Product not exist";
endif;
}
else
{
$data['responseCode']='0';
$data['msg']="Please select product & quantity";
}
$data2 = json_encode($data);
echo $data2;
?>
EmoticonEmoticon