Skip to main content
added 25 characters in body
Source Link
Shafeel Sha
  • 1.4k
  • 13
  • 32

Try this : Tested Code

Note :Just pass productId parameter in your ajax call & Logged User IdLogged User Id Automatically fetchfetch from the Session

class WishlistRemoveItems extends \Magento\Framework\App\Action\Action
{
    protected $wishlist;

    public function __construct(
        \Magento\Wishlist\Model\Wishlist $wishlist,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Action\Context $context
    ) {
        $this->wishlist = $wishlist;
        $this->request = $request;
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    public function execute()
    {

        $customerId =  $this->customerSession->getCustomer()->getId();
        $productId = $this->request->getPostValue('productId');
        $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();
            }
        }
    }
}

hope it will help you, Thanks

Try this : Tested Code

Note :Just pass productId parameter in your ajax call & Logged User Id Automatically fetch

class WishlistRemoveItems extends \Magento\Framework\App\Action\Action
{
    protected $wishlist;

    public function __construct(
        \Magento\Wishlist\Model\Wishlist $wishlist,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Action\Context $context
    ) {
        $this->wishlist = $wishlist;
        $this->request = $request;
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    public function execute()
    {

        $customerId =  $this->customerSession->getCustomer()->getId();
        $productId = $this->request->getPostValue('productId');
        $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();
            }
        }
    }
}

hope it will help you, Thanks

Try this : Tested Code

Note :Just pass productId parameter in your ajax call & Logged User Id Automatically fetch from the Session

class WishlistRemoveItems extends \Magento\Framework\App\Action\Action
{
    protected $wishlist;

    public function __construct(
        \Magento\Wishlist\Model\Wishlist $wishlist,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Action\Context $context
    ) {
        $this->wishlist = $wishlist;
        $this->request = $request;
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    public function execute()
    {

        $customerId =  $this->customerSession->getCustomer()->getId();
        $productId = $this->request->getPostValue('productId');
        $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();
            }
        }
    }
}

hope it will help you, Thanks

Source Link
Shafeel Sha
  • 1.4k
  • 13
  • 32

Try this : Tested Code

Note :Just pass productId parameter in your ajax call & Logged User Id Automatically fetch

class WishlistRemoveItems extends \Magento\Framework\App\Action\Action
{
    protected $wishlist;

    public function __construct(
        \Magento\Wishlist\Model\Wishlist $wishlist,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Action\Context $context
    ) {
        $this->wishlist = $wishlist;
        $this->request = $request;
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    public function execute()
    {

        $customerId =  $this->customerSession->getCustomer()->getId();
        $productId = $this->request->getPostValue('productId');
        $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();
            }
        }
    }
}

hope it will help you, Thanks