Skip to main content

Add Product to Cart if it is Not Exist magento1

if you want to add product to cart programmatically only if the product is not exist you have to use this code,

in this code you will check if the product is not exist in the Cart Quote, then if it is not exist you will add it

$productId = 1;
$quote = Mage::getSingleton('checkout/session')->getQuote();
if (! $quote->hasProductId($productId)) {
    $cart = Mage::getSingleton('checkout/cart');
    $cart->init();
    $product = Mage::getModel('catalog/product')->load($productId);
    
    $paramater = array('product' => $productId,
    'qty' => '1'
    );
    
    $request = new Varien_Object();
    $request->setData($paramater);
    $cart->addProduct($product, $request);
    $cart->save();
}