Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Shipping Class Reference
Inheritance diagram for Shipping:
RateCollectorInterface Labels

Public Member Functions

 __construct (\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Shipping\Model\Config $shippingConfig, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Shipping\Model\CarrierFactory $carrierFactory, \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory, \Magento\Shipping\Model\Shipment\RequestFactory $shipmentRequestFactory, \Magento\Directory\Model\RegionFactory $regionFactory, \Magento\Framework\Math\Division $mathDivision, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, RateRequestFactory $rateRequestFactory=null)
 
 getResult ()
 
 setOrigData ($data)
 
 resetResult ()
 
 getConfig ()
 
 collectRates (\Magento\Quote\Model\Quote\Address\RateRequest $request)
 
 collectCarrierRates ($carrierCode, $request)
 
 setCarrierAvailabilityConfigField ($code='active')
 
- Public Member Functions inherited from RateCollectorInterface
 collectRates (RateRequest $request)
 

Protected Member Functions

 _makePieces ($items, $maxWeight)
 

Protected Attributes

 $_orig = null
 
 $_result = null
 
 $_availabilityConfigField = 'active'
 
 $_scopeConfig
 
 $_storeManager
 
 $_shippingConfig
 
 $_carrierFactory
 
 $_rateResultFactory
 
 $_shipmentRequestFactory
 
 $_regionFactory
 
 $mathDivision
 
 $stockRegistry
 

Detailed Description

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 16 of file Shipping.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\Config\ScopeConfigInterface  $scopeConfig,
\Magento\Shipping\Model\Config  $shippingConfig,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Shipping\Model\CarrierFactory  $carrierFactory,
\Magento\Shipping\Model\Rate\ResultFactory  $rateResultFactory,
\Magento\Shipping\Model\Shipment\RequestFactory  $shipmentRequestFactory,
\Magento\Directory\Model\RegionFactory  $regionFactory,
\Magento\Framework\Math\Division  $mathDivision,
\Magento\CatalogInventory\Api\StockRegistryInterface  $stockRegistry,
RateRequestFactory  $rateRequestFactory = null 
)
Parameters
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Shipping\Model\Config$shippingConfig
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Shipping\Model\CarrierFactory$carrierFactory
\Magento\Shipping\Model\Rate\ResultFactory$rateResultFactory
\Magento\Shipping\Model\Shipment\RequestFactory$shipmentRequestFactory
\Magento\Directory\Model\RegionFactory$regionFactory
\Magento\Framework\Math\Division$mathDivision
\Magento\CatalogInventory\Api\StockRegistryInterface$stockRegistry
RateRequestFactory$rateRequestFactory@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 105 of file Shipping.php.

116  {
117  $this->_scopeConfig = $scopeConfig;
118  $this->_shippingConfig = $shippingConfig;
119  $this->_storeManager = $storeManager;
120  $this->_carrierFactory = $carrierFactory;
121  $this->_rateResultFactory = $rateResultFactory;
122  $this->_shipmentRequestFactory = $shipmentRequestFactory;
123  $this->_regionFactory = $regionFactory;
124  $this->mathDivision = $mathDivision;
125  $this->stockRegistry = $stockRegistry;
126  $this->rateRequestFactory = $rateRequestFactory ?: ObjectManager::getInstance()->get(RateRequestFactory::class);
127  }
$storeManager

Member Function Documentation

◆ _makePieces()

_makePieces (   $items,
  $maxWeight 
)
protected

Make pieces Compose packages list based on given items, so that each package is as heavy as possible

Parameters
array$items
float$maxWeight
Returns
array

Definition at line 429 of file Shipping.php.

