PluginProbe
Orderable – Restaurant & Food Ordering System / 0.2.0
Orderable – Restaurant & Food Ordering System v0.2.0
0.1.4 0.1.5 0.2.0 1.0.0 1.1.0 1.1.1 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.19.1 1.19.2 1.2.0 1.20.0 1.20.1 All 44 releases
orderable / inc / class-ajax.php

class-ajax.php in Orderable – Restaurant & Food Ordering System 0.2.0, at inc/class-ajax.php

226 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Ajax methods.
4 *
5 * @package Orderable/Classes
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Ajax class.
12 */
13 class Orderable_Ajax {
14 /**
15 * Run.
16 */
17 public static function run() {
18 $methods = array(
19 'get_product_options' => true,
20 'add_to_cart' => true,
21 'lookup_service' => true,
22 'get_onboard_woo_fields' => false,
23 );
24
25 self::add_ajax_methods( $methods, __CLASS__ );
26 }
27
28 /**
29 * Add ajax methods helper.
30 *
31 * @param array $methods
32 * @param object|string $class
33 */
34 public static function add_ajax_methods( $methods, $class ) {
35 if ( empty( $methods ) ) {
36 return;
37 }
38
39 foreach ( $methods as $method => $nopriv ) {
40 add_action( 'wp_ajax_orderable_' . $method, array( $class, $method ) );
41
42 if ( $nopriv ) {
43 add_action( 'wp_ajax_nopriv_orderable_' . $method, array( $class, $method ) );
44 }
45 }
46 }
47
48 /**
49 * Get product options for a variable product.
50 */
51 public static function get_product_options() {
52 $product_id = absint( filter_input( INPUT_POST, 'product_id', FILTER_SANITIZE_NUMBER_INT ) );
53
54 if ( empty( $product_id ) ) {
55 wp_send_json_error();
56 }
57
58 $product = wc_get_product( $product_id );
59
60 $response = array(
61 'product_id' => $product_id,
62 'product' => $product,
63 );
64
65 if ( 'variable' === $product->get_type() ) {
66 $attributes = Orderable_Products::get_available_attributes( $product );
67 $available_variations = $product->get_available_variations();
68 $variations_json = wp_json_encode( $available_variations );
69 }
70
71 $args = array( 'images' => true );
72
73 ob_start();
74
75 include ORDERABLE_TEMPLATES_PATH . 'product/options.php';
76
77 $response['html'] = ob_get_clean();
78
79 wp_send_json_success( $response );
80 }
81
82 /**
83 * AJAX add to cart.
84 */
85 public static function add_to_cart() {
86 ob_start();
87
88 // phpcs:disable WordPress.Security.NonceVerification.Missing
89 $product_id = absint( filter_input( INPUT_POST, 'product_id', FILTER_SANITIZE_NUMBER_INT ) );
90
91 if ( empty( $product_id ) ) {
92 return;
93 }
94
95 $variation_id = absint( filter_input( INPUT_POST, 'variation_id', FILTER_SANITIZE_NUMBER_INT ) );
96 $quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_POST['quantity'] ) );
97 $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
98 $product_status = get_post_status( $product_id );
99 $attributes = (array) json_decode( filter_input( INPUT_POST, 'attributes', FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES ), true );
100 $attributes = array_map( 'wp_unslash', $attributes );
101
102 if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $attributes ) && 'publish' === $product_status ) {
103 do_action( 'woocommerce_ajax_added_to_cart', $product_id );
104 }
105
106 WC_AJAX::get_refreshed_fragments();
107 // phpcs:enable
108 }
109
110 /**
111 * Lookup delivery and set chosen shipping method.
112 */
113 public static function lookup_service() {
114 $service = filter_input( INPUT_POST, 'service', FILTER_SANITIZE_STRING );
115
116 $address = array(
117 'city' => filter_input( INPUT_POST, 'city', FILTER_SANITIZE_STRING ),
118 'postcode' => filter_input( INPUT_POST, 'postcode', FILTER_SANITIZE_STRING ),
119 );
120
121 $delivery_pickup_methods = Orderable_Services::get_delivery_pickup_methods( $address );
122 $rates = array_pop( $delivery_pickup_methods );
123
124 if ( empty( array_filter( $delivery_pickup_methods ) ) ) {
125 wp_send_json_error( array(
126 'message' => __( 'Sorry, there are no delivery or pickup options available.', 'orderable' ),
127 ) );
128 }
129
130 if ( empty( $delivery_pickup_methods[ $service ] ) ) {
131 $other_service = 'delivery' === $service ? __( 'pickup', 'orderable' ) : __( 'delivery', 'orderable' );
132
133 wp_send_json_error( array(
134 'message' => sprintf( __( 'Sorry, there are no %s options available. Please try %s instead.', 'orderable' ), $service, $other_service ),
135 ) );
136 }
137
138 // Start session if it doesn't already exist.
139 if ( ! WC()->session->has_session() ) {
140 WC()->session->set_customer_session_cookie( true );
141 }
142
143 // Set customer data.
144 $customer = WC()->customer;
145 $customer->set_billing_city( $address['city'] );
146 $customer->set_billing_postcode( $address['postcode'] );
147 $customer->set_shipping_city( $address['city'] );
148 $customer->set_shipping_postcode( $address['postcode'] );
149 $customer->save();
150
151 do_action( 'woocommerce_checkout_update_user_meta', $customer->get_id(), array(
152 'billing_city' => $address['city'],
153 'billing_postcode' => $address['postcode'],
154 'shipping_city' => $address['city'],
155 'shipping_postcode' => $address['postcode'],
156 ) );
157
158 // Set the chosen shipping method.
159 $chosen = $delivery_pickup_methods[ $service ]->get_id();
160
161 WC()->session->set( 'chosen_shipping_methods', array( $chosen ) );
162
163 do_action( 'woocommerce_shipping_method_chosen', $chosen );
164
165 // Added this so right shipping method is selected at checkout.
166 // @see wc-cart-functions.php:425 `wc_get_chosen_shipping_method_for_package()`.
167 WC()->session->set( 'previous_shipping_methods', array( $rates ) );
168 WC()->session->set( 'shipping_method_counts', array( count( $rates ) ) );
169
170 $data = array(
171 'fragments' => apply_filters( 'orderable_service_fragments', array() ),
172 );
173
174 ob_start();
175 include ORDERABLE_MODULES_PATH . 'services/templates/service-selector.php';
176 $data['fragments']['.orderable-services-selector'] = ob_get_clean();
177
178 wp_send_json_success( $data );
179 }
180
181 /**
182 * Get countries states.
183 */
184 public static function get_onboard_woo_fields() {
185 $response = array(
186 'default_country' => self::get_default_country_options(),
187 'business_address' => WC()->countries->get_base_address(),
188 'business_address_2' => WC()->countries->get_base_address_2(),
189 'business_city' => WC()->countries->get_base_city(),
190 'business_postcode' => WC()->countries->get_base_postcode(),
191 );
192
193 wp_send_json_success( $response );
194 }
195
196 /**
197 * Get country/state options.
198 *
199 * @return string
200 */
201 public static function get_default_country_options() {
202 $countries_states = Orderable_Settings::get_countries_states();
203
204 if ( empty( $countries_states ) ) {
205 return false;
206 }
207
208 ob_start();
209
210 require ORDERABLE_INC_PATH . "/vendor/iconic-onboard/inc/class-settings.php";
211
212 $base = wc_get_base_location();
213
214 Orderable_Onboard_Settings::generate_select_field( array(
215 'id' => 'default_country',
216 'title' => __( 'Country / State', 'orderable' ),
217 'desc' => '',
218 'choices' => $countries_states,
219 'value' => implode( ':', array_filter( $base ) ),
220 'name' => '',
221 'class' => '',
222 ) );
223
224 return strip_tags( ob_get_clean(), '<option><optgroup>' );
225 }
226 }