I'm adding product to the cart but I want to add the product ONLY if the product quantity of this product is 0. In other words, only add if this product hasn't been added before. I added this && $qty equal to 0 but that's not working.

Can someone help?

if ($item->getData('product_id') == $productId) && $qty == 0) {

     $cart     = Mage::getSingleton('checkout/cart');
     $addprod  = 121212;                          
     $qty      = 1;

     $quote  =  Mage::getSingleton('checkout/session')->getQuote()
         ->addProduct( Mage::getModel('catalog/product')
         ->load($addprod), $qty)->save();

Try this code

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

Can you please try this code to check product exist on cart or not

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$itemsProductIds = $item->getProductId();
if (!in_array($_product->getId(), $itemsProductIds) {
//Code add product to cart
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.