PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.64
Timetics – Appointment Booking Calendar & Scheduling v1.0.64
1.0.64 1.0.62 1.0.63 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 All 65 releases
timetics / core / integrations / woocommerce / cart-api.php

cart-api.php in Timetics – Appointment Booking Calendar & Scheduling 1.0.64, at core/integrations/woocommerce/cart-api.php

165 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WooCommerce Cart API
4 *
5 * @package Timetics
6 */
7
8 namespace Timetics\Core\Integrations\Woocommerce;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use Timetics\Base\Api;
13 use Timetics\Core\Appointments\Appointment;
14 use Timetics\Utils\Singleton;
15
16 /**
17 *
18 */
19 class Cart_Api extends Api {
20 use Singleton;
21
22 /**
23 * Store api namespace
24 *
25 * @var string
26 */
27 protected $namespace = 'timetics/v1';
28
29 /**
30 * Store rest base
31 *
32 * @var string
33 */
34 protected $rest_base = 'woocommerce';
35
36 /**
37 * Register rest routes
38 *
39 * @return void
40 */
41 public function register_routes() {
42 /**
43 * Register route
44 *
45 * @var void
46 */
47 register_rest_route(
48 $this->namespace, $this->rest_base . '/cart', [
49 [
50 'methods' => \WP_REST_Server::CREATABLE,
51 'callback' => [$this, 'set_up_woocommerce'],
52 'permission_callback' => function () {
53 return true;
54 },
55 ],
56 ]
57 );
58 }
59
60 /**
61 * Add to cart booking
62 *
63 * @param Object $requst
64 *
65 * @return JSON
66 */
67 public function set_up_woocommerce( $requst ) {
68 $data = json_decode( $requst->get_body(), true );
69
70 $booking_id = ! empty( $data['booking_id'] ) ? intval( $data['booking_id'] ) : 0;
71 $token = ! empty( $data['security_token'] ) ? sanitize_text_field( $data['security_token'] ) : '';
72
73 $booking = new \Timetics\Core\Bookings\Booking( $booking_id );
74 // everything from the booking record itself once ownership is proven.
75 if ( ! $booking_id || '' === $token || ! $booking->is_booking() ) {
76 return new \WP_HTTP_Response(
77 [
78 'success' => 0,
79 'status_code' => 404,
80 'message' => __( 'Invalid booking.', 'timetics' ),
81 ],
82 404
83 );
84 }
85
86 $stored = (string) $booking->get_security_token();
87
88 if ( '' === $stored || ! hash_equals( $stored, $token ) ) {
89 return new \WP_HTTP_Response(
90 [
91 'success' => 0,
92 'status_code' => 403,
93 'message' => __( 'Invalid booking token.', 'timetics' ),
94 ],
95 403
96 );
97 }
98
99 $meeting_id = (int) $booking->get_appointment();
100 $price = (int) $booking->get_total();
101
102 // Hooks::add_product_to_cart() already ran on timetics_after_booking_create and
103 // stored the meeting details ( date, time, duration, timezone, location ) in this
104 // session. Keep them - overwriting the whole array would drop them before checkout.
105 $session_data = WC()->session->get( 'timetics_data' );
106
107 if ( ! is_array( $session_data ) || empty( $session_data['booking_id'] ) || (int) $session_data['booking_id'] !== $booking_id ) {
108 $session_data = [];
109 }
110
111 // Set session for timetics data for woocommerce.
112 WC()->session->set( 'timetics_data', array_merge( $session_data, [
113 'booking_id' => $booking_id,
114 'meeting_id' => $meeting_id,
115 'price' => $price,
116 ] ) );
117
118 // Remove all items from cart.
119 WC()->cart->empty_cart();
120
121 // Product Add to cart
122 $this->add_to_cart();
123
124 $data = [
125 'success' => 1,
126 'status_code' => 200,
127 'data' => array_merge([
128 'checkout_url' => wc_get_checkout_url(),
129 ], WC()->session->get( 'timetics_data' ) )
130 ];
131
132 return rest_ensure_response( $data );
133 }
134
135 /**
136 * Add to cart for woocommerce
137 *
138 * @return void
139 */
140 public function add_to_cart() {
141 $session_data = WC()->session->get( 'timetics_data' );
142
143 if ( ! $session_data ) {
144 return false;
145 }
146
147 $meeting_id = $session_data['meeting_id'];
148
149 $meeting = new Appointment( $meeting_id );
150 $product_id = $meeting->get_wc_product_id();
151 $quantity = 1;
152
153 if ( ! $product_id ) {
154 $product = new Product();
155 $product_id = $product->create( $meeting );
156 }
157
158 if ( ! $product_id ) {
159 return;
160 }
161
162 WC()->cart->add_to_cart( $product_id, $quantity );
163 }
164 }
165