0

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

8

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();
4
  • Option 2 worked very well, thank you Marius. Feb 28 '14 at 8:56
  • @TimHofman. Thanks for the edit.. Damn copy/paste.
    – Marius
    Feb 28 '14 at 9:34
  • @Marius no problem!
    – Tim Hofman
    Feb 28 '14 at 9:37
  • Option2: $hasProduct = (bool) $collection->count()
    – Styx
    Mar 28 '15 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.