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

153 lines 4.0 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\CustomsDeclaration;
15 use Packetery\Module\Helper;
16 use Packetery\Module\WeightCalculator;
17 use Packetery\Module\Options\Provider;
18 use Packetery\Module\Product;
19 use WC_Order;
20
21 /**
22 * Class Order
23 *
24 * @package Packetery\Module\EntityFactory
25 */
26 class Builder {
27
28 /**
29 * Options provider.
30 *
31 * @var Provider Options provider.
32 */
33 private $optionsProvider;
34
35 /**
36 * Weight calculator.
37 *
38 * @var WeightCalculator
39 */
40 private $calculator;
41
42 /**
43 * Customs declaration repository.
44 *
45 * @var CustomsDeclaration\Repository
46 */
47 private $customsDeclarationRepository;
48
49 /**
50 * Order constructor.
51 *
52 * @param Provider $optionsProvider Options Provider.
53 * @param WeightCalculator $calculator Weight calculator.
54 * @param CustomsDeclaration\Repository $customsDeclarationRepository Customs declaration repository.
55 */
56 public function __construct(
57 Provider $optionsProvider,
58 WeightCalculator $calculator,
59 CustomsDeclaration\Repository $customsDeclarationRepository
60 ) {
61 $this->optionsProvider = $optionsProvider;
62 $this->calculator = $calculator;
63 $this->customsDeclarationRepository = $customsDeclarationRepository;
64 }
65
66 /**
67 * Creates common order entity from WC_Order.
68 *
69 * @param WC_Order $wcOrder WC_Order.
70 * @param Entity\Order $order Partial order.
71 *
72 * @return Entity\Order
73 */
74 public function finalize( WC_Order $wcOrder, Entity\Order $order ): Entity\Order {
75 $order->setCalculatedWeight( $this->calculator->calculateOrderWeight( $wcOrder ) );
76
77 if ( null === $order->containsAdultContent() ) {
78 $order->setAdultContent( $this->containsAdultContent( $wcOrder ) );
79 }
80
81 $order->setShippingCountry( Helper::getWcOrderCountry( $wcOrder ) );
82
83 $orderData = $wcOrder->get_data();
84 $contactInfo = ( $wcOrder->has_shipping_address() ? $orderData['shipping'] : $orderData['billing'] );
85
86 $order->setName( $contactInfo['first_name'] );
87 $order->setSurname( $contactInfo['last_name'] );
88 $order->setEshop( $this->optionsProvider->get_sender() );
89
90 if ( null === $order->getValue() ) {
91 $order->setValue( (float) $wcOrder->get_total( 'raw' ) );
92 }
93
94 $address = $order->getDeliveryAddress();
95 if ( null === $address ) {
96 $order->setAddressValidated( false );
97 $address = new Address( $contactInfo['address_1'], $contactInfo['city'], $contactInfo['postcode'] );
98 }
99
100 $order->setDeliveryAddress( $address );
101 $order->setCustomNumber( $wcOrder->get_order_number() );
102
103 // Shipping address phone is optional.
104 $order->setPhone( $orderData['billing']['phone'] );
105 if ( ! empty( $contactInfo['phone'] ) ) {
106 $order->setPhone( $contactInfo['phone'] );
107 }
108 // Additional address information.
109 if ( ! empty( $contactInfo['address_2'] ) ) {
110 $order->setNote( $contactInfo['address_2'] );
111 }
112
113 $order->setEmail( $orderData['billing']['email'] );
114 $codMethod = $this->optionsProvider->getCodPaymentMethod();
115 if ( null === $order->getCod() && $orderData['payment_method'] === $codMethod ) {
116 $order->setCod( $order->getValue() );
117 }
118
119 if ( $orderData['payment_method'] !== $codMethod ) {
120 $order->setCod( null );
121 }
122
123 $order->setSize( $order->getSize() );
124 $order->setCurrency( $wcOrder->get_currency() );
125
126 $order->setCustomsDeclaration( $this->customsDeclarationRepository->getByOrderNumber( $order->getNumber() ) );
127
128 return $order;
129 }
130
131 /**
132 * Finds out if adult content is present.
133 *
134 * @param WC_Order $wcOrder WC Order.
135 *
136 * @return bool
137 */
138 private function containsAdultContent( WC_Order $wcOrder ): bool {
139 foreach ( $wcOrder->get_items() as $item ) {
140 $product = $item->get_product();
141 if ( $product ) {
142 $productEntity = new Product\Entity( $product );
143 if ( $productEntity->isPhysical() && $productEntity->isAgeVerification18PlusRequired() ) {
144 return true;
145 }
146 }
147 }
148
149 return false;
150 }
151
152 }
153