2017-06-04 122 views
0

I need to get the currently products in cart to estimate how many products are in ? I used the below code but it gave me this error message > Fatal error: Call to undefined method Mage_Sales_Model_Resource_Quote_Item_Collection::getAllItems()如何在Magento中獲取購物車中的當前產品?

Code:

<?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

對於剛剛創建Magento管理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']; 
} 
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); 
} 
相關問題