我正在为 Magento 开发 AJAX 购物车。
我对扩展不感兴趣,因为这个购物车必须是作者制作的,它是非常定制的。
您能告诉我获取购物车中的总商品总数和商品数量等信息的最佳方式是什么吗?
以最快且最相关的方式。
我可以创建外部 php 文件,该文件将从用户的 session 中收集这些信息,然后创建 AJAX 脚本,每次用户添加或删除产品时,该脚本都会将其显示在标题上。
我认为这不是最好的解决方案。
是否有一些 API,或者执行此操作的最佳方法是什么?
谢谢
亚当
最佳答案
如果您指的是某种迷你购物车,我已经使用以下代码在多个项目上完成了此操作:
<?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();
?>
不确定这是否是处理迷你购物车功能的最佳方式,但它过去对我来说非常有效,并且高度可定制。
关于jquery - AJAX - 购物车 Magento 总计和购物车中的商品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18106133/