PluginProbe
Timetics – Appointment Booking Calendar & Scheduling / 1.0.13
Timetics – Appointment Booking Calendar & Scheduling v1.0.13
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 / cart-api.php

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

126 lines 2.9 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 use Timetics\Base\Api;
11 use Timetics\Core\Appointments\Appointment;
12 use Timetics\Utils\Singleton;
13
14 /**
15 *
16 */
17 class Cart_Api extends Api {
18 use Singleton;
19
20 /**
21 * Store api namespace
22 *
23 * @var string
24 */
25 protected $namespace = 'timetics/v1';
26
27 /**
28 * Store rest base
29 *
30 * @var string
31 */
32 protected $rest_base = 'woocommerce';
33
34 /**
35 * Register rest routes
36 *
37 * @return void
38 */
39 public function register_routes() {
40 /**
41 * Register route
42 *
43 * @var void
44 */
45 register_rest_route(
46 $this->namespace, $this->rest_base . '/cart', [
47 [
48 'methods' => \WP_REST_Server::CREATABLE,
49 'callback' => [$this, 'set_up_woocommerce'],
50 'permission_callback' => function () {
51 return true;
52 },
53 ],
54 ]
55 );
56 }
57
58 /**
59 * Add to cart booking
60 *
61 * @param Object $requst
62 *
63 * @return JSON
64 */
65 public function set_up_woocommerce( $requst ) {
66 $data = json_decode( $requst->get_body(), true );
67
68 $booking_id = ! empty( $data['booking_id'] ) ? intval( $data['booking_id'] ) : 0;
69 $meeting_id = ! empty( $data['meeting_id'] ) ? intval( $data['meeting_id'] ) : 0;
70 $price = ! empty( $data['price'] ) ? intval( $data['price'] ) : 0;
71
72 // Set session for timetics data for woocommerce.
73 WC()->session->set( 'timetics_data', [
74 'booking_id' => $booking_id,
75 'meeting_id' => $meeting_id,
76 'price' => $price,
77 ] );
78
79 // Remove all items from cart.
80 WC()->cart->empty_cart();
81
82 // Product Add to cart
83 $this->add_to_cart();
84
85 $data = [
86 'success' => 1,
87 'status_code' => 200,
88 'data' => array_merge([
89 'checkout_url' => wc_get_checkout_url(),
90 ], WC()->session->get( 'timetics_data' ) )
91 ];
92
93 return rest_ensure_response( $data );
94 }
95
96 /**
97 * Add to cart for woocommerce
98 *
99 * @return void
100 */
101 public function add_to_cart() {
102 $session_data = WC()->session->get( 'timetics_data' );
103
104 if ( ! $session_data ) {
105 return false;
106 }
107
108 $meeting_id = $session_data['meeting_id'];
109
110 $meeting = new Appointment( $meeting_id );
111 $product_id = $meeting->get_wc_product_id();
112 $quantity = 1;
113
114 if ( ! $product_id ) {
115 $product = new Product();
116 $product_id = $product->create( $meeting );
117 }
118
119 if ( ! $product_id ) {
120 return;
121 }
122
123 WC()->cart->add_to_cart( $product_id, $quantity );
124 }
125 }
126