1

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';
            }
        }
    }

}
1
  • Please check customer id get it in your function Commented Jul 3, 2020 at 6:55

1 Answer 1

0

It can be because of two reason

  1. the given customer id has no wish list
  2. or the wishlist instance is not created correctly. You should always get fresh instance.

Try changing your code to

public function removeWishlistForCustomer($customerId, $productId)
{
    $wish = $this->wishlistFactory->create()
                ->loadByCustomerId($customerId, true);
    $items = $wish->getItemCollection()->load();
    foreach ($items as $item) {
        if ($item->getProductId() == $productId) {
            $item->delete();
            return 'Product removed from wishlist';
        }
    }
}
1
  • Thanks for the response, i will check Commented Jul 4, 2020 at 4:32

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.