0

Using Custom Theme, when user is not logged in and try to add a product to wishlist, it is served up the login or create an account screen. Once the user logs in, the item user was trying to add to wishlist isn't appearing under My Wishlist in Account Dashboard and it says You have no items in your wish list. So now user has to go back and add.

Although when user is logged in and then try to add a product to wishlist, its working.

How do I add the product upon login?

1

1 Answer 1

2

I have patch for this issue. You need to override these two files.

  1. vendor/magento/module-wishlist/Helper/Data.php

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

Override Data.php and CustomerLogin.php with di.xml

app/code/{Vendor}/{Module}/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="Magento\Wishlist\Helper\Data" type="{Vendor}\{Module}\Helper\Wishlist\Data" />
    <preference for="Magento\Wishlist\Observer\CustomerLogin" type="{Vendor}\{Module}\Observer\Wishlist\CustomerLogin" />
</config>

app/code/{Vendor}/{Module}/Helper/Wishlist/Data.php

<?php

namespace Vendor\Module\Helper\Wishlist;

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;


class Data extends \Magento\Wishlist\Helper\Data
{
    /**
     * Refresh and set new form_key in session and cookie
     *
     * @return $this
     */
    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;
    }
}

Now remove generated folder and flush cache

3

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.