/** * 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; }
/** * 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(); } }
/** * 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); } } }
/** * 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); } } }
/** * 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; }
/** * 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; }
/** * 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); } }
/** * 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(); } } }
/** * 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(); }