PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.21
Timetics – Appointment Booking Calendar & Scheduling v1.0.21
1.0.61 1.0.60 1.0.59 1.0.58 1.0.57 1.0.56 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 All 62 releases
timetics / core / integrations / woocommerce / hooks.php

hooks.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.21, at core/integrations/woocommerce/hooks.php

407 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WooCommerce Hooks Class
4 *
5 * @package Timetics
6 */
7 namespace Timetics\Core\Integrations\Woocommerce;
8
9 use Timetics\Core\Appointments\Appointment;
10 use Timetics\Core\Bookings\Booking;
11 use Timetics\Core\Customers\Customer;
12 use Timetics\Utils\Singleton;
13 use Timetics\Core\Emails\New_Event_Email;
14 use Timetics\Core\Emails\New_Event_Customer_Email;
15
16 /**
17 * Class Hooks
18 */
19 class Hooks {
20 use Singleton;
21
22 /**
23 * Initialize
24 *
25 * @return void
26 */
27 public function init() {
28 Cart_Api::instance();
29
30 add_action( 'woocommerce_init', [$this, 'load_woocommerce_required_files'] );
31
32 add_action( 'woocommerce_thankyou', [$this, 'update_booking_payment_status'] );
33
34 add_filter( 'timetics_get_settings', [$this, 'add_woocommerce_currency_support'] );
35
36 add_filter( 'woocommerce_product_query', [$this, 'modify_woocommerce_shop'] );
37
38 add_action( 'woocommerce_coupon_options_usage_restriction', [$this, 'add_meeting_coupon_restriction_option'], 10, 2 );
39
40 add_action( 'woocommerce_coupon_options_save', [$this, 'save_coupon_option'], 10, 2 );
41
42 add_filter( 'woocommerce_coupon_get_products', [$this, 'assign_meeting_coupon_products'], 10, 2 );
43
44 add_filter( 'timetics_settings', [$this, 'add_wc_checkout_url'] );
45
46 add_filter( 'woocommerce_checkout_fields', [$this, 'hide_checkout_fields'] );
47
48 add_filter( 'woocommerce_checkout_posted_data', [$this, 'modify_order_data'] );
49
50 add_action( 'timetics_after_booking_create', [$this, 'add_product_to_cart'], 10, 4 );
51
52 add_filter( 'timetics_currency', [ $this, 'support_woocommerce_currency' ] );
53
54 add_action( 'admin_init', [ $this, 'create_term_timetics_meeting' ] );
55
56 add_action( 'wp_head', [ $this, 'hide_regular_price_checkout_css' ] );
57
58 }
59
60 /**
61 * Load all required files and functions
62 *
63 * @return void
64 */
65 public function load_woocommerce_required_files() {
66 if ( ! WC()->is_rest_api_request() ) {
67 return;
68 }
69
70 WC()->frontend_includes();
71
72 if ( null === WC()->cart && function_exists( 'wc_load_cart' ) ) {
73 wc_load_cart();
74 }
75 }
76
77 /**
78 * Add product data on woocommerce product data
79 *
80 * @return void
81 */
82 public function add_product_data_store( $stores ) {
83 $stores['product'] = 'Timetics\Core\Integrations\Woocommerce\Product_Data_Store';
84
85 return $stores;
86 }
87
88 /**
89 * Update booking payment status
90 *
91 * @param integer $order_id
92 *
93 * @return void
94 */
95 public function update_booking_payment_status( $order_id ) {
96
97 $order = wc_get_order( $order_id );
98
99 if ( ! $order->is_paid() ) {
100 WC()->session->set( 'timetics_data', null );
101 return;
102 }
103
104 $session_data = WC()->session->get( 'timetics_data' );
105
106 if ( ! $session_data ) {
107 return;
108 }
109
110 $booking = new Booking( $session_data['booking_id'] );
111
112 $booking->update( [
113 'post_status' => 'completed',
114 'payment_status' => 'completed',
115 'payment_method' => 'woocommerce',
116 ] );
117
118 $booking->create_event();
119 $new_event_email = new New_Event_Email( $booking );
120 $new_event_email->send();
121
122 $new_event_customer_email = new New_Event_Customer_Email( $booking );
123 $new_event_customer_email->send();
124
125 WC()->session->set( 'timetics_data', null );
126
127 }
128
129 /**
130 * Support woocommerce currency
131 *
132 * @param array $settings
133 *
134 * @return array
135 */
136 public function add_woocommerce_currency_support( $settings ) {
137
138 if ( ! function_exists( 'WC' ) ) {
139 return $settings;
140 }
141
142 $wc_integration = timetics_get_option( 'wc_integration' );
143
144 if ( ! $wc_integration ) {
145 return $settings;
146 }
147
148 $settings['currency'] = get_woocommerce_currency();
149
150 return $settings;
151 }
152
153 /**
154 * Hide timetics meeting from woocommerce shop page
155 *
156 * @param Object $query
157 *
158 * @return Object
159 */
160 public function modify_woocommerce_shop( $query ) {
161 $category_slug = 'timetics-meeting'; // Replace 'category-slug' with the desired category slug
162
163 // Get product category ID
164 $category = get_term_by( 'slug', $category_slug, 'product_cat' );
165
166 if ( $category ) {
167 $category_id = $category->term_id;
168
169 // Exclude products assigned to the category
170 $query->set( 'tax_query', array(
171 array(
172 'taxonomy' => 'product_cat',
173 'field' => 'term_id',
174 'terms' => $category_id,
175 'operator' => 'NOT IN',
176 ),
177 ) );
178 }
179
180 return $query;
181 }
182
183 /**
184 * Set coupon products
185 *
186 * @param integer $coupon_id
187 * @param Object $coupon
188 *
189 * @return void
190 */
191 public function save_coupon_option( $coupon_id, $coupon ) {
192 $meeting_ids = ! empty( $_POST['timetics_meeting_ids'] ) ? array_map( 'intval', $_POST['timetics_meeting_ids'] ) : [];
193
194 $coupon_products = $coupon->get_product_ids();
195 $coupon_products = array_merge( $coupon_products, $meeting_ids );
196 $coupon->update_meta_data( 'timetics_meeting_ids', $meeting_ids );
197
198 $coupon->set_product_ids( $coupon_products );
199 $coupon->save();
200 }
201
202 /**
203 * Add woocommerce coupon restriction option
204 *
205 * @param int $coupon_id
206 * @param Object $coupon
207 *
208 * @return void
209 */
210 public function add_meeting_coupon_restriction_option( $coupon_id, $coupon ) {
211 include_once TIMETICS_PLUGIN_DIR . '/templates/woocommerce/coupon-restriction-option.php';
212 }
213
214 /**
215 * Add checkout url on settings
216 *
217 * @param array $settings
218 *
219 * @return array
220 */
221 public function add_wc_checkout_url( $settings ) {
222 if ( ! timetics_is_woocommerce_active() ) {
223 return $settings;
224 }
225
226 $settings['wc_checkout_url'] = wc_get_checkout_url();
227
228 return $settings;
229 }
230
231 /**
232 * Hide all fields from checkout page
233 *
234 * @param array $fields
235 *
236 * @return array
237 */
238 public function hide_checkout_fields( $fields ) {
239 $session_data = WC()->session->get( 'timetics_data' );
240
241 if ( ! $session_data ) {
242 return $fields;
243 }
244
245 $fields['billing'] = array();
246
247 // Hide all shipping fields
248 $fields['shipping'] = array();
249
250 $fields['order'] = array();
251
252 return $fields;
253 }
254
255 /**
256 * Modify order posted data
257 *
258 * @param array $data
259 *
260 * @return array
261 */
262 public function modify_order_data( $data ) {
263 $session_data = WC()->session->get( 'timetics_data' );
264
265 if ( ! $session_data ) {
266 return $data;
267 }
268
269 $booking = new Booking( $session_data['booking_id'] );
270 $customer = new Customer( $booking->get_customer_id() );
271
272 $first_name = $customer->get_first_name();
273 $last_name = $customer->get_last_name();
274 $email = $customer->get_email();
275 $phone = $customer->get_phone();
276
277 if ( $first_name ) {
278 $data['billing_first_name'] = $first_name;
279 }
280
281 if ( $last_name ) {
282 $data['billing_last_name'] = $last_name;
283 }
284
285 if ( $email ) {
286 $data['billing_email'] = $email;
287 }
288
289 if ( $phone ) {
290 $data['billing_phone'] = $phone;
291 }
292
293 return $data;
294 }
295
296 /**
297 * Add to cart timetics appointment
298 *
299 * @param integer $booking_id
300 * @param integer $customer_id
301 * @param integer $meeting_id
302 * @param array $data
303 *
304 * @return void
305 */
306 public function add_product_to_cart( $booking_id, $customer_id, $meeting_id, $data ) {
307 if ( 'woocommerce' !== $data['payment_method'] ) {
308 return;
309 }
310
311 $booking = new Booking( $booking_id );
312 $meeting_id = $booking->get_appointment();
313 if( !empty($data['seats']) && is_array( $data['seats'])) {
314 $quantity = count( $data['seats'] );
315 }else {
316 $quantity = 1;
317 }
318 $price = $booking->get_total();
319
320
321 // Set session for timetics data for woocommerce.
322 WC()->session->set( 'timetics_data', [
323 'booking_id' => $booking_id,
324 'meeting_id' => $meeting_id,
325 'price' => $price,
326 ] );
327
328 // Remove all items from cart.
329 WC()->cart->empty_cart();
330
331 // Preparing for add to cart.
332 $meeting = new Appointment( $meeting_id );
333 $product_id = $meeting->get_wc_product_id();
334
335
336 if ( ! $product_id ) {
337 $product = new Product();
338 $product_id = $product->create( $meeting );
339 }
340
341 $wc_product = wc_get_product( $product_id );
342 if ( ! $wc_product->get_price() ) {
343 update_post_meta( $product_id, '_regular_price', wc_format_decimal( $price ) );
344 update_post_meta( $product_id, '_price', wc_format_decimal( $price ) );
345 }
346
347 if ( ! $product_id ) {
348 return;
349 }
350
351 WC()->cart->add_to_cart( $product_id, $quantity );
352 }
353
354 /**
355 * Suport woocmmerce currency
356 *
357 * @param string $currency
358 *
359 * @return string
360 */
361 public function support_woocommerce_currency( $currency ) {
362 if ( ! timetics_is_woocommerce_active() ) {
363 return $currency;
364 }
365
366 return get_woocommerce_currency();
367 }
368
369 /**
370 * Create a category if woocommerce loaded
371 *
372 * @return void
373 */
374 public function create_term_timetics_meeting() {
375 if ( term_exists( 'timetics-meeting', 'product_cat' ) ) {
376 return;
377 }
378
379 // Create new woocommerce product category for timetics meeting
380 timetics_add_woocommerce_product_cat();
381 }
382
383 public function hide_regular_price_checkout_css() {
384 $session = WC()->session;
385 if ( (is_checkout() || is_cart()) && $session ) {
386 $timetics_data = $session->get('timetics_data');
387 if( isset($timetics_data) ) {
388 ?>
389 <style type="text/css">
390 .wc-block-components-order-summary .wc-block-components-order-summary-item__individual-prices {
391 display: none;
392 }
393 .wc-block-cart-item__prices,
394 .woocommerce-cart-form__cart-item.cart_item .product-price {
395 display: none;
396 }
397 .wc-block-cart-item__quantity,
398 .woocommerce-cart-form__cart-item.cart_item .product-quantity {
399 display: none;
400 }
401 </style>
402 <?php
403 }
404 }
405 }
406 }
407