/**
  * @return void
  */
 public function execute()
 {
     $currentActiveStore = $this->storeManager->getStore();
     $storeCode = $this->_request->getParam(StoreResolver::PARAM_NAME, $this->storeCookieManager->getStoreCodeFromCookie());
     try {
         $store = $this->storeRepository->getActiveStoreByCode($storeCode);
     } catch (StoreIsInactiveException $e) {
         $error = __('Requested store is inactive');
     } catch (NoSuchEntityException $e) {
         $error = __('Requested store is not found');
     }
     if (isset($error)) {
         $this->messageManager->addError($error);
         $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
         return;
     }
     $defaultStoreView = $this->storeManager->getDefaultStoreView();
     if ($defaultStoreView->getId() == $store->getId()) {
         $this->storeCookieManager->deleteStoreCookie($store);
     } else {
         $this->httpContext->setValue(Store::ENTITY, $store->getCode(), $defaultStoreView->getCode());
         $this->storeCookieManager->setStoreCookie($store);
     }
     if ($store->isUseStoreInUrl()) {
         // Change store code in redirect url
         if (strpos($this->_redirect->getRedirectUrl(), $currentActiveStore->getBaseUrl()) !== false) {
             $this->getResponse()->setRedirect(str_replace($currentActiveStore->getBaseUrl(), $store->getBaseUrl(), $this->_redirect->getRedirectUrl()));
         } else {
             $this->getResponse()->setRedirect($store->getBaseUrl());
         }
     } else {
         $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
     }
 }
