2014-02-06 65 views
1

我在Magento 1.8上創建支付網關模塊
在我的模型代碼中,我試圖以正確的格式爲SOAP請求準備一個字符串。Magento付款模塊:購物車中的所有物品

我需要收集訂購產品,我用下面的代碼:

$order_string = ""; 
$quote = Mage::getSingleton('checkout/session')->getQuote(); 
$items = $quote->getAllVisibleItems(); 
foreach ($items as $item) { 
    $order_string .= "prod=" . $item->getName() . ",item_amount=" . $item->getPrice() . 'x' . $item->getQty() . ';'; 
} 

我面臨的問題是,$items變量始終是空的。 我試圖得到這個信息的許多變化:

$items = Mage::getSingleton('checkout/cart')->getAllItems(); 
$items = Mage::helper('checkout/cart')->getAllItems(); 
$items = Mage::helper('checkout/cart')->getCart()->getAllItems(); 
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems(); 

如果我嘗試和Mage::log任何信息,我面對的錯誤:

User Error: Some transactions have not been committed or rolled back in /Users/[Username]/Sites/[Site Name]/lib/Varien/Db/Adapter/Pdo/Mysql.php on line 3865 

如果我使用->getItemsCount();作爲後綴而不是getAllItems()getAllVisibleItems(),我似乎正確返回了項目數。

我只想返回一個項目數組,但真的,我需要的是名稱,價格和數量,所以如果有另一種方法返回該信息,我願意接受。

回答

1

我剛纔面臨同樣的問題。

$cartItems = Mage::getSingleton('checkout/session')->getQuote()->getAllItems(); 
foreach ($cartItems as $item) { 
    echo Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getShortDescription(); 
    echo $item->getPrice(); 
    echo $item->getSku(); 
    echo $item->getQty(); 
} 

找到在: http://www.kathirvel.com/magento-custom-attributes-of-products-in-the-cart/

如果還不行,那麼這是你的事件時讓你失望。如果你通過一個控制器運行它,完美地工作。

使用Magento社區版本1.9.0.1

相關問題