How do I check is product in my wishlist?
-
22-10-2019 - |
Question
Can I check if a product is in my wishlist? If yes, how?
Solution
Option one. Seams cleaner.
$productId = 1;
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($productId)
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customerId, true);
$hasProduct = false;
foreach ($wishlist->getItemCollection() as $_item) {
if ($_item->representProduct($product)) {
$hasProduct = true;
break;
}
}
//$hasProduct tells you if the product is in the wishlist
//Option 2. Seams faster
$productId = 1;
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customerId, true);
$collection = Mage::getModel('wishlist/item')->getCollection()
->addFieldToFilter('wishlist_id', $wishlist->getId())
->addFieldToFilter('product_id', $productId);
$item = $collection->getFirstItem();
$hasProduct = !!$item->getId();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange