3

How to remove product from wishlist using customer id and product id programmatically? I searched alot about it, But the results are remove wishlist item using item id only. So is there any way to remove wish list item using customer id and product id only?

Please suggest me how can I get this done.

Thanks.

2 Answers 2

11

Unfortunately Magento didn't provide ready to use function to delete item from wishlist. But you can use follow code to do this, I tested it and all works fine at my local Magento.

protected $wishlist;

public function __construct(
    \Magento\Wishlist\Model\Wishlist $wishlist
) {
    $this->wishlist = $wishlist;
}

public function execute()
{
    $customerId = 1;
    $productId = 6;
    $wish = $this->wishlist->loadByCustomerId($customerId);
    $items = $wish->getItemCollection();

    /** @var \Magento\Wishlist\Model\Item $item */
    foreach ($items as $item) {
        if ($item->getProductId() == $productId) {
            $item->delete();
            $wish->save();
        }
    }
}
2
  • I will check this ..... Mar 27, 2017 at 6:53
  • this is now working
    – devmage
    Jun 23, 2020 at 5:30
3

The given answer is perfect here is my solutuion. Thank you so much @Amasty

protected $wishlist;

public function __construct(
      \Magento\Wishlist\Model\Wishlist $wishlist
) {
      $this->wishlist = $wishlist;
}

    /* Remove a specific item from the wishlist.
     *
     * @api
     * @param  int $customerId id of customer.
     * @param  int $productId id of a product.
     * @return Empty wishlist.
     */
    public function removeWishlist($customerId,$productId) 
    {
        $wish = $this->wishlist->loadByCustomerId($customerId);
        $items = $wish->getItemCollection();
        foreach ($items as $item) 
        {
            if ($item->getProductId() == $productId) 
            {
                $item->delete();
                $wish->save();   
                return json_encode("You have successfully remove item from wishlist.");
            }
        }

I hope this will help.

1
  • not working here
    – devmage
    Jun 22, 2020 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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