Пример #1
0
 /**
  * Set persistent data to customer session
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  *
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isShoppingCartPersist()) {
         return $this;
     }
     if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn()) {
         /** @var  \Magento\Customer\Api\Data\CustomerInterface $customer */
         $customer = $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId());
         if ($defaultShipping = $customer->getDefaultShipping()) {
             /** @var  \Magento\Customer\Model\Data\Address $address */
             $address = $this->addressRepository->getById($defaultShipping);
             if ($address) {
                 $this->_customerSession->setDefaultTaxShippingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]);
             }
         }
         if ($defaultBilling = $customer->getDefaultBilling()) {
             $address = $this->addressRepository->getById($defaultBilling);
             if ($address) {
                 $this->_customerSession->setDefaultTaxBillingAddress(['country_id' => $address->getCountryId(), 'region_id' => $address->getRegion() ? $address->getRegionId() : null, 'postcode' => $address->getPostcode()]);
             }
         }
         $this->_customerSession->setCustomerId($customer->getId())->setCustomerGroupId($customer->getGroupId());
     }
     return $this;
 }
 /**
  * @param Observer $observer
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function execute(Observer $observer)
 {
     /** @var $customer \Magento\Customer\Api\Data\CustomerInterface */
     $customer = $observer->getEvent()->getCustomer();
     // Check if customer is valid (remove persistent cookie for invalid customer)
     if (!$customer || !$customer->getId() || !$this->_persistentSession->isRememberMeChecked()) {
         $this->_sessionFactory->create()->removePersistentCookie();
         return;
     }
     $persistentLifeTime = $this->_persistentData->getLifeTime();
     // Delete persistent session, if persistent could not be applied
     if ($this->_persistentData->isEnabled() && $persistentLifeTime <= 0) {
         // Remove current customer persistent session
         $this->_sessionFactory->create()->deleteByCustomerId($customer->getId());
         return;
     }
     /** @var $sessionModel \Magento\Persistent\Model\Session */
     $sessionModel = $this->_persistentSession->getSession();
     // Check if session is wrong or not exists, so create new session
     if (!$sessionModel->getId() || $sessionModel->getCustomerId() != $customer->getId()) {
         /** @var \Magento\Persistent\Model\Session $sessionModel */
         $sessionModel = $this->_sessionFactory->create();
         $sessionModel->setLoadExpired()->loadByCustomerId($customer->getId());
         if (!$sessionModel->getId()) {
             /** @var \Magento\Persistent\Model\Session $sessionModel */
             $sessionModel = $this->_sessionFactory->create();
             $sessionModel->setCustomerId($customer->getId())->save();
         }
         $this->_persistentSession->setSession($sessionModel);
     }
     // Set new cookie
     if ($sessionModel->getId()) {
         $sessionModel->setPersistentCookie($persistentLifeTime, $this->_customerSession->getCookiePath());
     }
 }
