1

I'm trying to return the wishlist item collection for the currently logged in user and iterate over the collection.

I've tried the following, but I get an empty collection.

<?php /* @escapeNotVerified */
                                    $_in_wishlist = false;
                                    echo count($this->helper('Magento\Wishlist\Helper\Data')->getWishlistItemCollection());
                                    foreach ($this->helper('Magento\Wishlist\Helper\Data')->getWishlistItemCollection() as $_wishlist_item){
                                        if($_product->getId() == $_wishlist_item->getProduct()->getId()){
                                            $_in_wishlist = true;
                                            break;
                                        }
                                    } ?>

I've also tried, getWishlist()->getItemCollection() but this doesn't return anything either.

I'm logged in and I have 2 items in my wishlist. Could this be to do with caching?

2 Answers 2

5

To do so you need to use the Magento\Wishlist\Controller\WishlistProviderInterface interface.

Inject this class in your constructor:

protected $wishlistProvider;

public function __construct(
    ...
    \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
) {
    $this->wishlistProvider = $wishlistProvider;
    ...
}

Then you can call the following to get the current logged in user wishlist:

$currentUserWishlist = $this->wishlistProvider->getWishlist();
if ($currentUserWishlist) {
    $wishlistItems = $currentUserWishlist->getItemCollection();
}
4
  • Hi, thanks for this, i've overridden the ListProduct class by adding <preference for="Magento\Catalog\Block\Product\ListProduct" type="Amrita\Catalog\Block\Product\ListProduct" /> to di.xml. I wan't to override the constructor so i've extended Magento\Catalog\Block\Product\ListProduct and added my own constructor as above, which injects the wishlist provider and calls the constructor in the base file. The issue is that it seems to have broken the product list, as no products are now pulling through, have i missed a step? Commented Jun 13, 2016 at 14:32
  • Hi, i moved my list.phtml out of the design directory and into the custom module where i had overridden the constructor, but the wishlist items are still not being returned? Commented Jun 13, 2016 at 14:54
  • What about class Magento\Wishlist\Helper\Data and method: /** * Retrieve wishlist by logged in customer * * @return \Magento\Wishlist\Model\Wishlist */ public function getWishlist() { if ($this->_wishlist === null) { if ($this->_coreRegistry->registry('shared_wishlist')) { $this->_wishlist = $this->_coreRegistry->registry('shared_wishlist'); } else { $this->_wishlist = $this->wishlistProvider->getWishlist(); } } return $this->_wishlist; } Commented Jan 13, 2017 at 8:04
  • It also uses wishlist provider but not in first place - first it check registry. Returns correct wishlist. Commented Jan 13, 2017 at 8:05
2
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$wishlist = $objectManager->get('\Magento\Wishlist\Model\Wishlist');
$wishlist_collection = $wishlist->loadByCustomerId("customer_id", true)->getItemCollection();
?>

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.