Search code examples
magentomagento-1.5

Always count return 0 (zero) from a product collection in Magento


Actually, I am trying to find if this product is in a wishlist or not. So I tried Daniel Sloof's answer in Stack Overflow question Check whether a product is in the wishlist or not, but the product collection always returns 0.

What I tried is here:

$_productCollection1 = Mage::helper('wishlist')
                             ->getProductCollection()
                             ->addFieldToFilter('sku','00114477oo0077');
 $_productCollection1->count();

This one returns "0".

To debug, I print the query in directly applied in my database in returning one row.

Using

$_productCollection1->getSelect()->assemble()

and query

SELECT `e` . * , `cat_index`.`position` AS `cat_index_position`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id = e.entity_id
AND cat_index.store_id = '1'
AND cat_index.visibility
IN ( 3, 2, 4 )
AND cat_index.category_id = '2'
WHERE (
e.sku = '00114477oo0077'
)

So what's is wrong here? Is there any other way to do this?


Solution

  • I solved this by the below function.

    function checkInWishilist($_product){
        Mage::getSingleton('customer/session')->isLoggedIn();
        $session = Mage::getSingleton('customer/session');
        $cidData = $session->isLoggedIn();
        $customer_id = $session->getId();
    
        if ($customer_id){
            $wishlist = Mage::getModel('wishlist/item')->getCollection();
            $wishlist->getSelect()
                      ->join(array('t2' => 'wishlist'),
                             'main_table.wishlist_id = t2.wishlist_id',
                             array('wishlist_id','customer_id'))
                             ->where('main_table.product_id = '.$_product->getId().' AND t2.customer_id='.$customer_id);
            $count = $wishlist->count();
            $wishlist = Mage::getModel('wishlist/item')->getCollection();
        }
        else{
            $count="0";
        }
    
        if ($count):
            return true;
        else:
            return false;
        endif;
    }