Пример #3
0
 /**
  * Reset quote reward point amount
  *
  * @param \Magento\Customer\CustomerData\Customer $subject
  * @param \Closure $proceed
  *
  * @return array
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundGetSectionData(\Magento\Customer\CustomerData\Customer $subject, \Closure $proceed)
 {
     /** unset customer first name  */
     if (!$this->customerSession->isLoggedIn() && $this->persistentData->isEnabled() && $this->persistentSession->isPersistent()) {
         return [];
     }
     return $proceed();
 }
 /**
  * Unload persistent session (if set in config)
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  * @param Observer $observer
  * @return void
  */
 public function execute(Observer $observer)
 {
     if (!$this->_persistentData->isEnabled() || !$this->_persistentData->getClearOnLogout()) {
         return;
     }
     $this->_sessionFactory->create()->removePersistentCookie();
     // Unset persistent session
     $this->_persistentSession->setSession(null);
 }
 /**
  * @param \Magento\Checkout\Model\DefaultConfigProvider $subject
  * @param array $result
  * @return array
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, array $result)
 {
     if ($this->persistentHelper->isEnabled() && $this->persistentSession->isPersistent() && !$this->customerSession->isLoggedIn()) {
         /** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */
         $quoteIdMask = $this->quoteIdMaskFactory->create();
         $result['quoteData']['entity_id'] = $quoteIdMask->load($this->checkoutSession->getQuote()->getId(), 'quote_id')->getMaskedId();
     }
     return $result;
 }
 /**
  * Set quote to be loaded even if not active
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if (!($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn() && !$this->_persistentData->isShoppingCartPersist())) {
         return;
     }
     if ($this->_checkoutSession) {
         $this->_checkoutSession->setLoadInactive();
     }
 }
 /**
  * Make persistent quote to be guest
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute($observer)
 {
     /** @var $action \Magento\Persistent\Controller\Index */
     $action = $observer->getEvent()->getControllerAction();
     if ($action instanceof \Magento\Persistent\Controller\Index) {
         if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn() || $this->_persistentData->isShoppingCartPersist()) {
             $this->quoteManager->setGuest(true);
         }
     }
 }
 /**
  * Process address additional data
  *
  * @param AddressAdditionalDataProcessor $subject
  * @param AddressAdditionalData $additionalData
  * @return void
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function beforeProcess(AddressAdditionalDataProcessor $subject, AddressAdditionalData $additionalData)
 {
     if (!$this->persistentHelper->isEnabled() || !$this->persistentHelper->isRememberMeEnabled()) {
         return;
     }
     $checkboxStatus = $additionalData->getExtensionAttributes()->getPersistentRememberMe();
     $isRememberMeChecked = empty($checkboxStatus) ? false : true;
     $this->persistentSession->setRememberMeChecked($isRememberMeChecked);
     $this->checkoutSession->setRememberMeChecked($isRememberMeChecked);
 }
Пример #9
0
 /**
  * Apply persistent data
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute($observer)
 {
     if (!$this->_persistentData->canProcess($observer) || !$this->_persistentSession->isPersistent() || $this->_customerSession->isLoggedIn()) {
         return $this;
     }
     /** @var \Magento\Persistent\Model\Persistent\Config $persistentConfig */
     $persistentConfig = $this->_persistentConfigFactory->create();
     $persistentConfig->setConfigFilePath($this->_persistentData->getPersistentConfigFilePath())->fire();
     return $this;
 }
 /**
  * Prevent clear checkout session
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     /** @var $action \Magento\Persistent\Controller\Index */
     $action = $observer->getEvent()->getControllerAction();
     if ($action instanceof \Magento\Persistent\Controller\Index) {
         if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn() || !$this->_persistentData->isShoppingCartPersist()) {
             $action->setClearCheckoutSession(false);
         }
     }
 }
