Magento – How to get all the customer wishlist items in magento

customermagento-1.8PHPwishlist

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

Best Answer

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