简体   繁体   中英

AJAX - cart Magento totals and items in cart

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM