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

375 lines 9.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
57 /**
58 * Load all required files and functions
59 *
60 * @return void
61 */
62 public function load_woocommerce_required_files() {
63 if ( ! WC()->is_rest_api_request() ) {
64 return;
65 }
66
67 WC()->frontend_includes();
68
69 if ( null === WC()->cart && function_exists( 'wc_load_cart' ) ) {
70 wc_load_cart();
71 }
72 }
73
74 /**
75 * Add product data on woocommerce product data
76 *
77 * @return void
78 */
79 public function add_product_data_store( $stores ) {
80 $stores['product'] = 'Timetics\Core\Integrations\Woocommerce\Product_Data_Store';
81
82 return $stores;
83 }
84
85 /**
86 * Update booking payment status
87 *
88 * @param integer $order_id
89 *
90 * @return void
91 */
92 public function update_booking_payment_status( $order_id ) {
93
94 $order = wc_get_order( $order_id );
95
96 if ( ! $order->is_paid() ) {
97 WC()->session->set( 'timetics_data', null );
98 return;
99 }
100
101 $session_data = WC()->session->get( 'timetics_data' );
102
103 if ( ! $session_data ) {
104 return;
105 }
106
107 $booking = new Booking( $session_data['booking_id'] );
108
109 $booking->update( [
110 'post_status' => 'completed',
111 'payment_status' => 'completed',
112 'payment_method' => 'woocommerce',
113 ] );
114
115 $booking->create_event();
116 $new_event_email = new New_Event_Email( $booking );
117 $new_event_email->send();
118
119 $new_event_customer_email = new New_Event_Customer_Email( $booking );
120 $new_event_customer_email->send();
121
122 WC()->session->set( 'timetics_data', null );
123
124 }
125
126 /**
127 * Support woocommerce currency
128 *
129 * @param array $settings
130 *
131 * @return array
132 */
133 public function add_woocommerce_currency_support( $settings ) {
134
135 if ( ! function_exists( 'WC' ) ) {
136 return $settings;
137 }
138
139 $wc_integration = timetics_get_option( 'wc_integration' );
140
141 if ( ! $wc_integration ) {
142 return $settings;
143 }
144
145 $settings['currency'] = get_woocommerce_currency();
146
147 return $settings;
148 }
149
150 /**
151 * Hide timetics meeting from woocommerce shop page
152 *
153 * @param Object $query
154 *
155 * @return Object
156 */
157 public function modify_woocommerce_shop( $query ) {
158 $category_slug = 'timetics-meeting'; // Replace 'category-slug' with the desired category slug
159
160 // Get product category ID
161 $category = get_term_by( 'slug', $category_slug, 'product_cat' );
162
163 if ( $category ) {
164 $category_id = $category->term_id;
165
166 // Exclude products assigned to the category
167 $query->set( 'tax_query', array(
168 array(
169 'taxonomy' => 'product_cat',
170 'field' => 'term_id',
171 'terms' => $category_id,
172 'operator' => 'NOT IN',
173 ),
174 ) );
175 }
176
177 return $query;
178 }
179
180 /**
181 * Set coupon products
182 *
183 * @param integer $coupon_id
184 * @param Object $coupon
185 *
186 * @return void
187 */
188 public function save_coupon_option( $coupon_id, $coupon ) {
189 $meeting_ids = ! empty( $_POST['timetics_meeting_ids'] ) ? array_map( 'intval', $_POST['timetics_meeting_ids'] ) : [];
190
191 $coupon_products = $coupon->get_product_ids();
192 $coupon_products = array_merge( $coupon_products, $meeting_ids );
193 $coupon->update_meta_data( 'timetics_meeting_ids', $meeting_ids );
194
195 $coupon->set_product_ids( $coupon_products );
196 $coupon->save();
197 }
198
199 /**
200 * Add woocommerce coupon restriction option
201 *
202 * @param int $coupon_id
203 * @param Object $coupon
204 *
205 * @return void
206 */
207 public function add_meeting_coupon_restriction_option( $coupon_id, $coupon ) {
208 include_once TIMETICS_PLUGIN_DIR . '/templates/woocommerce/coupon-restriction-option.php';
209 }
210
211 /**
212 * Add checkout url on settings
213 *
214 * @param array $settings
215 *
216 * @return array
217 */
218 public function add_wc_checkout_url( $settings ) {
219 if ( ! timetics_is_woocommerce_active() ) {
220 return $settings;
221 }
222
223 $settings['wc_checkout_url'] = wc_get_checkout_url();
224
225 return $settings;
226 }
227
228 /**
229 * Hide all fields from checkout page
230 *
231 * @param array $fields
232 *
233 * @return array
234 */
235 public function hide_checkout_fields( $fields ) {
236 $session_data = WC()->session->get( 'timetics_data' );
237
238 if ( ! $session_data ) {
239 return $fields;
240 }
241
242 $fields['billing'] = array();
243
244 // Hide all shipping fields
245 $fields['shipping'] = array();
246
247 $fields['order'] = array();
248
249 return $fields;
250 }
251
252 /**
253 * Modify order posted data
254 *
255 * @param array $data
256 *
257 * @return array
258 */
259 public function modify_order_data( $data ) {
260 $session_data = WC()->session->get( 'timetics_data' );
261
262 if ( ! $session_data ) {
263 return $data;
264 }
265
266 $booking = new Booking( $session_data['booking_id'] );
267 $customer = new Customer( $booking->get_customer_id() );
268
269 $first_name = $customer->get_first_name();
270 $last_name = $customer->get_last_name();
271 $email = $customer->get_email();
272 $phone = $customer->get_phone();
273
274 if ( $first_name ) {
275 $data['billing_first_name'] = $first_name;
276 }
277
278 if ( $last_name ) {
279 $data['billing_last_name'] = $last_name;
280 }
281
282 if ( $email ) {
283 $data['billing_email'] = $email;
284 }
285
286 if ( $phone ) {
287 $data['billing_phone'] = $phone;
288 }
289
290 return $data;
291 }
292
293 /**
294 * Add to cart timetics appointment
295 *
296 * @param integer $booking_id
297 * @param integer $customer_id
298 * @param integer $meeting_id
299 * @param array $data
300 *
301 * @return void
302 */
303 public function add_product_to_cart( $booking_id, $customer_id, $meeting_id, $data ) {
304 if ( 'woocommerce' !== $data['payment_method'] ) {
305 return;
306 }
307
308 $booking = new Booking( $booking_id );
309 $meeting_id = $booking->get_appointment();
310 $price = $booking->get_total();
311
312
313 // Set session for timetics data for woocommerce.
314 WC()->session->set( 'timetics_data', [
315 'booking_id' => $booking_id,
316 'meeting_id' => $meeting_id,
317 'price' => $price,
318 ] );
319
320 // Remove all items from cart.
321 WC()->cart->empty_cart();
322
323 // Preparing for add to cart.
324 $meeting = new Appointment( $meeting_id );
325 $product_id = $meeting->get_wc_product_id();
326 $quantity = 1;
327
328 if ( ! $product_id ) {
329 $product = new Product();
330 $product_id = $product->create( $meeting );
331 }
332
333 $wc_product = wc_get_product( $product_id );
334 if ( ! $wc_product->get_price() ) {
335 update_post_meta( $product_id, '_regular_price', wc_format_decimal( $price ) );
336 update_post_meta( $product_id, '_price', wc_format_decimal( $price ) );
337 }
338
339 if ( ! $product_id ) {
340 return;
341 }
342
343 WC()->cart->add_to_cart( $product_id, $quantity );
344 }
345
346 /**
347 * Suport woocmmerce currency
348 *
349 * @param string $currency
350 *
351 * @return string
352 */
353 public function support_woocommerce_currency( $currency ) {
354 if ( ! timetics_is_woocommerce_active() ) {
355 return $currency;
356 }
357
358 return get_woocommerce_currency();
359 }
360
361 /**
362 * Create a category if woocommerce loaded
363 *
364 * @return void
365 */
366 public function create_term_timetics_meeting() {
367 if ( term_exists( 'timetics-meeting', 'product_cat' ) ) {
368 return;
369 }
370
371 // Create new woocommerce product category for timetics meeting
372 timetics_add_woocommerce_product_cat();
373 }
374 }
375