-
Notifications
You must be signed in to change notification settings - Fork 61
/
ShippingBuilder.php
79 lines (70 loc) · 2.59 KB
/
ShippingBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace TIG\PostNL\Plugin\Shipment;
use Magento\Framework\Api\ExtensionAttributesFactory;
use Magento\Sales\Api\Data\ShippingInterface;
use Magento\Sales\Model\Order\ShippingBuilder as SubjectClass;
use TIG\PostNL\Api\Data\ShipmentAttributeInterface;
use TIG\PostNL\Api\Data\ShipmentAttributeInterfaceFactory;
use TIG\PostNL\Api\OrderRepositoryInterface;
class ShippingBuilder
{
/**
* @var null|int
*/
private $orderId = null;
private ShipmentAttributeInterfaceFactory $shipmentAttributeFactory;
private OrderRepositoryInterface $orderRepository;
public function __construct(
OrderRepositoryInterface $orderRepository,
ShipmentAttributeInterfaceFactory $shipmentAttributeFactory
) {
$this->shipmentAttributeFactory = $shipmentAttributeFactory;
$this->orderRepository = $orderRepository;
}
public function afterCreate(
SubjectClass $subject,
?ShippingInterface $shipping
): ?ShippingInterface {
if ($shipping) {
$method = $shipping->getMethod();
if ($this->orderId &&
strpos($method, 'tig_postnl') !== false &&
$extensionAttributes = $shipping->getExtensionAttributes()
) {
$postnlOrder = $this->orderRepository->getByOrderId($this->orderId);
if ($postnlOrder) {
/** @var ShipmentAttributeInterface $model */
$model = $this->shipmentAttributeFactory->create();
$model->setProductCode($postnlOrder->getProductCode())
->setType((string)$postnlOrder->getType())
->setShipAt((string)$postnlOrder->getShipAt())
->setDeliveryDate($postnlOrder->getDeliveryDate())
->setExpectedDeliveryTimeStart($postnlOrder->getExpectedDeliveryTimeStart())
->setExpectedDeliveryTimeEnd($postnlOrder->getExpectedDeliveryTimeEnd());
$extensionAttributes->setPostnl($model);
$shipping->setExtensionAttributes($extensionAttributes);
}
}
}
$this->orderId = null;
return $shipping;
}
public function afterSetOrderId(
SubjectClass $subject,
$result,
$orderId
) {
$this->orderId = (int)$orderId;
return $result;
}
public function afterSetOrder(
SubjectClass $subject,
$result,
$order
) {
if ($order) {
$this->orderId = $order->getId();
}
return $result;
}
}