Skip to main content
Notice removed Draw attention by CommunityBot
Bounty Ended with no winning answer by CommunityBot
Notice added Draw attention by Vlad Patru
Bounty Started worth 50 reputation by Vlad Patru
added 117 characters in body
Source Link
Vlad Patru
  • 1.2k
  • 9
  • 24

LE:

Looks like the getItem method from WishlistFactory is returning NULL and I can't get to find out why

LE:

Looks like the getItem method from WishlistFactory is returning NULL and I can't get to find out why

` use Magento\Customer\Model\Session; use Magento\Framework\Controller\Result\JsonFactory; use Magento\Framework\Exception\NotFoundException; use Magento\Wishlist\Controller\WishlistProviderInterface; use Magento\Wishlist\Model\Item; use Magento\Wishlist\Model\Product\AttributeValueProvider; use Magento\Wishlist\Model\WishlistFactory;

/**

  • Class Remove

  • @package Codesicle\WishlistAjax\Plugin\Wishlist\Controller\Index / class Remove { /*

    • @var WishlistProviderInterface */ protected $wishlistProvider;

    /**

    • @var AttributeValueProvider */ private $attributeValueProvider;

    /**

    • @var Session */ protected $customerSession;

    /**

    • @var JsonFactory */ protected $jsonFactory;

    /**

    • @var WishlistFactory */ protected $wishlistFactory;

    public function __construct( WishlistProviderInterface $wishlistProvider, AttributeValueProvider $attributeValueProvider = null, JsonFactory $jsonFactory, Session $customerSession, WishlistFactory $wishlistFactory ) { $this->wishlistProvider = $wishlistProvider; $this->attributeValueProvider = $attributeValueProvider ?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class); $this->jsonFactory = $jsonFactory; $this->customerSession = $customerSession; $this->wishlistFactory = $wishlistFactory; }

    public function aroundExecute($subject, $proceed) {

     $id = (int)$this->getRequest()->getParam('item');
    
     $item = $subject->_objectManager->create(Item::class)->load($id);
    
     if (!$item->getId()) {
         throw new NotFoundException(__('Page not found.'));
     }
     $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
     if (!$wishlist) {
         throw new NotFoundException(__('Page not found.'));
     }
     try {
         $item->delete();
         $wishlist->save();
         $productName = $this->attributeValueProvider
             ->getRawAttributeValue($item->getProductId(), 'name');
    
         $response = [
             'errors' => false,
             'message' => __('%1 has been removed from your Wish List.', $productName)
         ];
     } catch (\Magento\Framework\Exception\LocalizedException $exception) {
         $response = [
             'errors' => true,
             'message' => __('We can\'t delete the item from Wish List right now because of an error: %1.', $exception->getMessage())
         ];
     } catch (\Exception $exception) {
         $response = [
             'errors' => true,
             'message' => __('We can\'t delete the item from the Wish List right now.')
         ];
     }
     $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
    
     $resultJson = $this->jsonFactory->create();
     return $resultJson->setData($response);
    

    }

 use Magento\Customer\Model\Session;
    use Magento\Framework\Controller\Result\JsonFactory;
    use Magento\Framework\Exception\NotFoundException;
    use Magento\Wishlist\Controller\WishlistProviderInterface;
    use Magento\Wishlist\Model\Item;
    use Magento\Wishlist\Model\Product\AttributeValueProvider;
    use Magento\Wishlist\Model\WishlistFactory;

/**
 * Class Remove
 * @package Codesicle\WishlistAjax\Plugin\Wishlist\Controller\Index
 */
class Remove
{
    /**
     * @var WishlistProviderInterface
     */
    protected $wishlistProvider;

    /**
     * @var AttributeValueProvider
     */
    private $attributeValueProvider;

 
    /**
     * @var Session
     */
    protected $customerSession;

    /**
     * @var JsonFactory
     */
    protected $jsonFactory;

    /**
     * @var WishlistFactory
     */
    protected $wishlistFactory;

    public function __construct(
        WishlistProviderInterface $wishlistProvider,
        AttributeValueProvider $attributeValueProvider = null,
        JsonFactory $jsonFactory,
        Session $customerSession,
        WishlistFactory $wishlistFactory
    )
    {
        $this->wishlistProvider = $wishlistProvider;
        $this->attributeValueProvider = $attributeValueProvider
            ?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class);
        $this->jsonFactory = $jsonFactory;
        $this->customerSession = $customerSession;
        $this->wishlistFactory = $wishlistFactory;
    }

    public function aroundExecute($subject, $proceed)
    {

        $id = (int)$this->getRequest()->getParam('item');

        $item = $subject->_objectManager->create(Item::class)->load($id);

        if (!$item->getId()) {
            throw new NotFoundException(__('Page not found.'));
        }
        $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
        if (!$wishlist) {
            throw new NotFoundException(__('Page not found.'));
        }
        try {
            $item->delete();
            $wishlist->save();
            $productName = $this->attributeValueProvider
                ->getRawAttributeValue($item->getProductId(), 'name');
           
            $response = [
                'errors' => false,
                'message' => __('%1 has been removed from your Wish List.', $productName)
            ];
        } catch (\Magento\Framework\Exception\LocalizedException $exception) {
            $response = [
                'errors' => true,
                'message' => __('We can\'t delete the item from Wish List right now because of an error: %1.', $exception->getMessage())
            ];
        } catch (\Exception $exception) {
            $response = [
                'errors' => true,
                'message' => __('We can\'t delete the item from the Wish List right now.')
            ];
        }
        $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
        
        $resultJson = $this->jsonFactory->create();
        return $resultJson->setData($response);
    }

` use Magento\Customer\Model\Session; use Magento\Framework\Controller\Result\JsonFactory; use Magento\Framework\Exception\NotFoundException; use Magento\Wishlist\Controller\WishlistProviderInterface; use Magento\Wishlist\Model\Item; use Magento\Wishlist\Model\Product\AttributeValueProvider; use Magento\Wishlist\Model\WishlistFactory;

