3

I want to get all the product ids from wishlist in magento. I tried with below code but I am getting only one product id.

public function GetWishlistProductId($productId)
{
    $customer = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');

    $wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer);
    $wishListItemCollection = $wishList->getItemCollection();

    if (count($wishListItemCollection)) {
        $arrProductIds = array();

        foreach ($wishListItemCollection as $item) {
            /*@var $product Mage_Catalog_Model_Product */
            $product = $item->getProduct();
            $arrProductIds[] = $product->getId();

            foreach ($arrProductIds as $key => $val){
                if($productId == $val){
                    $pval=$val;
                }
            }
        }
    }
    return($pval);
} 

1 Answer 1

4

The follows should do what you are wanting but you will have to change your loop structure a little bit as when you loop through your products you will only have finish one iteration of the collection items in your original version:

public function GetWishlistProductId($productId)
{
    $customer = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');

    $wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer);
    $wishListItemCollection = $wishList->getItemCollection();

    if (count($wishListItemCollection)) {
        $arrProductIds = array();

        foreach ($wishListItemCollection as $item) {
            $arrProductIds[] = $item->getProductId();
        }
    }
    // Now here you can take that array and do what you want with it.        
    return $arrProductIds;
}
10
  • hi david still i am getting on product idArray ( [0] => 27 )
    – Mani Dev
    Commented Jan 23, 2014 at 13:55
  • @ManiCoolDude does that entry in the database have more than one product id? You could also try Mage::log($item->getProductId()); to log what and how many product ids are returned. Commented Jan 23, 2014 at 13:58
  • yeah david There is more than one product id
    – Mani Dev
    Commented Jan 23, 2014 at 14:00
  • @ManiCoolDude could you update the code in the question to show the full loops? Commented Jan 23, 2014 at 14:01
  • check post i have updated with my code
    – Mani Dev
    Commented Jan 23, 2014 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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