Hi Magento developers...
I'm having a bit of trouble with my new module. I created a simple module for people leaving their basket, without buying. I register every item put in the basket nice, with the observers in Magento. I then have a cron job running every minute and checking if a user has been inactive for 20 min. Then it send an email with a reference to a token in the database and here comes my failing bit.
I'm calling something like: www.domain.tld/add-to-basket?token=some_hash
I then retrieve all items from my database, with that token nothing hard here. But when I want to add then to the current guest cart it fails. I have searched the web thin and have come up with this.:
$session = Mage::getModel('checkout/session');
$quote = $session->getQuote();
$quote->setStoreId(1);
foreach($entries as $entry){
$quantity = $entry->getQuantity();
$productId = $entry->getProductId();
$product = Mage::getSingleton('catalog/product')->load($productId);
$quote->addProduct($product, new Varien_Object(['qty' => $quantity]));
}
$quote->setTotalsCollectedFlag(false);
$quote->collectTotals();
$quote->save();
Info:
- I'm using oneStepCheckout
- Sometimes it works
- No exceptions are thrown (the code is wrapped in a try/catch)
- Have been debugging all the way (no errors or mystically values)
Maybe:
I think that the problem is, that the cart has not been initialized properly, when I try to add stuff to it, but I can't seem to find a solution for that.
Hope you can help.
// Ulrik