/**

  • Class Remove

  • @package Codesicle\WishlistAjax\Plugin\Wishlist\Controller\Index / class Remove { /*

    • @var WishlistProviderInterface */ protected $wishlistProvider;

    /**

    • @var AttributeValueProvider */ private $attributeValueProvider;

    /**

    • @var Session */ protected $customerSession;

    /**

    • @var JsonFactory */ protected $jsonFactory;

    /**

    • @var WishlistFactory */ protected $wishlistFactory;

    public function __construct( WishlistProviderInterface $wishlistProvider, AttributeValueProvider $attributeValueProvider = null, JsonFactory $jsonFactory, Session $customerSession, WishlistFactory $wishlistFactory ) { $this->wishlistProvider = $wishlistProvider; $this->attributeValueProvider = $attributeValueProvider ?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class); $this->jsonFactory = $jsonFactory; $this->customerSession = $customerSession; $this->wishlistFactory = $wishlistFactory; }

    public function aroundExecute($subject, $proceed) {

     $id = (int)$this->getRequest()->getParam('item');
    
     $item = $subject->_objectManager->create(Item::class)->load($id);
    
     if (!$item->getId()) {
         throw new NotFoundException(__('Page not found.'));
     }
     $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
     if (!$wishlist) {
         throw new NotFoundException(__('Page not found.'));
     }
     try {
         $item->delete();
         $wishlist->save();
         $productName = $this->attributeValueProvider
             ->getRawAttributeValue($item->getProductId(), 'name');
    
         $response = [
             'errors' => false,
             'message' => __('%1 has been removed from your Wish List.', $productName)
         ];
     } catch (\Magento\Framework\Exception\LocalizedException $exception) {
         $response = [
             'errors' => true,
             'message' => __('We can\'t delete the item from Wish List right now because of an error: %1.', $exception->getMessage())
         ];
     } catch (\Exception $exception) {
         $response = [
             'errors' => true,
             'message' => __('We can\'t delete the item from the Wish List right now.')
         ];
     }
     $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
    
     $resultJson = $this->jsonFactory->create();
     return $resultJson->setData($response);
    

    }

 use Magento\Customer\Model\Session;
    use Magento\Framework\Controller\Result\JsonFactory;
    use Magento\Framework\Exception\NotFoundException;
    use Magento\Wishlist\Controller\WishlistProviderInterface;
    use Magento\Wishlist\Model\Item;
    use Magento\Wishlist\Model\Product\AttributeValueProvider;
    use Magento\Wishlist\Model\WishlistFactory;

/**
 * Class Remove
 * @package Codesicle\WishlistAjax\Plugin\Wishlist\Controller\Index
 */
class Remove
{
    /**
     * @var WishlistProviderInterface
     */
    protected $wishlistProvider;

    /**
     * @var AttributeValueProvider
     */
    private $attributeValueProvider;

 
    /**
     * @var Session
     */
    protected $customerSession;

    /**
     * @var JsonFactory
     */
    protected $jsonFactory;

    /**
     * @var WishlistFactory
     */
    protected $wishlistFactory;

