Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingBuilder.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Sales\Api\Data\ShippingInterfaceFactory;
11 use Magento\Sales\Api\Data\TotalInterfaceFactory;
13 use Magento\Sales\Model\OrderFactory;
14 
20 {
24  private $orderId = null;
25 
29  private $order;
30 
34  private $orderFactory;
35 
39  private $shippingFactory;
40 
44  private $totalFactory;
45 
53  public function __construct(
54  OrderFactory $orderFactory,
55  ShippingInterfaceFactory $shippingFactory,
56  TotalInterfaceFactory $totalFactory
57  ) {
58  $this->orderFactory = $orderFactory;
59  $this->shippingFactory = $shippingFactory;
60  $this->totalFactory = $totalFactory;
61  }
62 
67  public function setOrderId($orderId)
68  {
69  $this->orderId = $orderId;
70  }
71 
75  public function create()
76  {
77  $shipping = null;
78  if ($this->getOrderId()) {
79  $this->order = $this->orderFactory->create()->load($this->getOrderId());
80  if ($this->order->getEntityId()) {
82  $shipping = $this->shippingFactory->create();
83  $shippingAddress = $this->order->getShippingAddress();
84  if ($shippingAddress) {
85  $shipping->setAddress($shippingAddress);
86  }
87  $shipping->setMethod($this->order->getShippingMethod());
88  $shipping->setTotal($this->getTotal());
89  }
90  }
91  return $shipping;
92  }
93 
97  private function getOrderId()
98  {
99  return $this->orderId;
100  }
101 
105  private function getTotal()
106  {
108  $total = $this->totalFactory->create();
109  $total->setBaseShippingAmount($this->order->getBaseShippingAmount());
110  $total->setBaseShippingCanceled($this->order->getBaseShippingCanceled());
111  $total->setBaseShippingDiscountAmount($this->order->getBaseShippingDiscountAmount());
112  $total->setBaseShippingDiscountTaxCompensationAmnt($this->order->getBaseShippingDiscountTaxCompensationAmnt());
113  $total->setBaseShippingInclTax($this->order->getBaseShippingInclTax());
114  $total->setBaseShippingInvoiced($this->order->getBaseShippingInvoiced());
115  $total->setBaseShippingRefunded($this->order->getBaseShippingRefunded());
116  $total->setBaseShippingTaxAmount($this->order->getBaseShippingTaxAmount());
117  $total->setBaseShippingTaxRefunded($this->order->getBaseShippingTaxRefunded());
118  $total->setShippingAmount($this->order->getShippingAmount());
119  $total->setShippingCanceled($this->order->getShippingCanceled());
120  $total->setShippingDiscountAmount($this->order->getShippingDiscountAmount());
121  $total->setShippingDiscountTaxCompensationAmount($this->order->getShippingDiscountTaxCompensationAmount());
122  $total->setShippingInclTax($this->order->getShippingInclTax());
123  $total->setShippingInvoiced($this->order->getShippingInvoiced());
124  $total->setShippingRefunded($this->order->getShippingRefunded());
125  $total->setShippingTaxAmount($this->order->getShippingTaxAmount());
126  $total->setShippingTaxRefunded($this->order->getShippingTaxRefunded());
127  return $total;
128  }
129 }
$shippingAddress
Definition: order.php:40
__construct(OrderFactory $orderFactory, ShippingInterfaceFactory $shippingFactory, TotalInterfaceFactory $totalFactory)