PluginProbe
Packeta / 1.4.2
Packeta v1.4.2
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.4.2, at src/Packetery/Module/Order/Builder.php

156 lines 3.9 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 $order->setCalculatedWeight( $this->calculator->calculateOrderWeight( $wcOrder ) );
75
76 if ( null === $order->containsAdultContent() ) {
77 $order->setAdultContent( $this->containsAdultContent( $wcOrder ) );
78 }
79
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
89 if ( null === $order->getValue() ) {
90 $order->setValue( (float) $wcOrder->get_total( 'raw' ) );
91 }
92
93 $address = $order->getDeliveryAddress();
94 if ( null === $address ) {
95 $order->setAddressValidated( false );
96 $address = new Address( $contactInfo['address_1'], $contactInfo['city'], $contactInfo['postcode'] );
97 }
98
99 $order->setDeliveryAddress( $address );
100 $order->setCustomNumber( $wcOrder->get_order_number() );
101
102 // Shipping address phone is optional.
103 $order->setPhone( $orderData['billing']['phone'] );
104 if ( ! empty( $contactInfo['phone'] ) ) {
105 $order->setPhone( $contactInfo['phone'] );
106 }
107 // Additional address information.
108 if ( ! empty( $contactInfo['address_2'] ) ) {
109 $order->setNote( $contactInfo['address_2'] );
110 }
111
112 $order->setEmail( $orderData['billing']['email'] );
113 $codMethod = $this->optionsProvider->getCodPaymentMethod();
114 if ( null === $order->getCod() && $orderData['payment_method'] === $codMethod ) {
115 $order->setCod( $order->getValue() );
116 }
117
118 if ( $orderData['payment_method'] !== $codMethod ) {
119 $order->setCod( null );
120 }
121
122 $order->setSize( $order->getSize() );
123
124 if ( $order->isExternalCarrier() ) {
125 $carrier = $this->carrierRepository->getById( (int) $order->getCarrierId() );
126 $order->setCarrier( $carrier );
127 }
128
129 $order->setCurrency( $wcOrder->get_currency() );
130
131 return $order;
132 }
133
134 /**
135 * Finds out if adult content is present.
136 *
137 * @param WC_Order $wcOrder WC Order.
138 *
139 * @return bool
140 */
141 private function containsAdultContent( WC_Order $wcOrder ): bool {
142 foreach ( $wcOrder->get_items() as $item ) {
143 $product = $item->get_product();
144 if ( $product ) {
145 $productEntity = new Product\Entity( $product );
146 if ( $productEntity->isPhysical() && $productEntity->isAgeVerification18PlusRequired() ) {
147 return true;
148 }
149 }
150 }
151
152 return false;
153 }
154
155 }
156