0

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

1 Answer 1

0

I think I figured it out. I initialize the cart, then add all the items, then I set the flag of totals collected to false and then run save on the cart.

The final code:

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

foreach($entries as $entry){
    $quantity = $entry->getQuantity();
    $productId = $entry->getProductId();
    $product = Mage::getModel('catalog/product')->load($productId);
    $cart->addProduct($product, new Varien_Object(['qty' => $quantity]));
}

$cart->getQuote()->setTotalsCollectedFlag(false);
$cart->save();

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.