Exemple #2
0
 /**
  * @param \Magento\Framework\App\Action\Action $subject
  * @param callable $proceed
  * @param \Magento\Framework\App\RequestInterface $request
  * @return mixed
  */
 public function aroundDispatch(\Magento\Framework\App\Action\Action $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request)
 {
     $defaultStore = $this->storeManager->getWebsite()->getDefaultStore();
     $this->httpContext->setValue(\Magento\Core\Helper\Data::CONTEXT_CURRENCY, $this->session->getCurrencyCode(), $defaultStore->getDefaultCurrency()->getCode());
     $this->httpContext->setValue(\Magento\Core\Helper\Data::CONTEXT_STORE, $this->httpRequest->getParam('___store', $defaultStore->getStoreCodeFromCookie()), $this->storeManager->getWebsite()->getDefaultStore()->getCode());
     return $proceed($request);
 }
 /**
  * @param \Magento\Framework\App\Action\Action $subject
  * @param callable $proceed
  * @param \Magento\Framework\App\RequestInterface $request
  * @return mixed
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundDispatch(\Magento\Framework\App\Action\Action $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request)
 {
     $this->httpContext->setValue(Data::CONTEXT_CATALOG_SORT_DIRECTION, $this->toolbarModel->getDirection(), \Magento\Catalog\Helper\Product\ProductList::DEFAULT_SORT_DIRECTION);
     $this->httpContext->setValue(Data::CONTEXT_CATALOG_SORT_ORDER, $this->toolbarModel->getOrder(), $this->productListHelper->getDefaultSortField());
     $this->httpContext->setValue(Data::CONTEXT_CATALOG_DISPLAY_MODE, $this->toolbarModel->getMode(), $this->productListHelper->getDefaultViewMode());
     $this->httpContext->setValue(Data::CONTEXT_CATALOG_LIMIT, $this->toolbarModel->getLimit(), $this->productListHelper->getDefaultLimitPerPageValue($this->productListHelper->getDefaultViewMode()));
     return $proceed($request);
 }
Exemple #4
0
 public function testSendVary()
 {
     $vary = array('some-vary-key' => 'some-vary-value');
     $expected = sha1(serialize($vary));
     $this->_context->setValue('some-vary-key', 'some-vary-value', 'default');
     $this->_cookieMock->expects($this->once())->method('set')->with(Http::COOKIE_VARY_STRING, $expected);
     $this->_model->sendVary();
 }
 public function testGetData()
 {
     $this->object->setValue('key1', 'value1', 'default1');
     $this->object->setValue('key2', 'value2', 'default2');
     $this->object->setValue('key3', 'value3', 'value3');
     $this->object->unsValue('key1');
     $this->assertEquals(['key2' => 'value2'], $this->object->getData());
 }
 public function testGetVaryString()
 {
     $this->object->setValue('key2', 'value2', 'default2');
     $this->object->setValue('key1', 'value1', 'default1');
     $data = ['key2' => 'value2', 'key1' => 'value1'];
     ksort($data);
     $this->assertEquals(sha1(serialize($data)), $this->object->getVaryString());
 }
 /**
  * @param \Magento\Framework\App\ActionInterface $subject
  * @param callable $proceed
  * @param \Magento\Framework\App\RequestInterface $request
  * @return mixed
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundDispatch(\Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request)
 {
     /** @var \Magento\Store\Model\Store $defaultStore */
     $defaultStore = $this->storeManager->getWebsite()->getDefaultStore();
     $requestedStoreCode = $this->httpRequest->getParam(StoreResolverInterface::PARAM_NAME, $this->storeCookieManager->getStoreCodeFromCookie());
     /** @var \Magento\Store\Model\Store $currentStore */
     $currentStore = $requestedStoreCode ? $this->storeManager->getStore($requestedStoreCode) : $defaultStore;
     $this->httpContext->setValue(StoreManagerInterface::CONTEXT_STORE, $currentStore->getCode(), $this->storeManager->getDefaultStoreView()->getCode());
     $this->httpContext->setValue(HttpContext::CONTEXT_CURRENCY, $this->session->getCurrencyCode() ?: $currentStore->getDefaultCurrencyCode(), $defaultStore->getDefaultCurrencyCode());
     return $proceed($request);
 }
 /**
  * @param \Magento\Framework\App\ActionInterface $subject
  * @param callable $proceed
  * @param \Magento\Framework\App\RequestInterface $request
  * @return mixed
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundDispatch(\Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request)
 {
     if (!$this->customerSession->isLoggedIn() || !$this->moduleManager->isEnabled('Magento_PageCache') || !$this->cacheConfig->isEnabled() || !$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) {
         return $proceed($request);
     }
     $defaultBillingAddress = $this->customerSession->getDefaultTaxBillingAddress();
     $defaultShippingAddress = $this->customerSession->getDefaultTaxShippingAddress();
     $customerTaxClassId = $this->customerSession->getCustomerTaxClassId();
     if (!empty($defaultBillingAddress) || !empty($defaultShippingAddress)) {
         $taxRates = $this->taxCalculation->getTaxRates($defaultBillingAddress, $defaultShippingAddress, $customerTaxClassId);
         $this->httpContext->setValue('tax_rates', $taxRates, 0);
     }
     return $proceed($request);
 }
Exemple #9
0
    /**
     * @param \Magento\Framework\App\ActionInterface $subject
     * @param callable $proceed
     * @param \Magento\Framework\App\RequestInterface $request
     * @return mixed
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function aroundExecute(
        \Magento\Framework\App\ActionInterface $subject,
        \Closure $proceed,
        \Magento\Framework\App\RequestInterface $request
    ) {
        if (!$this->weeeHelper->isEnabled() ||
            !$this->customerSession->isLoggedIn() ||
            !$this->moduleManager->isEnabled('Magento_PageCache') ||
            !$this->cacheConfig->isEnabled()) {
            return $proceed($request);
        }

        $basedOn = $this->taxHelper->getTaxBasedOn();
        if ($basedOn != 'shipping' && $basedOn != 'billing') {
            return $proceed($request);
        }

        $weeeTaxRegion = $this->getWeeeTaxRegion($basedOn);
        $websiteId = $this->storeManager->getStore()->getWebsiteId();
        $countryId = $weeeTaxRegion['countryId'];
        $regionId = $weeeTaxRegion['regionId'];

        if (!$countryId && !$regionId) {
            // country and region does not exist
            return $proceed($request);
        } else if ($countryId && !$regionId) {
            // country exist and region does not exist
            $regionId = 0;
            $exist = $this->weeeTax->isWeeeInLocation(
                $countryId,
                $regionId,
                $websiteId
            );
        } else {
            // country and region exist
            $exist = $this->weeeTax->isWeeeInLocation(
                $countryId,
                $regionId,
                $websiteId
            );
            if (!$exist) {
                // just check the country for weee
                $regionId = 0;
                $exist = $this->weeeTax->isWeeeInLocation(
                    $countryId,
                    $regionId,
                    $websiteId
                );
            }
        }

        if ($exist) {
            $this->httpContext->setValue(
                'weee_tax_region',
                ['countryId' => $countryId, 'regionId' => $regionId],
                0
            );
        }
        return $proceed($request);
    }
Exemple #10
0
 /**
  * @param \Magento\Framework\App\ActionInterface $subject
  * @param callable $proceed
  * @param \Magento\Framework\App\RequestInterface $request
  * @return mixed
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundDispatch(\Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request)
 {
     /** @var \Magento\Store\Model\Store $defaultStore */
     $defaultStore = $this->storeManager->getWebsite()->getDefaultStore();
     $storeCode = $this->httpRequest->getParam(StoreResolverInterface::PARAM_NAME, $this->storeCookieManager->getStoreCodeFromCookie());
     if (is_array($storeCode)) {
         if (!isset($storeCode['_data']['code'])) {
             throw new \InvalidArgumentException(new Phrase('Invalid store parameter.'));
         }
         $storeCode = $storeCode['_data']['code'];
     }
     /** @var \Magento\Store\Model\Store $currentStore */
     $currentStore = $storeCode ? $this->storeManager->getStore($storeCode) : $defaultStore;
     $this->httpContext->setValue(StoreManagerInterface::CONTEXT_STORE, $currentStore->getCode(), $this->storeManager->getDefaultStoreView()->getCode());
     $this->httpContext->setValue(HttpContext::CONTEXT_CURRENCY, $this->session->getCurrencyCode() ?: $currentStore->getDefaultCurrencyCode(), $defaultStore->getDefaultCurrencyCode());
     return $proceed($request);
 }
