1

I want to remove an item from wishlist in magento2 with wishlist id or wish list item id, how can do that ?

4

You first need to inject several classes in your constructor

protected $_wishlistProviderInterface;
protected $_itemFactory;

public __construct(
    ...
    \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProviderInterface,
    \Magento\Wishlist\Model\ItemFactory $itemFactory
    ...
) {
    ...
    $this->_wishlistProviderInterface = $wishlistProviderInterface;
    $this->_itemFactory = $itemFactory;
    ...
}

Then you can do:

$this->_itemFactory->create()->load($itemId);
if (!$item->getId()) {
    throw new NotFoundException(__('Item not found.'));
}
$wishlist = $this->_wishlistProviderInterface->getWishlist($item->getWishlistId());
if (!$wishlist) {
    throw new NotFoundException(__('Wishlist not found.'));
}
try {
    $item->delete();
    $wishlist->save();
} catch (\Exception $e) {
}
  • How you can get $itemId and $item in this? – VIPIN A ROY Aug 29 '18 at 10:41
0

You can get itemId by using customer session.There is one method in wishlist loadByCustomerId, please check that one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.