Magento - Accessing a customer's wishlist

Go To StackoverFlow.com

9

I would like to be able to load a customer's wishlist and return the product id's of the products within the list

I am using:

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

The problem is that the arrays in the Item Collection are protected and I can't find any methods to extract the data.

Is there another way to do this?

2012-04-04 05:07
by Matthew Dolman


17

You're very close to your target.

$wishList = Mage::getSingleton('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();
    }
}

The array variable $arrProductIds will now contain the list of all Product IDs that have been wishlisted by that specific Customer.

Hope it helps.

2012-04-04 06:54
by Knowledge Craving
You can also simply use $item->getProductId(); - no need to load the whole product - simonthesorcerer 2014-01-07 11:53
From a re-usability standpoint, getting the entire product makes more sense though. Such as displaying the product name as well as the link, you'll need more than just an ID - DWils 2014-02-21 18:22


7

Your code is correct. There may be customer is not loaded. here is code.

$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();

foreach ($wishListItemCollection as $item)
{
   // do things
}
2013-09-09 05:47
by Amitkumar solanki


5

In any template, using magento 1.8, this works

Total: <?php echo $this->helper('wishlist')->getItemCount() ?>

// Items
$this->helper('wishlist')->getWishlist()->getItemCollection();
2013-12-15 01:23
by Ignacio Pascual
Thanks :) !!! this piece of code is working fine !! - Shan 2015-02-23 13:49


2

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

foreach ($wishListItemCollection as $item)
{
    //do your thing e.g. echo $item->getName();
}
2012-04-04 06:54
by benmarks


2

Try this with product all details like name, images etc...

<?php
 $customer = Mage::getSingleton('customer/session')->getCustomer();
 if($customer->getId())
{
     $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
     $wishListItemCollection = $wishlist->getItemCollection();
     foreach ($wishListItemCollection as $item)
     {
           echo $item->getName()."</br>";
           echo $item->getId()."</br>";
           echo $item->getPrice()."</br>";
           echo $item->getQty()."</br>";  
           $item = Mage::getModel('catalog/product')->setStoreId($item->getStoreId())->load($item->getProductId());
          if ($item->getId()) :
?>
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'small_image')->resize(113, 113); ?>" width="113" height="113" />
<?php endif; } } ?> 
2014-03-22 06:54
by Hardik
Ads