PluginProbe
Packeta / 1.2.6
Packeta v1.2.6
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / Builder.php

Builder.php in Packeta 1.2.6, at src/Packetery/Module/Order/Builder.php

145 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Order
4 *
5 * @package Packetery\Module\EntityFactory
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Order;
11
12 use Packetery\Core\Entity;
13 use Packetery\Core\Entity\Address;
14 use Packetery\Module\Calculator;
15 use Packetery\Module\Carrier;
16 use Packetery\Module\Options\Provider;
17 use Packetery\Module\Product;
18 use WC_Order;
19
20 /**
21 * Class Order
22 *
23 * @package Packetery\Module\EntityFactory
24 */
25 class Builder {
26
27 /**
28 * Options provider.
29 *
30 * @var Provider Options provider.
31 */
32 private $optionsProvider;
33
34 /**
35 * Carrier repository.
36 *
37 * @var Carrier\Repository Carrier repository.
38 */
39 private $carrierRepository;
40
41 /**
42 * Weight calculator.
43 *
44 * @var Calculator
45 */
46 private $calculator;
47
48 /**
49 * Order constructor.
50 *
51 * @param Provider $optionsProvider Options Provider.
52 * @param Carrier\Repository $carrierRepository Carrier repository.
53 * @param Calculator $calculator Weight calculator.
54 */
55 public function __construct(
56 Provider $optionsProvider,
57 Carrier\Repository $carrierRepository,
58 Calculator $calculator
59 ) {
60 $this->optionsProvider = $optionsProvider;
61 $this->carrierRepository = $carrierRepository;
62 $this->calculator = $calculator;
63 }
64
65 /**
66 * Creates common order entity from WC_Order.
67 *
68 * @param WC_Order $wcOrder WC_Order.
69 * @param Entity\Order $order Partial order.
70 *
71 * @return Entity\Order
72 */
73 public function finalize( WC_Order $wcOrder, Entity\Order $order ): Entity\Order {
74 $calculatedWeight = $this->calculator->calculateOrderWeight( $wcOrder );
75 if ( null === $order->getWeight() ) {
76 $order->setWeight( $calculatedWeight );
77 }
78 $order->setCalculatedWeight( $calculatedWeight );
79 $order->setAdultContent( $this->containsAdultContent( $wcOrder ) );
80 $order->setShippingCountry( strtolower( $wcOrder->get_shipping_country() ) );
81
82 $orderData = $wcOrder->get_data();
83 $contactInfo = ( $wcOrder->has_shipping_address() ? $orderData['shipping'] : $orderData['billing'] );
84
85 $order->setName( $contactInfo['first_name'] );
86 $order->setSurname( $contactInfo['last_name'] );
87 $order->setEshop( $this->optionsProvider->get_sender() );
88 $order->setValue( (float) $wcOrder->get_total( 'raw' ) );
89
90 $address = $order->getDeliveryAddress();
91 if ( null === $address ) {
92 $order->setAddressValidated( false );
93 $address = new Address( $contactInfo['address_1'], $contactInfo['city'], $contactInfo['postcode'] );
94 }
95
96 $order->setDeliveryAddress( $address );
97
98 // Shipping address phone is optional.
99 $order->setPhone( $orderData['billing']['phone'] );
100 if ( ! empty( $contactInfo['phone'] ) ) {
101 $order->setPhone( $contactInfo['phone'] );
102 }
103 // Additional address information.
104 if ( ! empty( $contactInfo['address_2'] ) ) {
105 $order->setNote( $contactInfo['address_2'] );
106 }
107
108 $order->setEmail( $orderData['billing']['email'] );
109 $codMethod = $this->optionsProvider->getCodPaymentMethod();
110 if ( $orderData['payment_method'] === $codMethod ) {
111 $order->setCod( $order->getValue() );
112 }
113 $order->setSize( $order->getSize() );
114
115 if ( $order->isExternalCarrier() ) {
116 $carrier = $this->carrierRepository->getById( (int) $order->getCarrierId() );
117 $order->setCarrier( $carrier );
118 }
119
120 $order->setCurrency( $wcOrder->get_currency() );
121
122 return $order;
123 }
124
125 /**
126 * Finds out if adult content is present.
127 *
128 * @param WC_Order $wcOrder WC Order.
129 *
130 * @return bool
131 */
132 private function containsAdultContent( WC_Order $wcOrder ): bool {
133 foreach ( $wcOrder->get_items() as $item ) {
134 $itemData = $item->get_data();
135 $productEntity = Product\Entity::fromPostId( $itemData['product_id'] );
136 if ( $productEntity->isAgeVerification18PlusRequired() ) {
137 return true;
138 }
139 }
140
141 return false;
142 }
143
144 }
145