Hello Magento Friends,
The current Magento tutorial blog illustrates How to Get Wishlist Data Using Customer ID in Magento 2.
Wishlist is a useful tool to help customers save their favorite items for future purchases. Learn How to Configure Wishlist in Magento 2.
The customer’s wishlist items are linked using the customer ID. To retrieve a customer’s wishlist items, you need the customer ID to filter the wishlist collection by the corresponding customer. The customer ID serves as a unique identifier to fetch the wishlist items associated with that specific customer.
Using the below step, you can get wishlist data, including product ID and quantity of product from customer ID in Magento 2.
Steps to Get Wishlist Data Using Customer ID in Magento 2:
Step 1: Create Data.php on the given path.
{{magento_root}}/app/code/Vendor/Extension/Helper/Data.php
Then add the code as follows.
<?php namespace Vendor\Extension\Helper; use Magento\Framework\App\Helper\Context; use Magento\Wishlist\Model\Wishlist; class Data extends \Magento\Framework\App\Helper\AbstractHelper { protected $wishlist; public function __construct( Wishlist $wishlist, Context $context ) { parent::__construct($context); $this->wishlist = $wishlist; } public function getwishlistdata($customerId) { return $this->wishlist->loadByCustomerId($customerId)->getItemCollection()->getData(); } }
You can use this method in the controller file or block file as per your requirement.
Conclusion:
Hence, using the above method, you can easily get wishlist data of customers using the customer ID in Magento 2. If you face any errors while implementing the above code, kindly share them with me through the comment section.
Share the article through social media platforms and with your other Magento friends. Stay in touch with us for more.
Happy Coding!