0

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

1 Answer 1

2

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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