Пример #11
0
 /**
  * Set persistent data to customer session
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute($observer)
 {
     if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isShoppingCartPersist()) {
         return $this;
     }
     if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn()) {
         $customer = $this->_customerAccountService->getCustomer($this->_persistentSession->getSession()->getCustomerId());
         $this->_customerSession->setCustomerId($customer->getId())->setCustomerGroupId($customer->getGroupId());
     }
     return $this;
 }
 /**
  * Renew persistent cookie
  *
  * @param Observer $observer
  * @return void
  */
 public function execute(Observer $observer)
 {
     if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isEnabled() || !$this->_persistentSession->isPersistent()) {
         return;
     }
     /** @var $request \Magento\Framework\App\RequestInterface */
     $request = $observer->getEvent()->getRequest();
     if ($this->_customerSession->isLoggedIn() || $request->getFullActionName() == 'customer_account_logout') {
         $this->_sessionFactory->create()->renewPersistentCookie($this->_persistentData->getLifeTime(), $this->_customerSession->getCookiePath());
     }
 }
 /**
  * Set persistent data to customer session
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isShoppingCartPersist()) {
         return $this;
     }
     if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn()) {
         $customer = $this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId());
         $this->_customerSession->setCustomerId($customer->getId())->setCustomerGroupId($customer->getGroupId());
     }
     return $this;
 }
 /**
  * Unset persistent cookie and make customer's quote as a guest
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute($observer)
 {
     if (!$this->_persistentData->canProcess($observer) || !$this->_persistentSession->isPersistent()) {
         return;
     }
     $this->_persistentSession->getSession()->removePersistentCookie();
     if (!$this->_customerSession->isLoggedIn()) {
         $this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
     }
     $this->quoteManager->setGuest();
 }
 /**
  * Check and clear session data if persistent session expired
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if (!$this->_persistentData->canProcess($observer)) {
         return;
     }
     if ($this->_persistentData->isEnabled() && !$this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn() && $this->_checkoutSession->getQuoteId() && !$observer->getControllerAction() instanceof \Magento\Checkout\Controller\Onepage) {
         $this->_eventManager->dispatch('persistent_session_expired');
         $this->quoteManager->expire();
         $this->_customerSession->setCustomerId(null)->setCustomerGroupId(null);
     }
 }
 /**
  * Synchronize persistent session info
  *
  * @param Observer $observer
  * @return void
  */
 public function execute(Observer $observer)
 {
     if (!$this->_persistentData->isEnabled() || !$this->_persistentSession->isPersistent()) {
         return;
     }
     /** @var $sessionModel \Magento\Persistent\Model\Session */
     $sessionModel = $this->_persistentSession->getSession();
     /** @var $request \Magento\Framework\App\RequestInterface */
     $request = $observer->getEvent()->getRequest();
     // Quote Id could be changed only by logged in customer
     if ($this->_customerSession->isLoggedIn() || $request && $request->getActionName() == 'logout' && $request->getControllerName() == 'account') {
         $sessionModel->save();
     }
 }
 /**
  * Set Checked status of "Remember Me"
  *
  * @param Observer $observer
  * @return void
  */
 public function execute(Observer $observer)
 {
     if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isEnabled() || !$this->_persistentData->isRememberMeEnabled()) {
         return;
     }
     /** @var $controllerAction \Magento\Framework\App\RequestInterface */
     $request = $observer->getEvent()->getRequest();
     if ($request) {
         $rememberMeCheckbox = $request->getPost('persistent_remember_me');
         $this->_persistentSession->setRememberMeChecked((bool) $rememberMeCheckbox);
         if ($request->getFullActionName() == 'checkout_onepage_saveBilling' || $request->getFullActionName() == 'customer_account_createpost') {
             $this->_checkoutSession->setRememberMeChecked((bool) $rememberMeCheckbox);
         }
     }
 }
 /**
  * Set persistent data into quote
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute($observer)
 {
     if (!$this->_persistentSession->isPersistent()) {
         return;
     }
     /** @var $quote \Magento\Quote\Model\Quote */
     $quote = $observer->getEvent()->getQuote();
     if (!$quote) {
         return;
     }
     if ($this->_persistentSession->isPersistent() && !$this->_customerSession->isLoggedIn() && !$this->_persistentData->isShoppingCartPersist() && $this->quoteManager->isPersistent()) {
         //Quote is not actual customer's quote, just persistent
         $quote->setIsActive(false)->setIsPersistent(true);
     }
 }
 /**
  * Set Checked status of "Remember Me"
  *
  * @param Observer $observer
  * @return void
  */
 public function execute(Observer $observer)
 {
     if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isEnabled() || !$this->_persistentData->isRememberMeEnabled()) {
         return;
     }
     /** @var $request \Magento\Framework\App\RequestInterface */
     $request = $observer->getEvent()->getRequest();
     if ($request && $request->isXmlHttpRequest()) {
         $requestData = [];
         $content = $request->getContent();
         if ($content) {
             $requestData = \Zend_Json::decode($content);
         }
         $isRememberMeChecked = empty($requestData['persistent_remember_me']) ? false : true;
         $this->_persistentSession->setRememberMeChecked((bool) $isRememberMeChecked);
     }
 }
Пример #20
0
 /**
  * Emulate quote by persistent data
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return void
  */
 public function execute($observer)
 {
     $stopActions = ['persistent_index_saveMethod', 'customer_account_createpost'];
     if (!$this->_persistentData->canProcess($observer) || !$this->_persistentSession->isPersistent() || $this->_customerSession->isLoggedIn()) {
         return;
     }
     $actionName = $observer->getEvent()->getRequest()->getFullActionName();
     if (in_array($actionName, $stopActions)) {
         return;
     }
     if ($this->_persistentData->isShoppingCartPersist()) {
         $this->_checkoutSession->setCustomerData($this->customerRepository->getById($this->_persistentSession->getSession()->getCustomerId()));
         if (!$this->_checkoutSession->hasQuote()) {
             $this->_checkoutSession->getQuote();
         }
     }
 }
