PluginProbe
Sendy / 3.0
Sendy v3.0
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 / src / Modules / Checkout.php

Checkout.php in Sendy 3.0, at src/Modules/Checkout.php

166 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 Sendy\WooCommerce\Plugin;
6 use Sendy\WooCommerce\Utils\View;
7
8 final class Checkout
9 {
10 public function __construct()
11 {
12 add_action('wp_enqueue_scripts', [$this, 'enqueue_assets']);
13 add_action('woocommerce_review_order_after_shipping', [$this, 'render_pickup_point_field'], 10);
14
15 add_action('wp_ajax_sendy_set_pickup_point', [$this, 'store_selected_pickup_point_in_session']);
16 add_action('wp_ajax_nopriv_sendy_set_pickup_point', [$this, 'store_selected_pickup_point_in_session']);
17
18 add_action('woocommerce_checkout_create_order', [$this, 'store_selected_pickup_point_in_order'], 10, 3);
19
20 add_action('woocommerce_order_details_after_customer_address', [$this, 'show_selected_pickup_point_on_confirmation'], 10, 2);
21 }
22
23 /**
24 * Load the assets, but only on the checkout page
25 *
26 * @return void
27 */
28 public function enqueue_assets(): void
29 {
30 if (is_checkout()) {
31 wp_enqueue_script('wp-util');
32 wp_enqueue_script('sendy-api', 'https://app.sendy.nl/embed/api.js', [], Plugin::VERSION);
33 wp_enqueue_script('sendy-checkout', SENDY_WC_PLUGIN_DIR_URL . '/resources/js/checkout.js', ['jquery', 'sendy-api'], Plugin::VERSION);
34 }
35 }
36
37 /**
38 * Render the button to select a pick-up point in the checkout
39 *
40 * @return void
41 */
42 public function render_pickup_point_field(): void
43 {
44 if (in_array('sendy_pickup_point', wc_get_chosen_shipping_method_ids())) {
45 if (!is_null($this->get_shipping_method_instance_id())) {
46 $carrier = $this->get_carrier_for_shipping_method($this->get_shipping_method_instance_id());
47
48 $selectedPickupPoint = WC()->session->get("sendy_selected_parcelshop_{$this->get_shipping_method_instance_id()}");
49
50 echo wp_kses(
51 View::fromTemplate('checkout/pickup_point_selection.php')->render([
52 'carrier' => $carrier,
53 'instance_id' => $this->get_shipping_method_instance_id(),
54 'selected_pickup_point' => $selectedPickupPoint,
55 ]),
56 View::ALLOWED_TAGS
57 );
58 }
59 }
60 }
61
62 /**
63 * Store the selected pickup point in the session
64 *
65 * @return void
66 */
67 public function store_selected_pickup_point_in_session(): void
68 {
69 if (empty($_REQUEST['nonce']) || !wp_verify_nonce(sanitize_key($_REQUEST['nonce']), 'sendy_store_pickup_point')) {
70 wp_send_json_error('Nonce verification failed');
71 }
72
73 $data = [
74 'id' => sanitize_text_field(wp_unslash(isset($_REQUEST['id']) ? $_REQUEST['id'] : '')),
75 'name' => sanitize_text_field(wp_unslash(isset($_REQUEST['name']) ? $_REQUEST['name'] : '')),
76 'street' => sanitize_text_field(wp_unslash(isset($_REQUEST['street']) ? $_REQUEST['street'] : '')),
77 'number' => sanitize_text_field(wp_unslash(isset($_REQUEST['number']) ? $_REQUEST['number'] : '')),
78 'postal_code' => sanitize_text_field(wp_unslash(isset($_REQUEST['postal_code']) ? $_REQUEST['postal_code'] : '')),
79 'city' => sanitize_text_field(wp_unslash(isset($_REQUEST['city']) ? $_REQUEST['city'] : ''))
80 ];
81
82 WC()->session->set('sendy_selected_parcelshop_' . sanitize_key($_REQUEST['instance_id'] ?? ''), $data);
83
84 wp_send_json_success();
85 }
86
87 /**
88 * Add the selected pickup point to the order metadata
89 *
90 * @param \WC_Order $order
91 * @return void
92 */
93 public function store_selected_pickup_point_in_order(\WC_Order $order): void
94 {
95 $instanceId = $this->get_shipping_method_instance_id();
96
97 if ($data = WC()->session->get("sendy_selected_parcelshop_{$instanceId}")) {
98
99 $order->update_meta_data('_sendy_pickup_point_id', $data['id']);
100 $order->update_meta_data('_sendy_pickup_point_data', $data);
101
102 WC()->session->set("sendy_selected_parcelshop_{$instanceId}", null);
103 }
104 }
105
106 /**
107 * Display the address of the selected pickup point on the order confirmation page
108 *
109 * @param string $addressType
110 * @param \WC_Order $order
111 * @return void
112 */
113 public function show_selected_pickup_point_on_confirmation(string $addressType, \WC_Order $order): void
114 {
115 if ($addressType === 'shipping' && $order->meta_exists('_sendy_pickup_point_id')) {
116 echo wp_kses(
117 View::fromTemplate('checkout/order_confirmation.php')->render([
118 'pickup_point' => $order->get_meta('_sendy_pickup_point_data')
119 ]),
120 View::ALLOWED_TAGS
121 );
122 }
123 }
124
125 /**
126 * Get the instance id of the selected shipment method
127 *
128 * This ID is used to correctly store the selected pickup point and makes it possible to offer pickup point delivery
129 * for multiple carriers.
130 *
131 * @return int|null
132 */
133 private function get_shipping_method_instance_id(): ?int
134 {
135 $selectedMethods = WC()->session->get('chosen_shipping_methods');
136
137 $pickupPointDelivery = array_filter(
138 $selectedMethods,
139 function ($item) { return str_starts_with($item, 'sendy_pickup_point'); }
140 );
141
142 if (count($pickupPointDelivery) > 0) {
143 [$name, $instanceId] = explode(':', $pickupPointDelivery[0]);
144
145 return (int) $instanceId;
146 }
147
148 return null;
149 }
150
151 /**
152 * Get the carrier for the shipping method
153 *
154 * This method is used to load the correct pick-up points for a carrier in the checkout
155 *
156 * @param int $instance_id
157 * @return string|null
158 */
159 private function get_carrier_for_shipping_method(int $instance_id): ?string
160 {
161 $options = get_option("woocommerce_sendy_pickup_point_{$instance_id}_settings");
162
163 return $options['carrier'] ?? null;
164 }
165 }
166