PluginProbe
Sendy / 3.4.7
Sendy v3.4.7
3.4.6 3.4.7 trunk 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 All 32 releases
sendy / lib / Modules / BlocksCheckout.php

BlocksCheckout.php in Sendy 3.4.7, at lib/Modules/BlocksCheckout.php

162 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sendy\WooCommerce\Modules;
4
5 use Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema;
6 use Sendy\WooCommerce\ShippingMethods\PickupPointDelivery;
7 use Sendy\WooCommerce\Utils\BlocksIntegration;
8
9 final class BlocksCheckout
10 {
11 public static function init(): self
12 {
13 return new self();
14 }
15
16 private function __construct()
17 {
18 add_action('woocommerce_store_api_checkout_order_processed', [$this, 'validate_pickup_point'], 9);
19 add_action('woocommerce_store_api_checkout_order_processed', [$this, 'store_selected_pickup_point_in_order']);
20 add_action('woocommerce_blocks_loaded', [$this, 'register_store_api_endpoints']);
21 add_action('woocommerce_blocks_loaded', [$this, 'register_checkout_block']);
22 }
23
24 public function register_checkout_block(): void
25 {
26 add_action('woocommerce_blocks_checkout_block_registration', function ($integrationRegistry) {
27 $integrationRegistry->register(new BlocksIntegration());
28 });
29 }
30
31 /**
32 * Validate if a pick-up point is selected
33 */
34 public function validate_pickup_point(\WC_Order $order): void
35 {
36 if (! in_array('sendy_pickup_point', wc_get_chosen_shipping_method_ids())) {
37 return;
38 }
39
40 $instanceId = sendy_shipping_method_instance_id();
41
42 $selectedPickupPoint = WC()->session->get("sendy_selected_parcelshop_{$instanceId}");
43
44 if (empty($selectedPickupPoint)) {
45 wc_add_notice(__('Please select a pick-up point.', 'sendy'), 'error');
46 }
47 }
48
49
50 /**
51 * Add the selected pickup point to the order metadata
52 */
53 public function store_selected_pickup_point_in_order(\WC_Order $order): void
54 {
55 $instanceId = sendy_shipping_method_instance_id();
56
57 if ($data = WC()->session->get("sendy_selected_parcelshop_{$instanceId}")) {
58 $order->update_meta_data('_sendy_pickup_point_id', $data['id']);
59 $order->update_meta_data('_sendy_pickup_point_data', $data);
60
61 $order->save();
62
63 WC()->session->set("sendy_selected_parcelshop_{$instanceId}", null);
64 }
65 }
66
67
68 public function register_store_api_endpoints(): void
69 {
70 woocommerce_store_api_register_update_callback([
71 'namespace' => 'sendy-set-pickup-point',
72 'callback' => function ($data) {
73 $instanceId = sendy_shipping_method_instance_id();
74
75 $selectedPickupPoint = [
76 'instance_id' => $instanceId,
77 'id' => sanitize_text_field($data['id'] ?? ''),
78 'name' => sanitize_text_field($data['name'] ?? ''),
79 'street' => sanitize_text_field($data['street'] ?? ''),
80 'number' => sanitize_text_field($data['number'] ?? ''),
81 'postal_code' => sanitize_text_field($data['postal_code'] ?? ''),
82 'city' => sanitize_text_field($data['city'] ?? ''),
83 ];
84
85 WC()->session->set('sendy_selected_parcelshop_' . $instanceId, $selectedPickupPoint);
86 },
87 ]);
88
89 woocommerce_store_api_register_endpoint_data([
90 'endpoint' => CartSchema::IDENTIFIER,
91 'namespace' => 'sendy-pickup-point',
92 'data_callback' => function () {
93 $instanceId = sendy_shipping_method_instance_id();
94
95 $data = WC()->session->get("sendy_selected_parcelshop_{$instanceId}");
96
97 return [
98 'name' => $data['name'] ?? null,
99 'street' => $data['street'] ?? null,
100 'number' => $data['number'] ?? null,
101 'postal_code' => $data['postal_code'] ?? null,
102 'city' => $data['city'] ?? null,
103 ];
104 },
105 'schema_callback' => fn() => [
106 'properties' => [
107 'name' => [
108 'description' => 'Name of the pickup point',
109 'type' => ['string', 'null'],
110 'readonly' => true,
111 ],
112 'street' => [
113 'description' => 'Street of the pickup point',
114 'type' => ['string', 'null'],
115 'readonly' => true,
116 ],
117 'number' => [
118 'description' => 'Number of the pickup point',
119 'type' => ['string', 'null'],
120 'readonly' => true,
121 ],
122 'portal_code' => [
123 'description' => 'Postal code of the pickup point',
124 'type' => ['string', 'null'],
125 'readonly' => true,
126 ],
127 'city' => [
128 'description' => 'City of the pickup point',
129 'type' => ['string', 'null'],
130 'readonly' => true,
131 ],
132 ],
133 ],
134 'schema_type' => ARRAY_A,
135 ]);
136
137 woocommerce_store_api_register_endpoint_data([
138 'endpoint' => CartSchema::IDENTIFIER,
139 'namespace' => 'sendy-carrier',
140 'data_callback' => function () {
141 $instanceId = sendy_shipping_method_instance_id();
142
143 $carrier = (new PickupPointDelivery($instanceId))->get_option('carrier');
144
145 return [
146 'carrier' => $carrier ?? null,
147 ];
148 },
149 'schema_callback' => fn() => [
150 'properties' => [
151 'name' => [
152 'description' => 'The carrier to user for the pickup point picker',
153 'type' => ['string', 'null'],
154 'readonly' => true,
155 ],
156 ],
157 ],
158 'schema_type' => ARRAY_A,
159 ]);
160 }
161 }
162