1

i want to retrieve details of items in cart created , i have a cart id , how can i retrieve the cart info(details). i am using this code but it gives no results , please help

<?php

if(isset($_POST["cartId"])){
  require_once('../app/Mage.php'); //Path to Magento umask(0); 
  $cartId=$_POST["cartId"];
  Mage::app(); 
  $quote = Mage::helper('checkout/cart')->getCart()->getQuote();
  $result[]=array();

  foreach ($quote->getAllItems() as $item) {
    $product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
    $productMediaConfig = Mage::getModel('catalog/product_media_config');
    $baseImageUrl = $productMediaConfig->getMediaUrl($product->getImage());
    $price=0;
    $price=$product->getFinalPrice();
    $result[]=array(
      "productId"=>$item->getProduct()->getId(),
      "price"=>$price,
      "imageUrl"=>$baseImageUrl
      );
  }

    //return array of products 

  echo print_r($result);
  exit();

}else{
    echo "error";
    exit();
}

4 Answers 4

2

if you have the cart id, you can do this:

$quote = Mage::getModel('sales/quote')->load($cartId);
foreach ($quote->getAllItems() as $item) { //or $quote->getAllVisibleItems()
    //...your code here
}
6
  • i think he is first trying to get id from session first and then load cart Commented Nov 5, 2015 at 6:57
  • Gives an error Fatal error: Call to a member function load() on a non-object Commented Nov 5, 2015 at 6:59
  • @mahethekiller. Sorry I made a mistake. See my edit.
    – Marius
    Commented Nov 5, 2015 at 7:01
  • now i get this error Fatal error: Call to a member function getModelInstance() on a non-object in /home/phonatetech/public_html/app/Mage.php on line 463 Commented Nov 5, 2015 at 7:11
  • did you add Mage::app(); above the code I've written?
    – Marius
    Commented Nov 5, 2015 at 7:13
0

The following code works for me.

$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
    $productId = $item->getProductId();
    $product = Mage::getModel('catalog/product')->load($productId);
    // Do something more
}
9
  • it returns blank results nothing is returned ...., Commented Nov 5, 2015 at 6:54
  • Are you getting this in a page other than Magento ? did you checked dose it even going inside of if condition ? Commented Nov 5, 2015 at 6:55
  • page is at root of my magento website Commented Nov 5, 2015 at 6:56
  • and are you getting $cartId ? is it returning any value Commented Nov 5, 2015 at 6:58
  • yes cart id is there Commented Nov 5, 2015 at 6:58
0

Try bellow code:

         $storeId = '';
         $quoteId = 1;
         $quote = Mage::getModel("sales/quote");

        if (!(is_string($storeId) || is_integer($storeId))) {
            $quote->loadByIdWithoutStore($quoteId);
        } else {
            $quote = $quote->setStoreId($storeId)->load($quoteId);
        }

        $shippingaddress = $quote->getShippingAddress();
        $billingAddress = $quote->getBillingAddress();

        foreach ($quote->getAllItems() as $item) {
            print_r($item);
        }

        $payment = $quote->getPayment();

OR

$cartId = 1;
$info = Mage::getModel('checkout/cart_Api')->info($cartId);
print_r($info);
1
  • i got another solution thanx Commented Nov 5, 2015 at 7:59
0

Finally i found an solution , what i did was that i used soap api to get product Ids and then using mage class to load other data here is the code :

    require_once('../app/Mage.php');//Path to Magento umask(0);
    if(isset($_GET["cartId"])){
      $cartId=$_GET["cartId"];
      echo $cartId;
      $proxy = new SoapClient('https://www.magentohost.com/api/v2_soap/?wsdl'); // TODO : change url
      $sessionId = $proxy->login('apiuser','apipassword');
      $result = $proxy->shoppingCartInfo($sessionId, $cartId);
      $itemDetails=array();
      Mage::app(); 
      foreach ($result->items as $item) { 
        $product2 = Mage::getModel('catalog/product')->load($item->product_id);
        $productMediaConfig = Mage::getModel('catalog/product_media_config');
        $baseImageUrl = $productMediaConfig->getMediaUrl($product2->getImage());
        $price2=0;
        $price2=$product2->getFinalPrice();
        $itemDetails[]=array(
          "productId"=>$item->product_id,
          "name"=>$item->name,
          "price"=>$product2->getPrice(),
          "spprice"=>$price2,
          "imageurl"=>$baseImageUrl,
          "qty"=>$item->qty
          );
      }
        //return array of productdetails
      echo print_r($itemDetails);
      exit();
    }else{
      echo "error";
      exit();
    }

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.