430  {
431  $pieces = [];
432  if (!empty($items)) {
433  $sumWeight = 0;
434 
435  $reverseOrderItems = $items;
436  arsort($reverseOrderItems);
437 
438  foreach ($reverseOrderItems as $key => $weight) {
439  if (!isset($items[$key])) {
440  continue;
441  }
442  unset($items[$key]);
443  $sumWeight = $weight;
444  foreach ($items as $key => $weight) {
445  if ($sumWeight + $weight < $maxWeight) {
446  unset($items[$key]);
447  $sumWeight += $weight;
448  } elseif ($sumWeight + $weight > $maxWeight) {
449  $pieces[] = (string)(double)$sumWeight;
450  break;
451  } else {
452  unset($items[$key]);
453  $pieces[] = (string)(double)($sumWeight + $weight);
454  $sumWeight = 0;
455  break;
456  }
457  }
458  }
459  if ($sumWeight > 0) {
460  $pieces[] = (string)(double)$sumWeight;
461  }
462  $pieces = array_count_values($pieces);
463  }
464 
465  return $pieces;
466  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$items

◆ collectCarrierRates()

collectCarrierRates (   $carrierCode,
  $request 
)

Collect rates of given carrier

Parameters
string$carrierCode
\Magento\Quote\Model\Quote\Address\RateRequest$request
Returns
$this @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 252 of file Shipping.php.

253  {
254  /* @var $carrier \Magento\Shipping\Model\Carrier\AbstractCarrier */
255  $carrier = $this->_carrierFactory->createIfActive($carrierCode, $request->getStoreId());
256  if (!$carrier) {
257  return $this;
258  }
259  $carrier->setActiveFlag($this->_availabilityConfigField);
260  $result = $carrier->checkAvailableShipCountries($request);
261  if (false !== $result && !$result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Error) {
262  $result = $carrier->processAdditionalValidation($request);
263  }
264  /*
265  * Result will be false if the admin set not to show the shipping module
266  * if the delivery country is not within specific countries
267  */
268  if (false !== $result) {
269  if (!$result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Error) {
270  if ($carrier->getConfigData('shipment_requesttype')) {
271  $packages = $this->composePackagesForCarrier($carrier, $request);
272  if (!empty($packages)) {
273  $sumResults = [];
274  foreach ($packages as $weight => $packageCount) {
275  $request->setPackageWeight($weight);
276  $result = $carrier->collectRates($request);
277  if (!$result) {
278  return $this;
279  } else {
280  $result->updateRatePrice($packageCount);
281  }
282  $sumResults[] = $result;
283  }
284  if (!empty($sumResults) && count($sumResults) > 1) {
285  $result = [];
286  foreach ($sumResults as $res) {
287  if (empty($result)) {
288  $result = $res;
289  continue;
290  }
291  foreach ($res->getAllRates() as $method) {
292  foreach ($result->getAllRates() as $resultMethod) {
293  if ($method->getMethod() == $resultMethod->getMethod()) {
294  $resultMethod->setPrice($method->getPrice() + $resultMethod->getPrice());
295  continue;
296  }
297  }
298  }
299  }
300  }
301  } else {
302  $result = $carrier->collectRates($request);
303  }
304  } else {
305  $result = $carrier->collectRates($request);
306  }
307  if (!$result) {
308  return $this;
309  }
310  }
311  if ($carrier->getConfigData('showmethod') == 0 && $result->getError()) {
312  return $this;
313  }
314  // sort rates by price
315  if (method_exists($result, 'sortRatesByPrice') && is_callable([$result, 'sortRatesByPrice'])) {
316  $result->sortRatesByPrice();
317  }
318  $this->getResult()->append($result);
319  }
320  return $this;
321  }
$method
Definition: info.phtml:13

◆ collectRates()

collectRates ( \Magento\Quote\Model\Quote\Address\RateRequest  $request)

Retrieve all methods for supplied shipping data

Parameters
\Magento\Quote\Model\Quote\Address\RateRequest$request
Returns
$this
Todo:
make it ordered

Definition at line 181 of file Shipping.php.

182  {
183  $storeId = $request->getStoreId();
184  if (!$request->getOrig()) {
185  $request->setCountryId(
186  $this->_scopeConfig->getValue(
188  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
189  $request->getStore()
190  )
191  )->setRegionId(
192  $this->_scopeConfig->getValue(
194  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
195  $request->getStore()
196  )
197  )->setCity(
198  $this->_scopeConfig->getValue(
200  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
201  $request->getStore()
202  )
203  )->setPostcode(
204  $this->_scopeConfig->getValue(
206  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
207  $request->getStore()
208  )
209  );
210  }
211 
212  $limitCarrier = $request->getLimitCarrier();
213  if (!$limitCarrier) {
214  $carriers = $this->_scopeConfig->getValue(
215  'carriers',
216  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
217  $storeId
218  );
219 
220  foreach ($carriers as $carrierCode => $carrierConfig) {
221  $this->collectCarrierRates($carrierCode, $request);
222  }
223  } else {
224  if (!is_array($limitCarrier)) {
225  $limitCarrier = [$limitCarrier];
226  }
227  foreach ($limitCarrier as $carrierCode) {
228  $carrierConfig = $this->_scopeConfig->getValue(
229  'carriers/' . $carrierCode,
230  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
231  $storeId
232  );
233  if (!$carrierConfig) {
234  continue;
235  }
236  $this->collectCarrierRates($carrierCode, $request);
237  }
238  }
239 
240  return $this;
241  }
collectCarrierRates($carrierCode, $request)
Definition: Shipping.php:252

◆ getConfig()

getConfig ( )

Retrieve configuration model

Returns
\Magento\Shipping\Model\Config

Definition at line 169 of file Shipping.php.

170  {
171  return $this->_shippingConfig;
172  }

◆ getResult()

getResult ( )

Get shipping rate result model

Returns
\Magento\Shipping\Model\Rate\Result

Definition at line 134 of file Shipping.php.

135  {
136  if (empty($this->_result)) {
137  $this->_result = $this->_rateResultFactory->create();
138  }
139  return $this->_result;
140  }

◆ resetResult()

resetResult ( )

Reset cached result

Returns
$this

Definition at line 158 of file Shipping.php.

159  {
160  $this->getResult()->reset();
161  return $this;
162  }

◆ setCarrierAvailabilityConfigField()

setCarrierAvailabilityConfigField (   $code = 'active')

Set part of carrier xml config path

Parameters
string$code
Returns
$this

Definition at line 508 of file Shipping.php.

509  {
510  $this->_availabilityConfigField = $code;
511  return $this;
512  }
$code
Definition: info.phtml:12

◆ setOrigData()

setOrigData (   $data)

Set shipping orig data

Parameters
array$data
Returns
void

Definition at line 148 of file Shipping.php.

149  {
150  $this->_orig = $data;
151  }

Field Documentation

◆ $_availabilityConfigField

$_availabilityConfigField = 'active'
protected

Definition at line 37 of file Shipping.php.

◆ $_carrierFactory

$_carrierFactory
protected

Definition at line 59 of file Shipping.php.

◆ $_orig

$_orig = null
protected

Definition at line 23 of file Shipping.php.

◆ $_rateResultFactory

$_rateResultFactory
protected

Definition at line 64 of file Shipping.php.

◆ $_regionFactory

$_regionFactory
protected

Definition at line 74 of file Shipping.php.

◆ $_result

$_result = null
protected

Definition at line 30 of file Shipping.php.

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 44 of file Shipping.php.

◆ $_shipmentRequestFactory

$_shipmentRequestFactory
protected

Definition at line 69 of file Shipping.php.

◆ $_shippingConfig

$_shippingConfig
protected

Definition at line 54 of file Shipping.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 49 of file Shipping.php.

◆ $mathDivision

$mathDivision
protected

Definition at line 79 of file Shipping.php.

◆ $stockRegistry

$stockRegistry
protected

Definition at line 84 of file Shipping.php.


The documentation for this class was generated from the following file: