1

I am facing issue adding product in wishlist (magento version 2.2.3).

Here is scenario,

Step 1 :- Make sure customer not logged in.

Step 2 :- Go to product list page and click "Add to wishlist"

Step 3 :- it will redirected to login page.

Step 4 :- Now after login you can check product not added in wishlist.

1 Answer 1

0

Generate new FormKey and replace for oldRequestParams Wishlist

  1. vendor\magento\module-wishlist\Helper\Data.php
    just added below code in this file...

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\App\PageCache\FormKey as PageCacheFormKey;
use Magento\Framework\Session\Config\ConfigInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;

    public function refreshFormKey()
    {
         $beforeWishlistRequest = $this->_customerSession->getBeforeWishlistRequest();


    if ($beforeWishlistRequest !== null) {

        $formKeyData = ObjectManager::getInstance()->get(FormKey::class);

        //form_key is already empty
        $formKey = $formKeyData->getFormKey();

        $sessionConfig = ObjectManager::getInstance()->get(ConfigInterface::class);
        $cookieMetadata = ObjectManager::getInstance()->get(CookieMetadataFactory::class)->createPublicCookieMetadata();
        $cookieMetadata->setDomain($sessionConfig->getCookieDomain());
        $cookieMetadata->setPath($sessionConfig->getCookiePath());
        $cookieMetadata->setDuration($sessionConfig->getCookieLifetime());

        ObjectManager::getInstance()->get(PageCacheFormKey::class)->set(
            $formKey,
            $cookieMetadata
        );

        $beforeWishlistRequest['form_key'] = $formKey;
        $this->_customerSession->setBeforeWishlistRequest($beforeWishlistRequest);
        $this->_customerSession->setBeforeRequestParams($beforeWishlistRequest);
        $this->_customerSession->setBeforeModuleName('wishlist');
        $this->_customerSession->setBeforeControllerName('index');
        $this->_customerSession->setBeforeAction('add');
    }

    return $this;
}

2. vendor\magento\module-wishlist\Observer\CustomerLogin.php

public function execute(Observer $observer)
    {
        $this->wishlistData->refreshFormKey(); // add this line only
        $this->wishlistData->calculate();
    }

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.