1
votes

I'm working on AJAX cart for Magento.

I'm not interested in extensions, as this cart has to be author made, it's very custom.

Can you tell me what's the best way to acquire such info as grand item total in cart and number of items?

In fastest and most relevant way.

I can create external php file which will gather this info from user's session and then AJAX script which is gonna display it on header every time when user adds or deletes a product.

I can think of that's not the best solution.

Is there some API, or what's the best way to do this?

Thanks,

Adam

1
Hi Adam, how can i display total cart weight in ajax cart?Gem

1 Answers

4
votes

If you are referring to a mini-cart of sorts, I have accomplished this on multiple projects by using the below code:

<?php

// gets the current session of the user
Mage::getSingleton('core/session', array('name'=>'frontend'));

// loads all the products in the current users cart
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();

// counts the total number of items in cart
$totalItems = count($items);

// you can loop through all the products, and load them to get all attributes
foreach( $items as $item ) {
    $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $item->getSku());
    // display code, etc
}


// gets the subtotal of the current cart
$subTotal = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();

?>

Not sure if this is the best way to handle the mini-cart functionality but it has worked great for me in the past and is highly customizable.