PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.22
Timetics – Appointment Booking Calendar & Scheduling v1.0.22
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.22, at core/integrations/woocommerce/hooks.php

413 lines 11.0 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 $is_email_to_customer = timetics_get_option( 'booking_created_customer');
120 $is_email_to_host = timetics_get_option( 'booking_created_host');
121 if( $is_email_to_host ){
122 $new_event_email = new New_Event_Email( $booking );
123 $new_event_email->send();
124 }
125
126 if( $is_email_to_customer ){
127 $new_event_customer_email = new New_Event_Customer_Email( $booking );
128 $new_event_customer_email->send();
129 }
130
131 WC()->session->set( 'timetics_data', null );
132
133 }
134
135 /**
136 * Support woocommerce currency
137 *
138 * @param array $settings
139 *
140 * @return array
141 */
142 public function add_woocommerce_currency_support( $settings ) {
143
144 if ( ! function_exists( 'WC' ) ) {
145 return $settings;
146 }
147
148 $wc_integration = timetics_get_option( 'wc_integration' );
149
150 if ( ! $wc_integration ) {
151 return $settings;
152 }
153
154 $settings['currency'] = get_woocommerce_currency();
155
156 return $settings;
157 }
158
159 /**
160 * Hide timetics meeting from woocommerce shop page
161 *
162 * @param Object $query
163 *
164 * @return Object
165 */
166 public function modify_woocommerce_shop( $query ) {
167 $category_slug = 'timetics-meeting'; // Replace 'category-slug' with the desired category slug
168
169 // Get product category ID
170 $category = get_term_by( 'slug', $category_slug, 'product_cat' );
171
172 if ( $category ) {
173 $category_id = $category->term_id;
174
175 // Exclude products assigned to the category
176 $query->set( 'tax_query', array(
177 array(
178 'taxonomy' => 'product_cat',
179 'field' => 'term_id',
180 'terms' => $category_id,
181 'operator' => 'NOT IN',
182 ),
183 ) );
184 }
185
186 return $query;
187 }
188
189 /**
190 * Set coupon products
191 *
192 * @param integer $coupon_id
193 * @param Object $coupon
194 *
195 * @return void
196 */
197 public function save_coupon_option( $coupon_id, $coupon ) {
198 $meeting_ids = ! empty( $_POST['timetics_meeting_ids'] ) ? array_map( 'intval', $_POST['timetics_meeting_ids'] ) : [];
199
200 $coupon_products = $coupon->get_product_ids();
201 $coupon_products = array_merge( $coupon_products, $meeting_ids );
202 $coupon->update_meta_data( 'timetics_meeting_ids', $meeting_ids );
203
204 $coupon->set_product_ids( $coupon_products );
205 $coupon->save();
206 }
207
208 /**
209 * Add woocommerce coupon restriction option
210 *
211 * @param int $coupon_id
212 * @param Object $coupon
213 *
214 * @return void
215 */
216 public function add_meeting_coupon_restriction_option( $coupon_id, $coupon ) {
217 include_once TIMETICS_PLUGIN_DIR . '/templates/woocommerce/coupon-restriction-option.php';
218 }
219
220 /**
221 * Add checkout url on settings
222 *
223 * @param array $settings
224 *
225 * @return array
226 */
227 public function add_wc_checkout_url( $settings ) {
228 if ( ! timetics_is_woocommerce_active() ) {
229 return $settings;
230 }
231
232 $settings['wc_checkout_url'] = wc_get_checkout_url();
233
234 return $settings;
235 }
236
237 /**
238 * Hide all fields from checkout page
239 *
240 * @param array $fields
241 *
242 * @return array
243 */
244 public function hide_checkout_fields( $fields ) {
245 $session_data = WC()->session->get( 'timetics_data' );
246
247 if ( ! $session_data ) {
248 return $fields;
249 }
250
251 $fields['billing'] = array();
252
253 // Hide all shipping fields
254 $fields['shipping'] = array();
255
256 $fields['order'] = array();
257
258 return $fields;
259 }
260
261 /**
262 * Modify order posted data
263 *
264 * @param array $data
265 *
266 * @return array
267 */
268 public function modify_order_data( $data ) {
269 $session_data = WC()->session->get( 'timetics_data' );
270
271 if ( ! $session_data ) {
272 return $data;
273 }
274
275 $booking = new Booking( $session_data['booking_id'] );
276 $customer = new Customer( $booking->get_customer_id() );
277
278 $first_name = $customer->get_first_name();
279 $last_name = $customer->get_last_name();
280 $email = $customer->get_email();
281 $phone = $customer->get_phone();
282
283 if ( $first_name ) {
284 $data['billing_first_name'] = $first_name;
285 }
286
287 if ( $last_name ) {
288 $data['billing_last_name'] = $last_name;
289 }
290
291 if ( $email ) {
292 $data['billing_email'] = $email;
293 }
294
295 if ( $phone ) {
296 $data['billing_phone'] = $phone;
297 }
298
299 return $data;
300 }
301
302 /**
303 * Add to cart timetics appointment
304 *
305 * @param integer $booking_id
306 * @param integer $customer_id
307 * @param integer $meeting_id
308 * @param array $data
309 *
310 * @return void
311 */
312 public function add_product_to_cart( $booking_id, $customer_id, $meeting_id, $data ) {
313 if ( 'woocommerce' !== $data['payment_method'] ) {
314 return;
315 }
316
317 $booking = new Booking( $booking_id );
318 $meeting_id = $booking->get_appointment();
319 if( !empty($data['seats']) && is_array( $data['seats'])) {
320 $quantity = count( $data['seats'] );
321 }else {
322 $quantity = 1;
323 }
324 $price = $booking->get_total();
325
326
327 // Set session for timetics data for woocommerce.
328 WC()->session->set( 'timetics_data', [
329 'booking_id' => $booking_id,
330 'meeting_id' => $meeting_id,
331 'price' => $price,
332 ] );
333
334 // Remove all items from cart.
335 WC()->cart->empty_cart();
336
337 // Preparing for add to cart.
338 $meeting = new Appointment( $meeting_id );
339 $product_id = $meeting->get_wc_product_id();
340
341
342 if ( ! $product_id ) {
343 $product = new Product();
344 $product_id = $product->create( $meeting );
345 }
346
347 $wc_product = wc_get_product( $product_id );
348 if ( ! $wc_product->get_price() ) {
349 update_post_meta( $product_id, '_regular_price', wc_format_decimal( $price ) );
350 update_post_meta( $product_id, '_price', wc_format_decimal( $price ) );
351 }
352
353 if ( ! $product_id ) {
354 return;
355 }
356
357 WC()->cart->add_to_cart( $product_id, $quantity );
358 }
359
360 /**
361 * Suport woocmmerce currency
362 *
363 * @param string $currency
364 *
365 * @return string
366 */
367 public function support_woocommerce_currency( $currency ) {
368 if ( ! timetics_is_woocommerce_active() ) {
369 return $currency;
370 }
371
372 return get_woocommerce_currency();
373 }
374
375 /**
376 * Create a category if woocommerce loaded
377 *
378 * @return void
379 */
380 public function create_term_timetics_meeting() {
381 if ( term_exists( 'timetics-meeting', 'product_cat' ) ) {
382 return;
383 }
384
385 // Create new woocommerce product category for timetics meeting
386 timetics_add_woocommerce_product_cat();
387 }
388
389 public function hide_regular_price_checkout_css() {
390 $session = WC()->session;
391 if ( (is_checkout() || is_cart()) && $session ) {
392 $timetics_data = $session->get('timetics_data');
393 if( isset($timetics_data) ) {
394 ?>
395 <style type="text/css">
396 .wc-block-components-order-summary .wc-block-components-order-summary-item__individual-prices {
397 display: none;
398 }
399 .wc-block-cart-item__prices,
400 .woocommerce-cart-form__cart-item.cart_item .product-price {
401 display: none;
402 }
403 .wc-block-cart-item__quantity,
404 .woocommerce-cart-form__cart-item.cart_item .product-quantity {
405 display: none;
406 }
407 </style>
408 <?php
409 }
410 }
411 }
412 }
413