Exemple #11
0
 /**
  * @param CustomerData $customer
  * @return $this
  */
 public function setCustomerDataAsLoggedIn($customer)
 {
     $this->_httpContext->setValue(Context::CONTEXT_AUTH, true, false);
     $this->setCustomerData($customer);
     $customerModel = $this->_customerFactory->create()->updateData($customer);
     $this->setCustomer($customerModel);
     $this->_eventManager->dispatch('customer_login', ['customer' => $customerModel]);
     $this->_eventManager->dispatch('customer_data_object_login', ['customer' => $customer]);
     return $this;
 }
 /**
  * @param \Magento\Store\Model\StoreManagerInterface $storage
  * @param string $scopeType
  * @return void
  */
 protected function _checkRequestStore(\Magento\Store\Model\StoreManagerInterface $storage, $scopeType)
 {
     $storeCode = $this->request->getParam('___store');
     if (empty($storeCode)) {
         return;
     }
     if (!$this->setCurrentStore($storage, $storeCode, $scopeType)) {
         return;
     }
     if ($storage->getStore()->getCode() == $storeCode) {
         $store = $storage->getStore($storeCode);
         if ($store->getWebsite()->getDefaultStore()->getId() == $store->getId()) {
             $this->_cookie->set(Store::COOKIE_NAME, null);
         } else {
             $this->_cookie->set(Store::COOKIE_NAME, $storage->getStore()->getCode(), true);
             $this->_httpContext->setValue(Store::ENTITY, $storage->getStore()->getCode(), \Magento\Store\Model\Store::DEFAULT_CODE);
         }
     }
     return;
 }
Exemple #13
0
 /**
  * @param \Magento\Store\Model\StoreManagerInterface $storage
  * @param string $scopeType
  * @return void
  */
 protected function _checkRequestStore(\Magento\Store\Model\StoreManagerInterface $storage, $scopeType)
 {
     $storeCode = $this->request->getParam('___store');
     if (empty($storeCode)) {
         return;
     }
     if (!$this->setCurrentStore($storage, $storeCode, $scopeType)) {
         return;
     }
     if ($storage->getStore()->getCode() == $storeCode) {
         $store = $storage->getStore($storeCode);
         if ($store->getWebsite()->getDefaultStore()->getId() == $store->getId()) {
             $this->_cookieManager->deleteCookie(Store::COOKIE_NAME);
         } else {
             $publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()->setDurationOneYear();
             $this->_cookieManager->setPublicCookie(Store::COOKIE_NAME, $storage->getStore()->getCode(), $publicCookieMetadata);
             $this->_httpContext->setValue(Store::ENTITY, $storage->getStore()->getCode(), \Magento\Store\Model\Store::DEFAULT_CODE);
         }
     }
     return;
 }
 /**
  * Test welcome message when customer is logged in
  */
 public function testGetWelcomeLoggedIn()
 {
     $this->context->setValue(Context::CONTEXT_AUTH, true, false);
     $this->assertEquals('Welcome, John Smith!', $this->block->getWelcome());
 }
