1

Following is my code:-

 try
        {
          $store = $this->_getStore();
          Mage::app()->setCurrentStore($store->getId());
          $customer_ID = 3;
          $customer = Mage::getModel('customer/customer')->load($customer_ID);

          // load quote by customer
          $quote = Mage::getModel('sales/quote')->loadByCustomer($customer_ID);

          $quote->assignCustomer($customer);
          $product = Mage::getModel('catalog/product')
                     // set the current store ID
                     ->setStoreId(Mage::app()->getStore()->getId())
                     // load the product object
                     ->load(3);

           // Add Product to Quote
           $quote->addProduct($product,1);

           // Calculate the new Cart total and Save Quote
           $quote->collectTotals()->save();

           return json_encode(array("", $quote->getId()));

        }catch(Exception $e)
        {
            return $e->getMessage();;
        }   

Using this the product is getting added in cart successfully and returning quote_id, but using this id i want to fetch current cart items details for currently logged in user.

1 Answer 1

2

You need to load quote collection and filter via id or you can try below code for your requirement:

// retrieve quote items array
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

foreach($items as $item) {
    echo 'ID: '.$item->getProductId().'<br />';
    echo 'Name: '.$item->getName().'<br />';
    echo 'Sku: '.$item->getSku().'<br />';
    echo 'Quantity: '.$item->getQty().'<br />';
    echo 'Price: '.$item->getPrice().'<br />';
    echo "<br />";          
}

I hope this will help you.

4
  • I want to load cart items for logged in user, will this code work?
    – Akash Rai
    May 5, 2016 at 12:54
  • This code is not working, please check above my code and provide me your answer based on it.
    – Akash Rai
    May 5, 2016 at 12:57
  • What happened sir?
    – Akash Rai
    May 5, 2016 at 13:14
  • Is it possible to get cart details using customer id
    – Akash Rai
    May 5, 2016 at 13:53

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.