on the wishlist's page we have remove wishlist's button for each item, and we want to add a button that can remove all wish-list item all at once

thank you

up vote 0 down vote accepted

I create my own module with a specific route to solve this problem, and I am thinking another best way to extend the action like do it from a plugin or something else, here are the code

<?php
namespace XXX\RemoveAllWishList\Controller\Index;

use Magento\Framework\App\Action;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Controller\ResultFactory;
use Magento\Wishlist\Controller\WishlistProviderInterface;

class Index extends \Magento\Wishlist\Controller\AbstractIndex
{
    /**
     * @var WishlistProviderInterface
     */
    protected $wishlistProvider;

    /**
     * @var Validator
     */
    protected $formKeyValidator;

    /**
     * @param Action\Context $context
     * @param WishlistProviderInterface $wishlistProvider
     * @param Validator $formKeyValidator
     */
    public function __construct(
        Action\Context $context,
        WishlistProviderInterface $wishlistProvider,
        Validator $formKeyValidator
    ) {
        $this->wishlistProvider = $wishlistProvider;
        $this->formKeyValidator = $formKeyValidator;
        parent::__construct($context);
    }

    /**
     * Remove item
     * @return \Magento\Framework\Controller\Result\Redirect
     * @throws \Exception
     */
    public function execute()
    {
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

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

        if (!$wishlistId) {
            throw new NotFoundException(__('Page not found.'));
        }

        if($wishlistId) {
            $wishlist = $this->wishlistProvider->getWishlist($wishlistId);

            $wishlistCollection = $wishlist->getItemCollection();
            foreach ($wishlistCollection as $item) {
                $item->delete();
            }
            $wishlist->save();
        }
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());

        $request = $this->getRequest();
        $refererUrl = (string)$request->getServer('HTTP_REFERER');
        $url = (string)$request->getParam(\Magento\Framework\App\Response\RedirectInterface::PARAM_NAME_REFERER_URL);
        if ($url) {
            $refererUrl = $url;
        }
        if ($request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED) && $refererUrl) {
            $redirectUrl = $refererUrl;
        } else {
            $redirectUrl = $this->_redirect->getRedirectUrl($this->_url->getUrl('*/*'));
        }
        $resultRedirect->setUrl($redirectUrl);
        return $resultRedirect;
    }
}

Here is the main method to delete all the wishlist item

foreach ($wishlistCollection as $item) {
     $item->delete();
}
$wishlist->save();

Thank You.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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