skip to Main Content

if we click on "add to wish list" button, it’s displaying the "WISHLIST" page.

I don’t want to display the wishlist page. instead of that, I want to be on the same page. but the item should

"add to wishlist".

please help me to find a solution.

2

Answers


  1. You should customize the process of Magento add to the wishlist.
    Magento using dataPost.js for posting form of wishlist.

    You can change to use ajax to achieve it.

    Login or Signup to reply.
  2. You can override the Add Controller of Wishlist Module and change the redirect URL to current URL :

    Create a Module for example Wishlist/Example

    etc/di.xml

     <?xml version="1.0"?>
     <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
       <preference for="MagentoWishlistControllerIndexAdd" type="WishlistExampleControllerIndexAdd">
        </preference>
     </config>
    

    Under Controller/Index/Add.php file :

    <?php
     
    namespace WishlistExampleControllerIndex;
     
    use MagentoCatalogApiProductRepositoryInterface;
    use MagentoFrameworkAppAction;
    use MagentoFrameworkDataFormFormKeyValidator;
    use MagentoFrameworkExceptionNotFoundException;
    use MagentoFrameworkExceptionNoSuchEntityException;
    use MagentoFrameworkControllerResultFactory;
     
    /**
     * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
     */
    class Add extends MagentoWishlistControllerIndexAdd
    {
        /**
         * Adding new item
         *
         * @return MagentoFrameworkControllerResultRedirect
         * @throws NotFoundException
         * @SuppressWarnings(PHPMD.CyclomaticComplexity)
         * @SuppressWarnings(PHPMD.NPathComplexity)
         * @SuppressWarnings(PHPMD.UnusedLocalVariable)
         */
        public function execute()
        {
            /** @var MagentoFrameworkControllerResultRedirect $resultRedirect */
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            if (!$this->formKeyValidator->validate($this->getRequest())) {
                return $resultRedirect->setPath('*/');
            }
     
            $wishlist = $this->wishlistProvider->getWishlist();
            if (!$wishlist) {
                throw new NotFoundException(__('Page not found.'));
            }
     
            $session = $this->_customerSession;
     
            $requestParams = $this->getRequest()->getParams();
     
            if ($session->getBeforeWishlistRequest()) {
                $requestParams = $session->getBeforeWishlistRequest();
                $session->unsBeforeWishlistRequest();
            }
     
            $productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;
            if (!$productId) {
                $resultRedirect->setPath('*/');
                return $resultRedirect;
            }
     
            try {
                $product = $this->productRepository->getById($productId);
            } catch (NoSuchEntityException $e) {
                $product = null;
            }
     
            if (!$product || !$product->isVisibleInCatalog()) {
                $this->messageManager->addErrorMessage(__('We can't specify a product.'));
                $resultRedirect->setPath('*/');
                return $resultRedirect;
            }
     
            try {
                $buyRequest = new MagentoFrameworkDataObject($requestParams);
     
                $result = $wishlist->addNewItem($product, $buyRequest);
                if (is_string($result)) {
                    throw new MagentoFrameworkExceptionLocalizedException(__($result));
                }
                if ($wishlist->isObjectNew()) {
                    $wishlist->save();
                }
                $this->_eventManager->dispatch(
                    'wishlist_add_product',
                    ['wishlist' => $wishlist, 'product' => $product, 'item' => $result]
                );
     
                $referer = $session->getBeforeWishlistUrl();
                if ($referer) {
                    $session->setBeforeWishlistUrl(null);
                } else {
                    $referer = $this->_redirect->getRefererUrl();
                }
     
                $this->_objectManager->get(MagentoWishlistHelperData::class)->calculate();
     
                $this->messageManager->addComplexSuccessMessage(
                    'addProductSuccessMessage',
                    [
                        'product_name' => $product->getName(),
                        'referer' => $referer
                    ]
                );
            } catch (MagentoFrameworkExceptionLocalizedException $e) {
                $this->messageManager->addErrorMessage(
                    __('We can't add the item to Wish List right now: %1.', $e->getMessage())
                );
            } catch (Exception $e) {
                $this->messageManager->addExceptionMessage(
                    $e,
                    __('We can't add the item to Wish List right now.')
                );
            }
     
            $resultRedirect->setUrl($this->_redirect->getRefererUrl());
            return $resultRedirect;
        }
    }
    

    Here in the Controller all we change is

    $resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);
    

    By

    $resultRedirect->setUrl($this->_redirect->getRefererUrl());
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search