2011-12-05 21 views
6

Belirli bir ürün için ürün sepetini almaya çalışıyorum;Magento - Belirli bir ürün kimliği için sepet öğeler alın

Bu kodu denedim:

$product = Mage::getModel('catalog/product') 
    ->setStoreId(Mage::app()->getStore()->getId()) 
    ->load('2784'); 

$quote = Mage::getSingleton('checkout/cart')->getQuote(); 
$cartItems = $quote->getItemByProduct($product); 
foreach ($cartItems as $item) { 
    echo $item->getId()."<br/>"; 
} 

ama bu bir şey verir yok.

"getItemByProduct" öğesini doğru biçimde kullanmak için kodumu nasıl değiştirebilirim?

Yardımlarınız için teşekkürler.

cevap

1

Ben

foreach ($quote->getItems() as $item) { 
    if ($item->getProductId() == $product->getId()) { 
     print_r($item->getData()); 
    } 
} 
+0

Ve doğrudan getItemByProduct'ı nasıl kullanabilirim? – Bizboss

10

getItemByProduct() ekstra döngü ilk Mage_Sales_Model_Quote_Item yüzden orada olup eşleşen gerek döndürür kullanmayı tercih ediyorum. bu yöntem sales/quote modelin olduğu gibi

$item = $quote->getItemByProduct($product); 
if ($item !== false) echo $item->getId(); 
+0

Bir hata alıyorum: Önemli hata: 996 – Bizboss

+0

satırında /catalog/product/list.phtml öğesinde nesne olmayan bir öğe işlevine getId() işlevini çağırın. $ Ürün '$ quote' sonra değilse' $ item 'false' olacaktır. Gördüğünüz hataya neden olabilecek bir şey budur. – clockworkgeek

1

Sen checkout/cart veya checkout/quote modeline getItemByProduct() işlevini kullanamazsınız.

Bu yöntemi Mage_Sales_Model_Quote class adresinde bulabilirsiniz. bu yüzden sales/quote ile kullanılır. umarım bu yararlıdır.

+0

chriz'i düzenlemek için teşekkürler –

İlgili konular