· 5 years ago · Jun 14, 2020, 10:50 PM
1<?php
2
3
4 /*
5 Plugin Name: Custom API
6 Plugin URI:
7 description: Custom API routes for WooCommerce
8 Version:
9 Author:
10 Author URI:
11 License:
12 */
13
14 function single_product_by_slug( $slug ) {
15 global $product;
16
17 $args = array (
18 'post_type' => 'product',
19 'name' => $slug['slug']
20 );
21
22 $post = get_posts($args);
23 $product = wc_get_product( $post[0]->ID );
24
25 $data = [
26 'name' => $product->get_name(),
27 'price' => $product->get_price(),
28 'regular_price' => $product->get_regular_price(),
29 'sale_price' => $product->get_sale_price(),
30 'sale_from' => $product->get_date_on_sale_from(),
31 'sale_to' => $product->get_date_on_sale_to(),
32
33 'stock_quantity' => $product->get_stock_quantity(),
34
35
36
37 'colors' => array($product->get_attribute( 'color' )),
38 'images' => $product->get_image(),
39 'variations' => $product->get_children(),
40 ];
41
42
43 if ( empty( $post ) ) {
44 return null;
45 }
46 // print_r($slug);
47 return $data;
48 }
49
50 function products_by_slug_variation( $slug ) {
51 global $product;
52
53 $args = array (
54 'post_type' => 'product',
55 'name' => $slug['slug']
56 );
57
58 $post = get_posts($args);
59 $product = wc_get_product( $post[0]->ID );
60
61 $attachment_ids[0] = get_post_thumbnail_id( $product->id );
62 $attachment = wp_get_attachment_image_src($attachment_ids[0], false );
63
64 if($product->has_child()) {
65 $variations = $product->get_available_variations();
66 $data = [
67 'id' => $product->get_id(),
68 'name' => $product->get_name(),
69 'price' => $product->get_price(),
70 'regular_price' => $product->get_regular_price(),
71 'sale_price' => $product->get_sale_price(),
72 'image' => $attachment,
73 'stock_quantity' => $product->get_stock_quantity(),
74
75 // 'stock_quantity' => $product->get_available_variations(),
76
77
78 'stock_status' => $product->get_stock_status(),
79 'backorders' => $product->get_backorders(),
80 "variations" => $variations = $product->get_available_variations(),
81
82 'description' => $product->get_description(),
83
84 'weight' => $product->get_weight(),
85 'length' => $product->get_length(),
86 'width' => $product->get_width(),
87 'height' => $product->get_height(),
88 'dimensions' => $product->get_dimensions(),
89
90 'reviews_allowed' => $product->get_reviews_allowed(),
91 'rating_counts' => $product->get_rating_counts(),
92 'get_average_rating' => $product->get_average_rating(),
93 'get_review_count' => $product->get_review_count()
94
95 ];
96 } else {
97 $data = [
98 'id' => $product->get_id(),
99 'name' => $product->get_name(),
100 'price' => $product->get_price(),
101 'regular_price' => $product->get_regular_price(),
102 'sale_price' => $product->get_sale_price(),
103
104 'stock_quantity' => $product->get_stock_quantity(),
105
106
107 'stock_status' => $product->get_stock_status(),
108 'backorders' => $product->get_backorders(),
109 'image' => $attachment,
110
111 'description' => $product->get_description(),
112
113 'weight' => $product->get_weight(),
114 'length' => $product->get_length(),
115 'width' => $product->get_width(),
116 'height' => $product->get_height(),
117 'dimensions' => $product->get_dimensions(),
118
119 'reviews_allowed' => $product->get_reviews_allowed(),
120 'rating_counts' => $product->get_rating_counts(),
121 'get_average_rating' => $product->get_average_rating(),
122 'get_review_count' => $product->get_review_count()
123 ];
124 }
125
126
127 if ( empty( $post ) ) {
128 return null;
129 }
130
131 return $data;
132 }
133
134 function custom_product_list( $args ) {
135 global $product;
136
137 // var_dump("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", $args['price']);
138
139 $offset = 0;
140 $limit = 9;
141
142
143
144 // var_dump($min_price, $max_price);
145
146 if ( isset( $args['page']['d'])) {
147 $custom_page = $args['page'];
148 } else {
149 $custom_page = 1;
150 }
151
152 if ( isset( $args['colors'])) {
153 $custom_colors = explode(',', $args['colors']);
154 $custom_operator = "IN";
155 } else {
156 $custom_colors = '';
157 $custom_operator = "OUT";
158 }
159
160 if ( isset( $args['price'])) {
161 $min_price = intval($args['price']['min']);
162 $max_price = intval($args['price']['max']);
163 } else {
164 $min_price = 0;
165 $max_price = 99999;
166 };
167
168 // $min_price = intval('0');
169 // $max_price = intval('5');
170
171 // var_dump($min_price, $max_price);
172 $query_args = array(
173 'page' => $custom_page,
174 'limit' => $limit,
175 'paginate' => true,
176 'orderby' => 'date',
177 'order' => 'DESC',
178 'price_between' => array($min_price, $max_price),
179 'tax_query' => array(
180 array(
181 'taxonomy' => 'pa_color',
182 'field' => 'slug',
183 'terms' => $custom_colors,//$custom_colors,
184 'operator' => $custom_operator
185 ),
186 ),
187 'return' => 'ids'
188 );
189 $query = new WC_Product_Query( $query_args );
190
191
192 $products = $query->get_products();
193
194 $productResults = [
195 "total" => $products->total,
196 "totalPages" => floor(($limit / $products->total) * 10),
197 "offset" => $offset,
198 "page" => $custom_page,
199 "limit" => $limit,
200 "products" => [],
201 "colors" => $custom_colors,
202 "test " => $test,
203 ];
204
205 if ( sizeof($products) > 0 ) :
206 foreach($products->products as $productID) :
207
208 $attachment_ids[0] = get_post_thumbnail_id( $productID );
209 $attachment = wp_get_attachment_image_src($attachment_ids[0], false );
210 $custom_product = wc_get_product($productID);
211
212 $product_variation = [];
213 if($custom_product->has_child()) {
214 $variations = $custom_product->get_available_variations();
215 foreach($variations as $variation) {
216 $var = [
217 "attributes" => $variation['attributes'],
218 "image" => $variation['image']['src'],
219 ];
220 $product_variation[] = $var;
221 }
222 }
223
224 if($custom_product->has_child()) {
225 $custom_variations = $product_variation;
226 $variationPrice = [
227 'min' => $custom_product->get_variation_price(),
228 'max' => $custom_product->get_variation_price('max')
229 ];
230 $color = "";
231
232 } else {
233 $variationPrice = "";
234 $custom_variations = [];
235 $color = $custom_product->get_attribute('Color');
236 }
237
238 array_push( $productResults['products'], [
239 'id' => $custom_product->get_id(),
240 'name' => $custom_product->get_name(),
241 'slug' => $custom_product->get_slug(),
242 'price' => $custom_product->get_price(),
243 'currency' => get_woocommerce_currency_symbol(),
244
245 'image' => $attachment,
246 'variationPrice' => $variationPrice,
247
248 'custom_variations' => $product_variation,
249 'color' => $color,
250 ]);
251
252 endforeach;
253 // array_push( $productResults['products'], [
254
255 // ];
256 endif;
257
258 return $productResults;
259 }
260
261
262 add_filter(
263 'woocommerce_product_data_store_cpt_get_products_query',
264 function ( $query, $query_vars ) {
265 if ( ! empty( $query_vars['price_between'] ) ) {
266 $query['meta_query'][] = array(
267 'key' => '_price',
268 'value' => $query_vars['price_between'],
269 'type' => 'NUMERIC',
270 'compare' => 'BETWEEN',
271 );
272 }
273 return $query;
274 },
275 10,
276 2
277 );
278
279
280
281
282 function filter_options() {
283 global $product;
284
285 $response = [
286 "colors" => get_terms("pa_color"),
287 // "max-price" => woocommerce_price()
288 ];
289
290 return rest_ensure_response($response);
291 }
292
293 function get_args( ) {
294
295
296 $response = [];
297
298 $args = array();
299
300 $args['page'] = array(
301 'type' => 'number',
302 );
303 // var_dump($args, "sd");
304 return $args;
305 }
306
307 add_action('rest_api_init', 'register_routes');
308 function register_routes() {
309
310 // $str = '/custom-product-list?(?:page=(?P<page>[\d]+))?';
311 // parse_str($str, $output);
312
313 register_rest_route( 'wc/v3', '/product/slug=(?P<slug>[a-zA-Z0-9-]+)', array(
314 'method' => 'GET',
315 'callback' => 'single_product_by_slug'
316 ));
317 register_rest_route( 'wc/v3', '/product/slug=(?P<slug>[a-zA-Z0-9-]+)/variations', array(
318 'method' => 'GET',
319 'callback' => 'products_by_slug_variation'
320 ));
321 register_rest_route( 'wc/v3', '/filter-options', array(
322 'method' => 'GET',
323 'callback' => 'filter_options'
324 ));
325 register_rest_route( 'wc/v3', '/custom-product-list', array(
326 'method' => 'GET',
327 'callback' => 'custom_product_list',
328 'args' => get_args(),
329 ));
330
331 }