PluginProbe
Orderable – Restaurant & Food Ordering System / 1.19.0
Orderable – Restaurant & Food Ordering System v1.19.0
0.1.4 0.1.5 0.2.0 1.0.0 1.1.0 1.1.1 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.19.1 1.19.2 1.2.0 1.20.0 1.20.1 All 44 releases
orderable / inc / modules / timings / class-timings-checkout.php

class-timings-checkout.php in Orderable – Restaurant & Food Ordering System 1.19.0, at inc/modules/timings/class-timings-checkout.php

250 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 * Module: Timings checkout.
4 *
5 * @package Orderable/Classes
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Timings checkout class.
12 */
13 class Orderable_Timings_Checkout {
14 /**
15 * Init.
16 */
17 public static function run() {
18 add_action( 'woocommerce_review_order_after_shipping', array( __CLASS__, 'output_timing_fields' ) );
19 add_action( 'woocommerce_checkout_process', array( __CLASS__, 'validate_checkout' ) );
20 add_action( 'woocommerce_checkout_update_order_meta', array( __CLASS__, 'process_checkout' ) );
21 add_filter( 'woocommerce_order_button_html', array( __CLASS__, 'place_order_button' ) );
22 }
23
24 /**
25 * Output timing fields at checkout.
26 *
27 * @throws Exception
28 */
29 public static function output_timing_fields() {
30 if ( ! self::has_available_methods() ) {
31 return;
32 }
33
34 $location = Orderable_Location::get_selected_location();
35 $service = Orderable_Services::get_selected_service();
36 $service_dates = $location->get_service_dates();
37 $asap = $location->get_asap_settings();
38
39 // No dates required.
40 if ( true === $service_dates ) {
41 return;
42 } ?>
43
44 <?php if ( false === $service_dates ) { ?>
45 <tr class="orderable-order-timings orderable-order-timings--no-dates">
46 <th>
47 <?php
48 // Translators: %s Service type.
49 printf( esc_html__( '%s Date', 'orderable' ), esc_html( $service ) );
50 ?>
51 </th>
52 <td>
53 <?php
54 // Translators: %s Service type.
55 printf( esc_html__( 'Sorry, there are currently no slots available for %s.', 'orderable' ), esc_html( strtolower( $service ) ) );
56 ?>
57 </td>
58 </tr>
59 <?php } else { ?>
60 <tr class="orderable-order-timings orderable-order-timings--date">
61 <th>
62 <label for="orderable-date">
63 <strong>
64 <?php
65 // Translators: %s Service type.
66 printf( esc_html__( '%s Date', 'orderable' ), esc_html( $service ) );
67 ?>
68 </strong>
69 </label>
70 </th>
71 <td>
72 <select name="orderable_order_date" id="orderable-date" class="orderable-order-timings__date">
73 <option value=""><?php esc_html_e( 'Select a date...', 'orderable' ); ?></option>
74 <?php if ( ! empty( $asap['date'] ) ) { ?>
75 <option value="asap"><?php esc_html_e( 'As soon as possible', 'orderable' ); ?></option>
76 <?php } ?>
77 <?php foreach ( $service_dates as $service_date_data ) { ?>
78 <option value="<?php echo esc_attr( $service_date_data['timestamp'] ); ?>" data-orderable-slots="<?php echo wc_esc_json( json_encode( array_values( $service_date_data['slots'] ) ) ); ?>"><?php echo $service_date_data['formatted']; ?></option>
79 <?php } ?>
80 </select>
81 </td>
82 </tr>
83 <tr class="orderable-order-timings orderable-order-timings--time" style="display: none;">
84 <th>
85 <label for="orderable-time">
86 <strong>
87 <?php
88 // Translators: %s Service type.
89 printf( __( '%s Time', 'orderable' ), $service );
90 ?>
91 </strong>
92 </label>
93 </th>
94 <td>
95 <input type="hidden" value="" name="orderable_order_time_slot_id" />
96 <select name="orderable_order_time" id="orderable-time" class="orderable-order-timings__time" disabled="disabled">
97 <option value=""><?php _e( 'Select a time...', 'orderable' ); ?></option>
98 <?php if ( ! empty( $asap['time'] ) ) { ?>
99 <option value="asap"><?php esc_html_e( 'As soon as possible', 'orderable' ); ?></option>
100 <?php } ?>
101 </select>
102 </td>
103 </tr>
104 <?php } ?>
105 <?php
106 }
107
108 /**
109 * Validate checkout fields.
110 */
111 public static function validate_checkout() {
112 $order_date = empty( $_POST['orderable_order_date'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['orderable_order_date'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
113
114 if ( isset( $_POST['orderable_order_date'] ) && empty( $order_date ) ) {
115 wc_add_notice( __( 'Please select an order date.', 'orderable' ), 'error' );
116 }
117 }
118
119 /**
120 * Process checkout fields.
121 *
122 * @param int $order_id Order ID.
123 *
124 * @throws Exception
125 */
126 public static function process_checkout( $order_id ) {
127 $checkout_data = self::get_checkout_data();
128
129 if ( empty( $checkout_data ) ) {
130 return;
131 }
132
133 $order = wc_get_order( $order_id );
134
135 if ( empty( $order ) ) {
136 return;
137 }
138
139 foreach ( $checkout_data as $key => $data ) {
140 if ( ! $data['save'] ) {
141 continue;
142 }
143
144 $order->update_meta_data( $key, $data['value'] );
145 }
146
147 $order->save();
148 }
149
150 /**
151 * Get posted checkout data.
152 *
153 * @return mixed|void
154 * @throws Exception
155 */
156 public static function get_checkout_data() {
157 $data = array();
158
159 $location = Orderable_Location::get_selected_location();
160 $order_date = empty( $_POST['orderable_order_date'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['orderable_order_date'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
161
162 if ( $order_date && 'asap' !== $order_date ) {
163 $date_format = get_option( 'date_format' );
164 $timestamp_adjusted = Orderable_Timings::get_timestamp_adjusted( $order_date );
165 $order_date_formatted = date_i18n( $date_format, $timestamp_adjusted );
166
167 $data['_orderable_order_timestamp'] = array(
168 'save' => true,
169 'value' => absint( $order_date ),
170 );
171
172 $data['orderable_order_date'] = array(
173 'save' => true,
174 'value' => sanitize_text_field( $order_date_formatted ),
175 );
176 }
177
178 if ( $order_date && 'asap' === $order_date ) {
179 $dates = $location->get_service_dates();
180
181 if ( ! $dates ) {
182 return;
183 }
184
185 $earliest_order_date = $dates[0]['timestamp'];
186 $date_format = get_option( 'date_format' );
187 $timestamp_adjusted = Orderable_Timings::get_timestamp_adjusted( $earliest_order_date );
188 $order_date_formatted = date_i18n( $date_format, $timestamp_adjusted );
189
190 $data['_orderable_order_timestamp'] = array(
191 'save' => true,
192 'value' => absint( $earliest_order_date ),
193 );
194
195 $data['orderable_order_date'] = array(
196 'save' => true,
197 'value' => sanitize_text_field( $order_date_formatted ) . esc_html__( ' (As soon as possible)', 'orderable' ),
198 );
199 }
200
201 return apply_filters( 'orderable_checkout_data', $data );
202 }
203
204 /**
205 * Has available shipping methods.
206 *
207 * @return bool
208 */
209 public static function has_available_methods() {
210 $has_available_methods = false;
211 $packages = WC()->shipping()->get_packages();
212
213 if ( ! empty( $packages ) ) {
214 foreach ( $packages as $i => $package ) {
215 if ( empty( $package['rates'] ) ) {
216 continue;
217 }
218
219 $has_available_methods = true;
220 break;
221 }
222 }
223
224 return apply_filters( 'orderable_has_available_methods', $has_available_methods );
225 }
226
227 /**
228 * Modify place order button.
229 *
230 * @param string $button_html Button HTML.
231 * @return string
232 */
233 public static function place_order_button( $button_html ) {
234 $location = Orderable_Location::get_selected_location();
235 $service_dates = $location->get_service_dates();
236
237 if ( $service_dates ) {
238 return $button_html;
239 }
240
241 if ( strpos( $button_html, 'disabled' ) !== false ) {
242 return $button_html;
243 }
244
245 $button_html = str_replace( '<button', '<button disabled', $button_html );
246
247 return $button_html;
248 }
249 }
250