2

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.

8

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();
        }
    }
}
  • I will check this ..... – Vignesh Bala Mar 27 '17 at 6:53
2

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.

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.