0

I have below code to create cart and addProduct :

$quote = Mage::getModel('sales/quote')->setStoreId(1);
$quote->assignCustomer($customer);
$product = Mage::getModel('catalog/product')->load($productId);
$quote->addProduct($product, new Varien_Object(array('qty' => 1)));
$quote->save();

Above code creating corresponding rows in quote tables, but when I'm checking on frontend cart page for same customer then I'm getting Cart is empty on cart page.

But when I try to addproduct in existing cart which is created from frontend using below code :

$quote = Mage::getModel('sales/quote')->load($cartId);
$quote->assignCustomer($customer);
$product = Mage::getModel('catalog/product')->load($productId);
$quote->addProduct($product, new Varien_Object(array('qty' => 1)));
$quote->save();

then the programmatically added product is coming on frontend.

Why this is happening?

2

1 Answer 1

0

I think issue in your first code snippet is that, when you add product into cart and assign this product to customer Magento create a new entry in sales_flat_quote table but the flag is_active is 0. So whenever you try to check frontend cart page for same customer, Magento load the quote with is_active 1 (If quote not found with is_active then It create a new quote).

So to solve this problem just set the flag is_active to 1. So your full code snippets looks like this,

$quote = Mage::getModel('sales/quote')->setStoreId(1);
$quote->assignCustomer($customer);
$product = Mage::getModel('catalog/product')->load($productId);
$quote->addProduct($product, new Varien_Object(array('qty' => 1)));
$quote->setData('is_active', 1); // only add this line
$quote->save();
1
  • I checked and is_active column is 1 in table sales_flat_quote from my code. But items_count and items_qty are 0. Dec 28, 2017 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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