Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingProcessor.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Quote\Model\ShippingFactory;
13 
15 {
19  private $shippingFactory;
20 
24  private $shippingAddressManagement;
25 
29  private $shippingMethodManagement;
30 
36  public function __construct(
37  ShippingFactory $shippingFactory,
38  ShippingAddressManagement $shippingAddressManagement,
39  ShippingMethodManagement $shippingMethodManagement
40  ) {
41  $this->shippingFactory = $shippingFactory;
42  $this->shippingAddressManagement = $shippingAddressManagement;
43  $this->shippingMethodManagement = $shippingMethodManagement;
44  }
45 
50  public function create(\Magento\Quote\Api\Data\AddressInterface $shippingAddress)
51  {
53  $shipping = $this->shippingFactory->create();
54  $shipping->setMethod($shippingAddress->getShippingMethod());
55  $shipping->setAddress($shippingAddress);
56  return $shipping;
57  }
58 
64  public function save(ShippingInterface $shipping, CartInterface $quote)
65  {
66  $this->shippingAddressManagement->assign($quote->getId(), $shipping->getAddress());
67  if (!empty($shipping->getMethod()) && $quote->getItemsCount() > 0) {
68  $nameComponents = explode('_', $shipping->getMethod());
69  $carrierCode = array_shift($nameComponents);
70  // carrier method code can contains more one name component
71  $methodCode = implode('_', $nameComponents);
72  $this->shippingMethodManagement->apply($quote->getId(), $carrierCode, $methodCode);
73  }
74  }
75 }
__construct(ShippingFactory $shippingFactory, ShippingAddressManagement $shippingAddressManagement, ShippingMethodManagement $shippingMethodManagement)
$quote
$shippingAddress
Definition: order.php:40
save(ShippingInterface $shipping, CartInterface $quote)