PluginProbe
PostNL for WooCommerce / 4.0.1
PostNL for WooCommerce v4.0.1
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / frontend / class-wcpn-frontend.php

class-wcpn-frontend.php in PostNL for WooCommerce 4.0.1, at includes/frontend/class-wcpn-frontend.php

261 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MyParcelNL\Sdk\src\Support\Arr;
4 use WPO\WC\PostNL\Compatibility\WC_Core as WCX;
5
6 if (! defined('ABSPATH')) {
7 exit;
8 } // Exit if accessed directly
9
10 if (class_exists('WCPN_Frontend')) {
11 return new WCPN_Frontend();
12 }
13
14 /**
15 * Frontend views
16 */
17 class WCPN_Frontend
18 {
19 public function __construct()
20 {
21 new WCPN_Frontend_Track_Trace();
22
23 // pickup address in email
24 // woocommerce_email_customer_details:
25 // @10 = templates/email-customer-details.php
26 // @20 = templates/email-addresses.php
27 add_action("woocommerce_email_customer_details", [$this, "email_pickup_html"], 19, 3);
28
29 // pickup address on thank you page
30 add_action("woocommerce_thankyou", [$this, "thankyou_pickup_html"], 10, 1);
31
32 // WooCommerce PDF Invoices & Packing Slips Premium Templates compatibility
33 add_filter("wpo_wcpdf_templates_replace_postnl_delivery_options", [$this, "wpo_wcpdf_delivery_options"], 10, 2);
34 add_filter("wpo_wcpdf_templates_replace_postnl_delivery_date", [$this, "wpo_wcpdf_delivery_date"], 10, 2);
35
36 // Initialize delivery options fees
37 new WCPN_Cart_Fees();
38
39 // Output most expensive shipping class in frontend data
40 add_action("woocommerce_checkout_before_order_review", [$this, "injectShippingClassInput"], 100);
41 add_action("woocommerce_update_order_review_fragments", [$this, "order_review_fragments"]);
42
43 // Ajax
44 add_action('wp_ajax_get_highest_shipping_class', [$this, 'ajaxGetHighestShippingClass']);
45 add_action('wp_ajax_nopriv_get_highest_shipping_class', [$this, 'ajaxGetHighestShippingClass']);
46 }
47
48 /**
49 * @param \WC_Order $order
50 *
51 * @throws \Exception
52 */
53 public function email_pickup_html(WC_Order $order): void
54 {
55 WCPOST()->admin->showDeliveryDateForOrder($order);
56 }
57
58 /**
59 * @param int $order_id
60 *
61 * @throws Exception
62 */
63 public function thankyou_pickup_html(int $order_id): void
64 {
65 $order = wc_get_order($order_id);
66 WCPOST()->admin->showDeliveryDateForOrder($order);
67 }
68
69 /**
70 * @param string $replacement
71 * @param WC_Order $order
72 *
73 * @return string
74 * @throws Exception
75 */
76 public function wpo_wcpdf_delivery_options(string $replacement, WC_Order $order): string
77 {
78 ob_start();
79 WCPOST()->admin->showDeliveryDateForOrder($order);
80
81 return ob_get_clean();
82 }
83
84 /**
85 * @param string $replacement
86 * @param WC_Order $order
87 *
88 * @return string
89 * @throws Exception
90 */
91 public function wpo_wcpdf_delivery_date(string $replacement, WC_Order $order): string
92 {
93 $deliveryOptions = WCPOST_Admin::getDeliveryOptionsFromOrder($order);
94 $deliveryDate = $deliveryOptions->getDate();
95
96 if ($deliveryDate) {
97 return wc_format_datetime(new WC_DateTime($deliveryDate), 'l d-m');
98 }
99
100 return $replacement;
101 }
102
103 /**
104 * Output the highest shipping class input
105 *
106 * @throws Exception
107 */
108 public function injectShippingClassInput(): void
109 {
110 echo '<div class="wcpn__shipping-data">';
111 $this->renderHighestShippingClassInput();
112 echo '</div>';
113 }
114
115 /**
116 * @return string|void
117 * @throws Exception
118 */
119 public function renderHighestShippingClassInput()
120 {
121 $shipping_class = WCPN_Frontend::get_cart_shipping_class();
122
123 if ($shipping_class) {
124 return sprintf(
125 '<input type="hidden" value="%s" name="postnl_highest_shipping_class">',
126 $shipping_class
127 );
128 }
129 }
130
131 /**
132 * Get the most expensive shipping class in the cart
133 * Requires WC2.4+
134 * Only supports 1 package, takes the first
135 *
136 * @return null|int
137 * @throws \Exception
138 */
139 public static function get_cart_shipping_class(): ?int
140 {
141 if (version_compare(WOOCOMMERCE_VERSION, '2.4', '<')) {
142 return null;
143 }
144
145 $shippingMethodString = WC()->session->get('chosen_shipping_methods')[0] ?? '';
146 $shippingMethod = WCPN_Export::getShippingMethod($shippingMethodString);
147
148 if (empty($shippingMethod)) {
149 return null;
150 }
151
152 if (method_exists($shippingMethod, 'find_shipping_classes')) {
153 // get package
154 $packages = WC()->cart->get_shipping_packages();
155 $package = current($packages);
156
157 // get shipping classes from package
158 $shippingClasses = $shippingMethod->find_shipping_classes($package);
159 } else {
160 $shippingClasses = [];
161 }
162
163 return WCPOST()->export->getShippingClass(
164 $shippingMethod,
165 $shippingClasses
166 );
167 }
168
169 /**
170 * @param $fragments
171 *
172 * @return mixed
173 */
174 public function order_review_fragments($fragments)
175 {
176 $postnl_shipping_data = $this->renderHighestShippingClassInput();
177 $fragments['.wcpn__shipping-data'] = $postnl_shipping_data;
178
179 return $fragments;
180 }
181
182 /**
183 * @param $order_id
184 *
185 * @return array|bool|mixed|void
186 * @throws Exception
187 */
188 public static function getTrackTraceShipments($order_id): array
189 {
190 $order = WCX::get_order($order_id);
191 $shipments = WCPOST_Admin::get_order_shipments($order);
192
193 if (empty($shipments)) {
194 return [];
195 }
196
197 foreach ($shipments as $shipment_id => $shipment) {
198 $trackTrace = Arr::get($shipment, "track_trace");
199
200 // skip concepts
201 if (! $trackTrace) {
202 unset($shipments[$shipment_id]);
203 continue;
204 }
205
206 $track_trace_url = WCPOST_Admin::getTrackTraceUrl(
207 $order_id,
208 $trackTrace
209 );
210
211 // add links & urls
212 Arr::set($shipments, "$shipment_id.track_trace_url", $track_trace_url);
213 Arr::set(
214 $shipments,
215 "$shipment_id.track_trace_link",
216 sprintf(
217 '<a href="%s">%s</a>',
218 $track_trace_url,
219 $trackTrace
220 )
221 );
222 }
223
224 return $shipments;
225 }
226
227 /**
228 * @param $order_id
229 *
230 * @return array|bool
231 * @throws Exception
232 */
233 public static function getTrackTraceLinks($order_id): array
234 {
235 $track_trace_links = [];
236
237 $consignments = self::getTrackTraceShipments($order_id);
238
239 foreach ($consignments as $key => $consignment) {
240 $track_trace_links[] = [
241 "link" => $consignment["track_trace_link"],
242 "url" => $consignment["track_trace_url"],
243 ];
244 }
245
246 return $track_trace_links;
247 }
248
249 /**
250 * @return int|null
251 * @throws Exception
252 */
253 public function ajaxGetHighestShippingClass(): ?int
254 {
255 echo WCPN_Frontend::get_cart_shipping_class();
256 die();
257 }
258 }
259
260 return new WCPN_Frontend();
261