Exemple #15
0
 /**
  * Set current store currency code
  *
  * @param   string $code
  * @return  string
  */
 public function setCurrentCurrencyCode($code)
 {
     $code = strtoupper($code);
     if (in_array($code, $this->getAvailableCurrencyCodes())) {
         $this->_getSession()->setCurrencyCode($code);
         $defaultCode = $this->_storeManager->getWebsite()->getDefaultStore()->getDefaultCurrency()->getCode();
         $this->_httpContext->setValue(Context::CONTEXT_CURRENCY, $code, $defaultCode);
     }
     return $this;
 }
Exemple #16
0
 /**
  * @param CustomerData $customer
  * @return $this
  */
 public function setCustomerDataAsLoggedIn($customer)
 {
     $this->_httpContext->setValue(\Magento\Customer\Helper\Data::CONTEXT_AUTH, true, false);
     $this->setCustomerData($customer);
     $customerModel = $this->_converter->createCustomerModel($customer);
     $this->setCustomer($customerModel);
     $this->_eventManager->dispatch('customer_login', array('customer' => $customerModel));
     $this->_eventManager->dispatch('customer_data_object_login', ['customer' => $customer]);
     return $this;
 }
Exemple #17
0
 /**
  * Set current store currency code
  *
  * @param   string $code
  * @return  string
  */
 public function setCurrentCurrencyCode($code)
 {
     $code = strtoupper($code);
     if (in_array($code, $this->getAvailableCurrencyCodes())) {
         $this->_getSession()->setCurrencyCode($code);
         $path = $this->_getSession()->getCookiePath();
         if ($code == $this->getDefaultCurrency()->getCurrencyCode()) {
             $this->_cookie->set(self::COOKIE_CURRENCY, null, null, $path);
         } else {
             $this->_cookie->set(self::COOKIE_CURRENCY, $code, null, $path);
         }
         $this->_httpContext->setValue(\Magento\Core\Helper\Data::CONTEXT_CURRENCY, $code, $this->_storeManager->getWebsite()->getDefaultStore()->getDefaultCurrency()->getCode());
     }
     return $this;
 }
 /**
  * @param \Magento\Framework\App\ActionInterface $subject
  * @param callable $proceed
  * @param \Magento\Framework\App\RequestInterface $request
  * @return mixed
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function aroundDispatch(\Magento\Framework\App\ActionInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request)
 {
     $this->httpContext->setValue(Context::CONTEXT_GROUP, $this->customerSession->getCustomerGroupId(), GroupManagement::NOT_LOGGED_IN_ID);
     $this->httpContext->setValue(Context::CONTEXT_AUTH, $this->customerSession->isLoggedIn(), false);
     return $proceed($request);
 }
Exemple #19
0
 /**
  * Set current store currency code
  *
  * @param   string $code
  * @return  string
  */
 public function setCurrentCurrencyCode($code)
 {
     $code = strtoupper($code);
     if (in_array($code, $this->getAvailableCurrencyCodes())) {
         $this->_getSession()->setCurrencyCode($code);
         $path = $this->_getSession()->getCookiePath();
         $sensitiveCookieMetadata = $this->_cookieMetadataFactory->createSensitiveCookieMetadata()->setPath($path);
         if ($code == $this->getDefaultCurrency()->getCurrencyCode()) {
             $this->_cookieManager->deleteCookie(self::COOKIE_CURRENCY, $sensitiveCookieMetadata);
         } else {
             $this->_cookieManager->setSensitiveCookie(self::COOKIE_CURRENCY, $code, $sensitiveCookieMetadata);
         }
         $this->_httpContext->setValue(\Magento\Core\Helper\Data::CONTEXT_CURRENCY, $code, $this->_storeManager->getWebsite()->getDefaultStore()->getDefaultCurrency()->getCode());
     }
     return $this;
 }
Exemple #20
0
 /**
  * Test welcome message when customer is logged in
  */
 public function testGetWelcomeLoggedIn()
 {
     $this->context->setValue(\Magento\Customer\Helper\Data::CONTEXT_AUTH, true, false);
     $this->assertEquals('Welcome, Firstname Lastname!', $this->block->getWelcome());
 }