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!