Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ContextPlugin.php
Go to the documentation of this file.
1 <?php
8 
14 {
18  protected $customerSession;
19 
23  protected $httpContext;
24 
28  protected $taxHelper;
29 
33  protected $weeeHelper;
34 
38  protected $moduleManager;
39 
43  protected $weeeTax;
44 
48  protected $cacheConfig;
49 
53  protected $storeManager;
54 
58  protected $scopeConfig;
59 
71  public function __construct(
72  \Magento\Customer\Model\Session $customerSession,
73  \Magento\Framework\App\Http\Context $httpContext,
74  \Magento\Weee\Model\Tax $weeeTax,
75  \Magento\Tax\Helper\Data $taxHelper,
76  \Magento\Weee\Helper\Data $weeeHelper,
77  \Magento\Framework\Module\Manager $moduleManager,
78  \Magento\PageCache\Model\Config $cacheConfig,
80  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
81  ) {
82  $this->customerSession = $customerSession;
83  $this->httpContext = $httpContext;
84  $this->weeeTax = $weeeTax;
85  $this->taxHelper = $taxHelper;
86  $this->weeeHelper = $weeeHelper;
87  $this->moduleManager = $moduleManager;
88  $this->cacheConfig = $cacheConfig;
89  $this->storeManager = $storeManager;
90  $this->scopeConfig = $scopeConfig;
91  }
92 
102  public function beforeDispatch(
103  \Magento\Framework\App\ActionInterface $subject,
105  ) {
106  if (!$this->weeeHelper->isEnabled() ||
107  !$this->customerSession->isLoggedIn() ||
108  !$this->moduleManager->isEnabled('Magento_PageCache') ||
109  !$this->cacheConfig->isEnabled()) {
110  return;
111  }
112 
113  $basedOn = $this->taxHelper->getTaxBasedOn();
114  if ($basedOn != 'shipping' && $basedOn != 'billing') {
115  return;
116  }
117 
118  $weeeTaxRegion = $this->getWeeeTaxRegion($basedOn);
119  $websiteId = $this->storeManager->getStore()->getWebsiteId();
120  $countryId = $weeeTaxRegion['countryId'];
121  $regionId = $weeeTaxRegion['regionId'];
122 
123  if (!$countryId && !$regionId) {
124  // country and region does not exist
125  return;
126  } elseif ($countryId && !$regionId) {
127  // country exist and region does not exist
128  $regionId = 0;
129  $exist = $this->weeeTax->isWeeeInLocation(
130  $countryId,
131  $regionId,
132  $websiteId
133  );
134  } else {
135  // country and region exist
136  $exist = $this->weeeTax->isWeeeInLocation(
137  $countryId,
138  $regionId,
139  $websiteId
140  );
141  if (!$exist) {
142  // just check the country for weee
143  $regionId = 0;
144  $exist = $this->weeeTax->isWeeeInLocation(
145  $countryId,
146  $regionId,
147  $websiteId
148  );
149  }
150  }
151 
152  if ($exist) {
153  $this->httpContext->setValue(
154  'weee_tax_region',
155  ['countryId' => $countryId, 'regionId' => $regionId],
156  0
157  );
158  }
159  }
160 
165  protected function getWeeeTaxRegion($basedOn)
166  {
167  $countryId = null;
168  $regionId = null;
169  $defaultCountryId = $this->scopeConfig->getValue(
170  \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_COUNTRY,
172  null
173  );
174  $defaultRegionId = $this->scopeConfig->getValue(
175  \Magento\Tax\Model\Config::CONFIG_XML_PATH_DEFAULT_REGION,
177  null
178  );
179 
180  if ($basedOn == 'shipping') {
181  $defaultShippingAddress = $this->customerSession->getDefaultTaxShippingAddress();
182  if (empty($defaultShippingAddress)) {
183  $countryId = $defaultCountryId;
184  $regionId = $defaultRegionId;
185  } else {
186  $countryId = $defaultShippingAddress['country_id'];
187  $regionId = $defaultShippingAddress['region_id'];
188  }
189  } elseif ($basedOn == 'billing') {
190  $defaultBillingAddress = $this->customerSession->getDefaultTaxBillingAddress();
191  if (empty($defaultBillingAddress)) {
192  $countryId = $defaultCountryId;
193  $regionId = $defaultRegionId;
194  } else {
195  $countryId = $defaultBillingAddress['country_id'];
196  $regionId = $defaultBillingAddress['region_id'];
197  }
198  }
199  return ['countryId' => $countryId, 'regionId' => $regionId];
200  }
201 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Customer\Model\Session $customerSession, \Magento\Framework\App\Http\Context $httpContext, \Magento\Weee\Model\Tax $weeeTax, \Magento\Tax\Helper\Data $taxHelper, \Magento\Weee\Helper\Data $weeeHelper, \Magento\Framework\Module\Manager $moduleManager, \Magento\PageCache\Model\Config $cacheConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
beforeDispatch(\Magento\Framework\App\ActionInterface $subject, \Magento\Framework\App\RequestInterface $request)