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

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