Get All Items & Totals In Magento Shopping Cart

Wednesday, October 31, 2012
This blog will show you how to get all items in a shopping cart and totals in a magento store :
 $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
 foreach($items as $item) {
    echo 'Product ID: '.$item->getProductId();
    echo 'Product Name: '.$item->getName();
    echo 'Product Sku: '.$item->getSku();
    echo 'Product Quantity: '.$item->getQty();
    echo 'Product Price: '.$item->getPrice();

if ($this->helper('tax')->displayCartPriceInclTax() || $this->helper('tax')->displayCartBothPrices()):
    $_incl = $this->helper('checkout')->getPriceInclTax($item);
    echo 'Product Price: '. $this->helper('checkout')->formatPrice($_incl- $item->getWeeeTaxDisposition());
else:
    echo 'Product Price: '. $final_price= Mage::helper('core')->currency($item->getPrice());
endif;
 }

// Total items added in cart
$totalItems = Mage::getModel('checkout/cart')->getQuote()->getItemsCount();

// Total Quantity added in cart
$totalQuantity = Mage::getModel('checkout/cart')->getQuote()->getItemsQty();

// Sub Total for item added in cart
$subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();

//grand total for for item added in cart
$grandTotal = Mage::getModel('checkout/cart')->getQuote()->getGrandTotal();

6 comments:

  1. sharvan kumar said...:

    very good article.well done

  1. Anonymous said...:

    thanks alot. nie article

  1. Karan Yadav said...:

    This Code is Very Nice But I need Mage::getSingleton('checkout/session')->getQuote()->setStoreId()->getAllItems(); So, Possible...?

  1. Anonymous said...:

    In my below code return only returns 'qty' of each products and if do echo then it prints all things but in end it prints null aslo. Do you have any idea how can print all details in json file for rest calls.

    Mage::app()->setCurrentStore(4);
    $cart = Mage::getModel('sales/quote')->loadByCustomer($this->getApiUser()->getUserId());
    $items = array();
    foreach ($cart->getAllVisibleItems() as $key => $item) {
    $items[] = array(
    'name' => $item->getName(),
    'entity_id' => $item->getProductId(),
    'description' => $item->getDescription(),
    'final_price_with_tax' => $item->getBasePriceInclTax(),
    'qty' => $item->getQty()
    );

    }
    return $items;

Post a Comment