Пример #21
0
 /**
  * Make quote to be guest
  *
  * @param bool $checkQuote Check quote to be persistent (not stolen)
  * @return void
  */
 public function setGuest($checkQuote = false)
 {
     /** @var $quote \Magento\Sales\Model\Quote */
     $quote = $this->checkoutSession->getQuote();
     if ($quote && $quote->getId()) {
         if ($checkQuote && !$this->persistentData->isShoppingCartPersist() && !$quote->getIsPersistent()) {
             $this->checkoutSession->clearQuote()->clearStorage();
             return;
         }
         $quote->getPaymentsCollection()->walk('delete');
         $quote->getAddressesCollection()->walk('delete');
         $this->_setQuotePersistent = false;
         $quote->setIsActive(true)->setCustomerId(null)->setCustomerEmail(null)->setCustomerFirstname(null)->setCustomerLastname(null)->setCustomerGroupId(\Magento\Customer\Service\V1\CustomerGroupServiceInterface::NOT_LOGGED_IN_ID)->setIsPersistent(false)->removeAllAddresses();
         //Create guest addresses
         $quote->getShippingAddress();
         $quote->getBillingAddress();
         $quote->collectTotals()->save();
     }
     $this->persistentSession->getSession()->removePersistentCookie();
 }
Пример #22
0
 /**
  * Set persistent data to customer session
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function emulateCustomer($observer)
 {
     if (!$this->_persistentData->canProcess($observer) || !$this->_isShoppingCartPersist()) {
         return $this;
     }
     if ($this->_isLoggedOut()) {
         $customer = $this->_getPersistentCustomer();
         $this->_customerSession->setCustomerId($customer->getId())->setCustomerGroupId($customer->getGroupId());
     }
     return $this;
 }
 /**
  * Apply persistent data to specific block
  *
  * @param \Magento\Framework\Event\Observer $observer
  * @return $this
  */
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
     if (!$this->_persistentSession->isPersistent() || $this->_customerSession->isLoggedIn()) {
         return $this;
     }
     /** @var $block \Magento\Framework\View\Element\AbstractBlock */
     $block = $observer->getEvent()->getBlock();
     if (!$block) {
         return $this;
     }
     $configFilePath = $observer->getEvent()->getConfigFilePath();
     if (!$configFilePath) {
         $configFilePath = $this->_persistentData->getPersistentConfigFilePath();
     }
     /** @var $persistentConfig \Magento\Persistent\Model\Persistent\Config */
     $persistentConfig = $this->_persistentConfigFactory->create();
     $persistentConfig->setConfigFilePath($configFilePath);
     foreach ($persistentConfig->getBlockConfigInfo(get_class($block)) as $persistentConfigInfo) {
         $persistentConfig->fireOne($persistentConfigInfo, $block);
     }
     return $this;
 }
Пример #24
0
 /**
  * Check if "Remember Me" checked
  *
  * @return bool
  */
 public function isRememberMeChecked()
 {
     if (is_null($this->_isRememberMeChecked)) {
         //Try to get from checkout session
         $isRememberMeChecked = $this->_checkoutSession->getRememberMeChecked();
         if (!is_null($isRememberMeChecked)) {
             $this->_isRememberMeChecked = $isRememberMeChecked;
             $this->_checkoutSession->unsRememberMeChecked();
             return $isRememberMeChecked;
         }
         return $this->_persistentData->isEnabled() && $this->_persistentData->isRememberMeEnabled() && $this->_persistentData->isRememberMeCheckedDefault();
     }
     return (bool) $this->_isRememberMeChecked;
 }
Пример #25
0
 /**
  * Get date-time before which persistent session is expired
  *
  * @param int|string|\Magento\Store\Model\Store $store
  * @return string
  */
 public function getExpiredBefore($store = null)
 {
     return gmdate('Y-m-d H:i:s', time() - $this->_persistentData->getLifeTime($store));
 }
 /**
  * {@inheritdoc}
  */
 public function getConfig()
 {
     $isCheckboxVisible = $this->persistentHelper->isEnabled() && $this->persistentHelper->isRememberMeEnabled();
     $isCheckboxChecked = $this->persistentHelper->isRememberMeCheckedDefault();
     return ['persistenceConfig' => ['isRememberMeCheckboxVisible' => $isCheckboxVisible, 'isRememberMeCheckboxChecked' => $isCheckboxChecked]];
 }
Пример #27
0
 public function testGetPersistentConfigFilePath()
 {
     $this->_modulesReader->expects($this->once())->method('getModuleDir')->with('etc', 'Magento_Persistent')->will($this->returnValue('path123'));
     $this->assertEquals('path123/persistent.xml', $this->_helper->getPersistentConfigFilePath());
 }
Пример #28
0
 /**
  * Is "Remember Me" checked
  *
  * @return bool
  */
 public function isRememberMeChecked()
 {
     return $this->_persistentData->isEnabled() && $this->_persistentData->isRememberMeEnabled() && $this->_persistentData->isRememberMeCheckedDefault();
 }