    public function __construct(
        WishlistProviderInterface $wishlistProvider,
        AttributeValueProvider $attributeValueProvider = null,
        JsonFactory $jsonFactory,
        Session $customerSession,
        WishlistFactory $wishlistFactory
    )
    {
        $this->wishlistProvider = $wishlistProvider;
        $this->attributeValueProvider = $attributeValueProvider
            ?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class);
        $this->jsonFactory = $jsonFactory;
        $this->customerSession = $customerSession;
        $this->wishlistFactory = $wishlistFactory;
    }

    public function aroundExecute($subject, $proceed)
    {

        $id = (int)$this->getRequest()->getParam('item');

        $item = $subject->_objectManager->create(Item::class)->load($id);

        if (!$item->getId()) {
            throw new NotFoundException(__('Page not found.'));
        }
        $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
        if (!$wishlist) {
            throw new NotFoundException(__('Page not found.'));
        }
        try {
            $item->delete();
            $wishlist->save();
            $productName = $this->attributeValueProvider
                ->getRawAttributeValue($item->getProductId(), 'name');
           
            $response = [
                'errors' => false,
                'message' => __('%1 has been removed from your Wish List.', $productName)
            ];
        } catch (\Magento\Framework\Exception\LocalizedException $exception) {
            $response = [
                'errors' => true,
                'message' => __('We can\'t delete the item from Wish List right now because of an error: %1.', $exception->getMessage())
            ];
        } catch (\Exception $exception) {
            $response = [
                'errors' => true,
                'message' => __('We can\'t delete the item from the Wish List right now.')
            ];
        }
        $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
        
        $resultJson = $this->jsonFactory->create();
        return $resultJson->setData($response);
    }
Source Link
Vlad Patru
  • 1.2k
  • 9
  • 24

Wishlist ajax remove

I want to remove product from wishlist from a custom button on detail. I did a plugin for the Magento\Wishlist\Controller\Index\Remove.php with an aroundExecute but for some reason I can't load/create products as magento does in the default Remove controller. I posted my file

` use Magento\Customer\Model\Session; use Magento\Framework\Controller\Result\JsonFactory; use Magento\Framework\Exception\NotFoundException; use Magento\Wishlist\Controller\WishlistProviderInterface; use Magento\Wishlist\Model\Item; use Magento\Wishlist\Model\Product\AttributeValueProvider; use Magento\Wishlist\Model\WishlistFactory;

/**

  • Class Remove

  • @package Codesicle\WishlistAjax\Plugin\Wishlist\Controller\Index / class Remove { /*

    • @var WishlistProviderInterface */ protected $wishlistProvider;

    /**

    • @var AttributeValueProvider */ private $attributeValueProvider;

    /**

    • @var Session */ protected $customerSession;

    /**

    • @var JsonFactory */ protected $jsonFactory;

    /**

    • @var WishlistFactory */ protected $wishlistFactory;

    public function __construct( WishlistProviderInterface $wishlistProvider, AttributeValueProvider $attributeValueProvider = null, JsonFactory $jsonFactory, Session $customerSession, WishlistFactory $wishlistFactory ) { $this->wishlistProvider = $wishlistProvider; $this->attributeValueProvider = $attributeValueProvider ?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class); $this->jsonFactory = $jsonFactory; $this->customerSession = $customerSession; $this->wishlistFactory = $wishlistFactory; }

    public function aroundExecute($subject, $proceed) {

     $id = (int)$this->getRequest()->getParam('item');
    
     $item = $subject->_objectManager->create(Item::class)->load($id);
    
     if (!$item->getId()) {
         throw new NotFoundException(__('Page not found.'));
     }
     $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
     if (!$wishlist) {
         throw new NotFoundException(__('Page not found.'));
     }
     try {
         $item->delete();
         $wishlist->save();
         $productName = $this->attributeValueProvider
             ->getRawAttributeValue($item->getProductId(), 'name');
    
         $response = [
             'errors' => false,
             'message' => __('%1 has been removed from your Wish List.', $productName)
         ];
     } catch (\Magento\Framework\Exception\LocalizedException $exception) {
         $response = [
             'errors' => true,
             'message' => __('We can\'t delete the item from Wish List right now because of an error: %1.', $exception->getMessage())
         ];
     } catch (\Exception $exception) {
         $response = [
             'errors' => true,
             'message' => __('We can\'t delete the item from the Wish List right now.')
         ];
     }
     $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
    
     $resultJson = $this->jsonFactory->create();
     return $resultJson->setData($response);
    

    }

What am I missing or what did I do wrong?