Question

Can I check if a product is in my wishlist? If yes, how?

Was it helpful?

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
scroll top