我需要购买购物车中的当前产品以估算多少 产品在?我使用下面的代码,但它给了我这个错误 信息 的>致命错误:调用未定义的方法 Mage_Sales_Model_Resource_Quote_Item_Collection :: getAllItems()
代码:
<?php
require 'app/Mage.php';
Mage::app();
if (!Mage::isInstalled()) {
echo "Application is not installed yet, please complete install wizard first.";
}
$cart =Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection();
foreach ($cart->getAllItems() as $item) {
$productName = $item->getProduct()->getName();
$productPrice = $item->getProduct()->getPrice();
print($item);
}
答案 0 :(得分:0)
为此,最好的方法是从magento admin创建API用户和密码,然后使用下面的代码。
$userName = 'API USER';
$key = 'API KEY';
$userId = 'USER ID';
$client = new SoapClient(Mage::getBaseUrl().'api/soap/?wsdl');
$session = $client->login($userName, $key);
$customer = Mage::getModel('customer/customer')->load($userId);
$quote = Mage::getSingleton('sales/quote')->loadByCustomer($customer);
$quoteId = $quote->getId();
$cartInfo = $client->call($session, 'cart.info', $quoteId);
foreach($cartInfo['items'] as $i=>$item)
{
echo $item['product_id'];
echo $item['sku'];
}
答案 1 :(得分:0)
您的代码中存在问题:
$cart =Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection();
应该是:
$cart = Mage::getSingleton('checkout/cart');
$quote = $cart->getQuote();
foreach ($quote->getAllItems() as $item) {
$productName = $item->getProduct()->getName();
$productPrice = $item->getProduct()->getPrice();
print($item);
}
答案 2 :(得分:0)
请尝试以下代码。
$ totalItems = Mage :: getSingleton(&#39; checkout / cart&#39;) - &gt; getQuote() - &gt; getItemsCount();
有关详细信息,请参阅此链接:http://www.magebuzz.com/blog/magento-get-product-quantity-in-cart/