How to get the wistlist_item_id in magento2
-
24-02-2021 - |
Question
How to get the wistlist_item_id for particular customerId and productId in magento2?
Thanks.
Solution
You need to inject \Magento\Wishlist\Model\Wishlist
Model class into your construct. Add this below code in your controller :
<?php
namespace VendorName\ModuleName\Controller\Index;
class Wishlist extends \Magento\Framework\App\Action\Action {
/**
* @var \Magento\Wishlist\Model\Wishlist
*/
protected $wishlist;
/**
* [__construct description]
* @param \Magento\Framework\App\Action\Context $context [description]
* @param \Magento\Wishlist\Model\Wishlist $wishlist [description]
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Wishlist\Model\Wishlist $wishlist
) {
$this->wishlist = $wishlist;
parent::__construct($context);
}
/**
* Default customer account page
*
* @return void
*/
public function execute() {
$customerId = '1';
$wishListData = $this->wishlist->loadByCustomerId($customerId, true)->getItemCollection();
foreach ($wishListData as $key => $value) {
echo "Wishlist Item ID : ".$value['wishlist_item_id']."<br/>";
echo "Wishlist Product ID : ".$value['product_id']."<br/>";
}
}
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange