0

I want to add to wishlist for guest user i want to store selected product custom options i have added a plugin on Add to Wishlist Button

<type name="Magento\Wishlist\Controller\Index\Plugin">
        <plugin name="guest_wishlist_plugin" type="Vendor\Module\Plugin\GuestPlugin" sortOrder="10"/>
    </type>

i am getting Custom Options empty.. i don't know what is the reason

Array
(
    [product] => 1754
    [uenc] => aHR0cDovL2xvY2FsaG9zdC9ndWVzdHdpc2hsaXN0MjMzL3dvbWVuL3RvcHMtd29tZW4vdGFua3Mtd29tZW4uaHRtbA,,
    [super_attribute] => Array
        (
            [142] => 
            [93] => 
        )

    [form_key] => f4ny48SW4tSwUDPX
)

Here is My plugin Code

use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;

class GuestPlugin
{
    /**
     * Name of cookie that holds private content version
     */
    const COOKIE_NAME = 'cookie_wishlist';

    /**
     * CookieManager
     *
     * @var CookieManagerInterface
     */
    private $cookieManager;

    /**
     * @var CookieMetadataFactory
     */
    private $cookieMetadataFactory;

    /**
     * @var SessionManagerInterface
     */
    private $sessionManager;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @var \Magento\Customer\Model\Session
     */
    private $customSession;

    private $resultFactory;

    protected $registry;

    public function __construct(
        Context $context,
        \Vendor\Module\Helper\Data $helper,
        ResultFactory $resultFactory,
        CookieManagerInterface $cookieManager,
        CookieMetadataFactory $cookieMetadataFactory,
        SessionManagerInterface $sessionManager,
        CustomerSession $customerSession
    ) {
        $this->resultRedirectFactory = $context->getResultRedirectFactory();
        $this->customerSession       = $customerSession;
        $this->cookieManager         = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
        $this->sessionManager        = $sessionManager;
        $this->helper                = $helper;
        $this->resultFactory         = $resultFactory;
    }
    public function beforeBeforeDispatch(
        \Magento\Wishlist\Controller\Index\Plugin $coreSubject,
        \Magento\Framework\App\ActionInterface $subject,
        RequestInterface $request
    ) {

        $obj = $request->getParams();
        if (!empty($obj['product'])) {
            if (!$this->customerSession->isLoggedIn()) {
                $cookieValue = explode(', ', $this->cookieManager->getCookie(self::COOKIE_NAME));
                if (empty($cookieValue)) {
                    $cookieValue = array();
                }
                array_push($cookieValue, $obj['product']);
                $this->set(implode(', ', $cookieValue), 100);
            }
        }
    }
private function set($value, $duration = 86400)
    {
        $metadata = $this->cookieMetadataFactory
            ->createPublicCookieMetadata()
            ->setDuration($duration)
            ->setPath($this->sessionManager->getCookiePath())
            ->setDomain($this->sessionManager->getCookieDomain());
        $this->cookieManager->setPublicCookie(
            self::COOKIE_NAME,
            $value,
            $metadata
        );
    }
}
  • Can you please upload your plugin code? – Rohan Hapani Nov 4 '19 at 10:21
  • see updated question – Waqar Ali Nov 4 '19 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.