Today we will talk about, How to get multiple wishlist data by wishlist name in the magento2 enterprise edition project.
As you know, in Magento 2 enterprise edition, customers can create multiple wishlists at their convenience. With multiple wishlists, users can easily categorize and organize their desired products. It also allows them to share specific lists with friends or family conveniently.
We can easily fetch wishlists by specific customer and wishlist name programmatically.
We can achieve this using Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory.
One customer is logged in and has a “Vacation Wants” wishlist. Let’s see how we can get this wishlist data programmatically.
We will create a function named getCustomerWishListByName() and specify 2 parameters: customerId and Wishlist Name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php namespace Jigar\Tutorials\Block; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Wishlist\Model\Wishlist; use Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory; /** * Class to load customer wish list by name. */ class Example { /** @var CollectionFactory */ private $wishlistCollectionFactory; /** * @param CollectionFactory $wishlistCollectionFactory */ public function __construct(CollectionFactory $wishlistCollectionFactory) { $this->wishlistCollectionFactory = $wishlistCollectionFactory; } /** * Load customer wish list by name. * * @param int $customerId * @param string $wishListName * @return Wishlist * @throws NoSuchEntityException if wish list with provided name for customer doesn't exist. */ public function getCustomerWishListByName(int $customerId, string $wishListName): Wishlist { $wishListCollection = $this->wishlistCollectionFactory->create(); $wishList = $wishListCollection->filterByCustomerId($customerId) ->addFieldToFilter('name', $wishListName)->getFirstItem(); if (!$wishList->getWishlistId()) { throw NoSuchEntityException::doubleField('customer_id', $customerId, 'name', $wishListName); } return $wishList; } } |
In your template file or any suitable place, you can call this function and get a wishlist as shown below.
1 2 3 4 5 6 7 8 9 10 |
<?php $customerId = 1; //Customer Id $wishlistName = "Vacation Wants"; //Wishlist Name $wishlist = $block->getCustomerWishListByName($customerId, $wishlistName); foreach ($wishlist->getItemCollection() as $item) { echo $item->getProduct()->getName();echo "<br>"; echo $item->getProduct()->getId();echo "<br>"; } |
This will output the wishlist item Name and Product ID, you can get additional product details as per your requirement.
That’s It… I hope this tutorial is helpful.
Please share it with your teammates and friends.
Do not forget to comment on your queries and feedback in the comment box.
You can also check,
Add Yes/No Attribute in Magento 2 programmatically
How to remove/disable add to compare in Magento 2?
How to Show/Hide one field based on another field in UI Component forms in Magento 2?