Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCarrier.php
Go to the documentation of this file.
1 <?php
8 
11 
19 {
20  const DEBUG_KEYS_MASK = '****';
21 
27  protected $_code;
28 
34  protected $_rates;
35 
41  protected $_numBoxes = 1;
42 
48  protected $_freeMethod = 'free_method';
49 
55  protected $_isFixed = false;
56 
63 
64  const USA_COUNTRY_ID = 'US';
65 
66  const CANADA_COUNTRY_ID = 'CA';
67 
68  const MEXICO_COUNTRY_ID = 'MX';
69 
70  const HANDLING_TYPE_PERCENT = 'P';
71 
72  const HANDLING_TYPE_FIXED = 'F';
73 
75 
77 
84 
90  protected $_scopeConfig;
91 
95  protected $_rateErrorFactory;
96 
100  protected $_logger;
101 
108  public function __construct(
109  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
110  \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
111  \Psr\Log\LoggerInterface $logger,
112  array $data = []
113  ) {
114  parent::__construct($data);
115  $this->_scopeConfig = $scopeConfig;
116  $this->_rateErrorFactory = $rateErrorFactory;
117  $this->_logger = $logger;
118  }
119 
126  public function getConfigData($field)
127  {
128  if (empty($this->_code)) {
129  return false;
130  }
131  $path = 'carriers/' . $this->_code . '/' . $field;
132 
133  return $this->_scopeConfig->getValue(
134  $path,
135  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
136  $this->getStore()
137  );
138  }
139 
148  public function getConfigFlag($field)
149  {
150  if (empty($this->_code)) {
151  return false;
152  }
153  $path = 'carriers/' . $this->_code . '/' . $field;
154 
155  return $this->_scopeConfig->isSetFlag(
156  $path,
157  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
158  $this->getStore()
159  );
160  }
161 
170  public function requestToShipment($request)
171  {
172  return new \Magento\Framework\DataObject();
173  }
174 
183  public function returnOfShipment($request)
184  {
185  return new \Magento\Framework\DataObject();
186  }
187 
195  public function getContainerTypes(\Magento\Framework\DataObject $params = null)
196  {
197  return [];
198  }
199 
208  protected function _getAllowedContainers(\Magento\Framework\DataObject $params = null)
209  {
210  $containersAll = $this->getContainerTypesAll();
211  if (empty($containersAll)) {
212  return [];
213  }
214  if (empty($params)) {
215  return $containersAll;
216  }
217  $containersFilter = $this->getContainerTypesFilter();
218  $containersFiltered = [];
219  $method = $params->getMethod();
220  $countryShipper = $params->getCountryShipper();
221  $countryRecipient = $params->getCountryRecipient();
222 
223  if (empty($containersFilter)) {
224  return $containersAll;
225  }
226  if (!$params || !$method || !$countryShipper || !$countryRecipient) {
227  return $containersAll;
228  }
229 
230  if ($countryShipper == self::USA_COUNTRY_ID && $countryRecipient == self::USA_COUNTRY_ID) {
231  $direction = 'within_us';
232  } else {
233  if ($countryShipper == self::USA_COUNTRY_ID && $countryRecipient != self::USA_COUNTRY_ID) {
234  $direction = 'from_us';
235  } else {
236  return $containersAll;
237  }
238  }
239 
240  foreach ($containersFilter as $dataItem) {
241  $containers = $dataItem['containers'];
242  $filters = $dataItem['filters'];
243  if (!empty($filters[$direction]['method']) && in_array($method, $filters[$direction]['method'])) {
244  foreach ($containers as $container) {
245  if (!empty($containersAll[$container])) {
246  $containersFiltered[$container] = $containersAll[$container];
247  }
248  }
249  }
250  }
251 
252  return !empty($containersFiltered) ? $containersFiltered : $containersAll;
253  }
254 
261  {
263  }
264 
272  public function getDeliveryConfirmationTypes(\Magento\Framework\DataObject $params = null)
273  {
274  return [];
275  }
276 
282  public function checkAvailableShipCountries(\Magento\Framework\DataObject $request)
283  {
284  $speCountriesAllow = $this->getConfigData('sallowspecific');
285  /*
286  * for specific countries, the flag will be 1
287  */
288  if ($speCountriesAllow && $speCountriesAllow == 1) {
289  $showMethod = $this->getConfigData('showmethod');
290  $availableCountries = [];
291  if ($this->getConfigData('specificcountry')) {
292  $availableCountries = explode(',', $this->getConfigData('specificcountry'));
293  }
294  if ($availableCountries && in_array($request->getDestCountryId(), $availableCountries)) {
295  return $this;
296  } elseif ($showMethod && (!$availableCountries || $availableCountries && !in_array(
297  $request->getDestCountryId(),
298  $availableCountries
299  ))
300  ) {
302  $error = $this->_rateErrorFactory->create();
303  $error->setCarrier($this->_code);
304  $error->setCarrierTitle($this->getConfigData('title'));
305  $errorMsg = $this->getConfigData('specificerrmsg');
306  $error->setErrorMessage(
307  $errorMsg ? $errorMsg : __(
308  'Sorry, but we can\'t deliver to the destination country with this shipping module.'
309  )
310  );
311 
312  return $error;
313  } else {
314  /*
315  * The admin set not to show the shipping module if the delivery country
316  * is not within specific countries
317  */
318  return false;
319  }
320  }
321 
322  return $this;
323  }
324 
334  {
335  return $this;
336  }
337 
347  {
348  return $this->processAdditionalValidation($request);
349  }
350 
356  public function isActive()
357  {
358  $active = $this->getConfigData('active');
359 
360  return $active == 1 || $active == 'true';
361  }
362 
368  public function isFixed()
369  {
370  return $this->_isFixed;
371  }
372 
378  public function isTrackingAvailable()
379  {
380  return false;
381  }
382 
388  public function isShippingLabelsAvailable()
389  {
390  return false;
391  }
392 
398  public function getSortOrder()
399  {
400  return $this->getConfigData('sort_order');
401  }
402 
409  protected function _updateFreeMethodQuote($request)
410  {
411  if (!$request->getFreeShipping()) {
412  return;
413  }
414  if ($request->getFreeMethodWeight() == $request->getPackageWeight() || !$request->hasFreeMethodWeight()) {
415  return;
416  }
417 
418  $freeMethod = $this->getConfigData($this->_freeMethod);
419  if (!$freeMethod) {
420  return;
421  }
422  $freeRateId = false;
423 
424  if (is_object($this->_result)) {
425  foreach ($this->_result->getAllRates() as $i => $item) {
426  if ($item->getMethod() == $freeMethod) {
427  $freeRateId = $i;
428  break;
429  }
430  }
431  }
432 
433  if ($freeRateId === false) {
434  return;
435  }
436  $price = null;
437  if ($request->getFreeMethodWeight() > 0) {
438  $this->_setFreeMethodRequest($freeMethod);
439 
440  $result = $this->_getQuotes();
441  if ($result && ($rates = $result->getAllRates()) && count($rates) > 0) {
442  if (count($rates) == 1 && $rates[0] instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method) {
443  $price = $rates[0]->getPrice();
444  }
445  if (count($rates) > 1) {
446  foreach ($rates as $rate) {
447  if ($rate instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method &&
448  $rate->getMethod() == $freeMethod
449  ) {
450  $price = $rate->getPrice();
451  }
452  }
453  }
454  }
455  } else {
460  $price = 0;
461  }
462 
466  if ($price !== null) {
467  $this->_result->getRateById($freeRateId)->setPrice($price);
468  }
469  }
470 
477  public function getFinalPriceWithHandlingFee($cost)
478  {
479  $handlingFee = (float)$this->getConfigData('handling_fee');
480  $handlingType = $this->getConfigData('handling_type');
481  if (!$handlingType) {
482  $handlingType = self::HANDLING_TYPE_FIXED;
483  }
484  $handlingAction = $this->getConfigData('handling_action');
485  if (!$handlingAction) {
486  $handlingAction = self::HANDLING_ACTION_PERORDER;
487  }
488 
489  return $handlingAction == self::HANDLING_ACTION_PERPACKAGE ? $this->_getPerpackagePrice(
490  $cost,
491  $handlingType,
492  $handlingFee
493  ) : $this->_getPerorderPrice(
494  $cost,
495  $handlingType,
496  $handlingFee
497  );
498  }
499 
508  protected function _getPerpackagePrice($cost, $handlingType, $handlingFee)
509  {
510  if ($handlingType == self::HANDLING_TYPE_PERCENT) {
511  return ($cost + $cost * $handlingFee / 100) * $this->_numBoxes;
512  }
513 
514  return ($cost + $handlingFee) * $this->_numBoxes;
515  }
516 
525  protected function _getPerorderPrice($cost, $handlingType, $handlingFee)
526  {
527  if ($handlingType == self::HANDLING_TYPE_PERCENT) {
528  return $cost * $this->_numBoxes + $cost * $this->_numBoxes * $handlingFee / 100;
529  }
530 
531  return $cost * $this->_numBoxes + $handlingFee;
532  }
533 
540  public function getTotalNumOfBoxes($weight)
541  {
542  /*
543  reset num box first before retrieve again
544  */
545  $this->_numBoxes = 1;
546  $maxPackageWeight = $this->getConfigData('max_package_weight');
547  if ($weight > $maxPackageWeight && $maxPackageWeight != 0) {
548  $this->_numBoxes = ceil($weight / $maxPackageWeight);
549  $weight = $weight / $this->_numBoxes;
550  }
551 
552  return $weight;
553  }
554 
560  public function isStateProvinceRequired()
561  {
562  return false;
563  }
564 
570  public function isCityRequired()
571  {
572  return false;
573  }
574 
582  public function isZipCodeRequired($countryId = null)
583  {
584  return false;
585  }
586 
593  protected function _debug($debugData)
594  {
595  if ($this->getDebugFlag()) {
596  $this->_logger->debug(var_export($debugData, true));
597  }
598  }
599 
607  public function getDebugFlag()
608  {
609  return $this->getConfigData('debug');
610  }
611 
618  public function debugData($debugData)
619  {
620  $this->_debug($debugData);
621  }
622 
628  public function getCarrierCode()
629  {
630  return $this->_code;
631  }
632 
640  public function getContentTypes(\Magento\Framework\DataObject $params)
641  {
642  return [];
643  }
644 
663  protected function filterDebugData($data)
664  {
665  try {
666  $xml = new \SimpleXMLElement($data);
667  $this->filterXmlData($xml);
668  $data = $xml->asXML();
669  } catch (\Exception $e) {
670  }
671  return $data;
672  }
673 
679  private function filterXmlData(\SimpleXMLElement $xml)
680  {
682  foreach ($xml->children() as $child) {
683  if ($child->count()) {
684  $this->filterXmlData($child);
685  } elseif (in_array((string) $child->getName(), $this->_debugReplacePrivateDataKeys)) {
686  $child[0] = self::DEBUG_KEYS_MASK;
687  }
688  }
689  }
690 }
getDeliveryConfirmationTypes(\Magento\Framework\DataObject $params=null)
_getPerpackagePrice($cost, $handlingType, $handlingFee)
getContainerTypes(\Magento\Framework\DataObject $params=null)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_getPerorderPrice($cost, $handlingType, $handlingFee)
__()
Definition: __.php:13
$price
$rates
Definition: tax.phtml:35
$logger
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory, \Psr\Log\LoggerInterface $logger, array $data=[])
$method
Definition: info.phtml:13
_getAllowedContainers(\Magento\Framework\DataObject $params=null)
$filters
Definition: uploader.phtml:11
processAdditionalValidation(\Magento\Framework\DataObject $request)
checkAvailableShipCountries(\Magento\Framework\DataObject $request)
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
proccessAdditionalValidation(\Magento\Framework\DataObject $request)
$i
Definition: gallery.phtml:31
getContentTypes(\Magento\Framework\DataObject $params)