0

I'm trying to remove an item from the wishlist programmatically. My code snippet as following:

/**
     * @param integer $customerId
     * @param integer $productId
     * @return boolean
     * @throws NoSuchEntityException
     */
    public function removeProductFromWishlist($customerId, $productId) {
        try {
            $this->_productRepository->getById($productId);
        } catch (NoSuchEntityException $e) {
            $product = null;
        }

        try {
            $this->_customerRepository->getById($customerId);
        } catch (NoSuchEntityException $e) {
            $customer = null;
        }

        $wishlist = $this->_wishlistFactory->create()->loadByCustomerId($customerId, true);
        $ids = $this->_wishlistCollectionFactory->create()->addCustomerIdFilter($customerId)->load()->getAllIds();
        try {
            foreach ($ids as $itemId) {
                $item = $this->_itemModel->load($itemId);
                if ($item->getProductId() == $productId) {
                    $item->delete();
                    $wishlist->save();
                }
            }
        } catch (\Exception $e) {
        }
        return true;
    }

But it's doesn't work, when I calling this method from Postman, I'm getting an error:

{
    "messages": {
        "error": [
            {
                "code": 500,
                "message": "Fatal Error: 'Uncaught Error: Call to a member function getId() on boolean in /home/logbasex/IdeaProjects/clique-omnyfy/vendor/dotmailer/dotmailer-magento2-extension/Observer/Customer/RemoveWishlistItem.php:52\nStack trace:\n#0 /home/logbasex/IdeaProjects/clique-omnyfy/vendor/magento/framework/Event/Invoker/InvokerDefault.php(88): Dotdigitalgroup\\Email\\Observer\\Customer\\RemoveWishlistItem->execute(Object(Magento\\Framework\\Event\\Observer))\n#1 /home/logbasex/IdeaProjects/clique-omnyfy/vendor/magento/framework/Event/Invoker/InvokerDefault.php(74): Magento\\Framework\\Event\\Invoker\\InvokerDefault->_callObserverMethod(Object(Dotdigitalgroup\\Email\\Observer\\Customer\\RemoveWishlistItem), Object(Magento\\Framework\\Event\\Observer))\n#2 /home/logbasex/IdeaProjects/clique-omnyfy/vendor/magento/framework/Event/Manager.php(66): Magento\\Framework\\Event\\Invoker\\InvokerDefault->dispatch(Array, Object(Magento\\Framework\\Event\\Observer))\n#3 /home/logbasex/IdeaProjects/clique-omnyfy/generated/code/Magento/Framework/Event/Manager/Proxy.php(95): Magen' in '/home/logbasex/IdeaProjects/clique-omnyfy/vendor/dotmailer/dotmailer-magento2-extension/Observer/Customer/RemoveWishlistItem.php' on line 52",
                "trace": "Trace is not available."
            }
        ]
    }
}

Any solution? Thank you!

1
  • I guess it could be a bug in dotmailler itself, if you dont need dotmailler can you try to desactivate it and see if it works? Commented Apr 2, 2021 at 20:38

1 Answer 1

0

Please use the below code to remove the wishlist item.

// $this->wishlist = \Magento\Wishlist\Model\Wishlist
$wish = $this->wishlist->loadByCustomerId($customerId);
$items = $wish->getItemCollection();
foreach ($items as $item) {
    if ($item->getProductId() == $productId) {
        $item->delete();
        $wish->save();
    }
}

Let me know if you still facing any issues.

Thanks!

3
  • I already tried :) But getItemCollection() return empty collection. As same as: magento.stackexchange.com/questions/316478/…
    – logbasex
    Commented Apr 2, 2021 at 9:43
  • So, the first check do you have any items on your wishlist?
    – ZealousWeb
    Commented Apr 2, 2021 at 9:57
  • Absolutely, I have two items in wishlist_item table. In my code snippet, I can get the item's productId but I can't delete item.
    – logbasex
    Commented Apr 2, 2021 at 10:00

Your Answer

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

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