i have created an remove wishlist rest api, i am passing customerid & productid to "removeWishlistForCustomer()" but getItemCollection() always returns null, so i can't delete wishlist items.
Please take a look at the below code & share your thoughts on this.
<?php
namespace Vueapi\Wishlist\Model;
use Vueapi\Wishlist\Api\WishlistManagementInterface;
use Magento\Customer\Model\CustomerRegistry;
use Magento\Framework\Exception\LocalizedException;
use Magento\Wishlist\Controller\WishlistProvider;
use Magento\Wishlist\Controller\WishlistProviderInterface;
use Magento\Wishlist\Model\Item;
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;
use Magento\Wishlist\Model\WishlistFactory;
/**
* Defines the implementaiton class of the WishlistManagementInterface
*/
class WishlistManagement implements WishlistManagementInterface
{
/**
* @var WishlistProviderInterface
*/
protected $wishlistProvider;
private $wishlist;
/**
* @var CollectionFactory
*/
protected $_wishlistCollectionFactory;
/**
* @var CustomerRegistry
*/
protected $_wishlistFactory;
/**
*@var \Magento\Catalog\Model\Product
*/
private $_objectManager;
protected $_productload;
/**
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storemanagerinterface;
protected $customerRegistry;
public function __construct(
CustomerRegistry $customerRegistry,
\Magento\Framework\ObjectManagerInterface $objectmanager,
WishlistProviderInterface $wishlistProvider,
\Magento\Catalog\Model\ProductFactory $_productloader,
\Magento\Store\Model\StoreManagerInterface $storemanagerinterface,
\Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory $_wishlistCollectionFactory,
\Magento\Wishlist\Model\WishlistFactory $wishlistFactory,
\Magento\Wishlist\Model\Wishlist $wishlist
) {
$this->_objectManager = $objectmanager;
$this->wishlistProvider = $wishlistProvider;
$this->wishlist = $wishlist;
$this->_productloader = $_productloader;
$this->_wishlistFactory = $wishlistFactory;
$this->_wishlistCollectionFactory = $_wishlistCollectionFactory;
$this->storemanagerinterface = $storemanagerinterface;
$this->customerRegistry = $customerRegistry;
}
public function removeWishlistForCustomer($customerId, $productId)
{
$wish = $this->wishlist->loadByCustomerId($customerId);
$items = $wish->getItemCollection();
foreach ($items as $item) {
if ($item->getProductId() == $productId) {
$item->delete();
$wish->save();
return 'Product removed from wishlist';
}
}
}
}