5

This question already has an answer here:

When Adding a product to cart programatically ,the price of the product becomes zero ($0.00) on the fist time , but on adding the same item to the cart, it shows the price .

The product adding is handled by Event observer method on the event "checkout_cart_add_product_complete" using the below code.

$cart = Mage::getModel('checkout/cart');            
$cart->init();
$product_id=1387;
$product = Mage::getModel('catalog/product')->load($product_id);    
$params = array(
    'product' => $product_id,
    'qty' =>1
    );
$cart->addProduct($product,$params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

enter image description here

Please help me to solve this problem.

But when i go to cart and return to the product page the price shown or when i delete a product in the cart the price shown.

marked as duplicate by Raphael at Digital Pianism, Keyul Shah, Amit Bera, Rajeev K Tomy, brentwpeterson Aug 9 '16 at 14:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0
$params = array( 'product' => $product_id, 'qty' =>1 )

You are programmatically adding it to cart but not specifying the price.

I believe in Magento's structure because you're adding it for the first time it's a custom one that it doesn't recognize till your second add. Assuming you have a set price in the admin panel for Ice Pack Medium could you try adding in the $params array a 'price' => whateverpriceyouwanthere

  • I added the price as array parameter, but still the same result, it shows zero price. – Akhil Kurian Aug 5 '16 at 4:13
0

Try below code to add product to the cart (basically to the quote)

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

Where $product is an instance of Mage_Catalog_Model_Product

0

tested code and working without any problem

  $product_id=1387;
  $_product = Mage::getModel('catalog/product')->load($product_id);
  $cart = Mage::getModel('checkout/cart');
  $cart->init();
  $cart->addProduct($_product, array('qty' => 1));    
  $cart->save();
  • 1
    The problem is probably that it's called from this specific observer. – Fabian Schmengler Aug 5 '16 at 10:37
  • Yes the problem is related to the event checkout_cart_add_product_complete . – Akhil Kurian Aug 6 '16 at 7:34
  • On which event you trigger the observer to add product to cart? can you provide the code? – Akhil Kurian Aug 6 '16 at 7:36
0

For some reason totals collection is not triggered by your code. As soon as you visit the cart page or add another product, totals are collected again and everything looks right.

The problem with your code seems to be that you are initializing a new cart object, there is probably a conflict with the already instantiated cart object .

Try to replace the first two lines with

$cart = Mage::getSingleton('checkout/cart');

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