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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 09:29:00

问题


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?


回答1:


Looking through the other thread you've linked I'm guessing that this way of loading the collection only returns a result for customers that have logged in and have the product in their wishlist.




回答2:


The reason why the call to getProductCollection() doesn't work is because it is deprecated after version 1.4.2.0, according to offcial Magento documentation (Class Mage_Wishlist_Helper_Data).

Use getWishlistItemCollection() like :

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

instead of using getProductCollection :

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

Doing so will make the call to $_productCollection1->count() return you the current number of items in the logged user's wishlist.




回答3:


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;
}


来源:https://stackoverflow.com/questions/8193639/always-count-return-0-zero-from-a-product-collection-in-magento

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!