forked from birdbrain/starward-woocommerce-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarward-woocommerce.php
293 lines (266 loc) · 11.1 KB
/
starward-woocommerce.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* Plugin Name: Starward WooCommerce
* Plugin URI: https://github.com/birdbrain/starward-woocommerce-plugin
* Description: This plugin creates custom API endpoints and extends existing WooCommerce REST API responses
* Version: 1.0.0
* Author: BirdBrain
* Author URI: [email protected]
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: starward-woocommerce
*/
/* ------------------------------------------------------------------------
Manipulating the WooCommerce Products Query to allow filtering
by attribute slug and term ids ( e.g. ?pa_color=21,22&pa_size=30 )
------------------------------------------------------------------------ */
function filter_product_category_multiple_attributes( $query ) {
if ($query->is_main_query()) {
return;
}
// // Filter by multiple attributes and terms.
foreach ( wc_get_attribute_taxonomy_names() as $attribute ) {
if ( isset($_GET[$attribute]) ) {
$array = array(
'relation' => 'AND'
);
foreach ( wc_get_attribute_taxonomy_names() as $attribute ) {
if ( isset($_GET[$attribute]) ) {
$array[] = array(
'taxonomy' => $attribute,
'field' => 'term_id',
'terms' => explode(',', $_GET[$attribute]),
'operator' => 'IN'
);
}
}
$tax_query = $query->get( 'tax_query' );
$tax_query[] = $array;
$query->set( 'tax_query', $tax_query );
break;
}
}
return $query;
}
add_action( 'pre_get_posts', 'filter_product_category_multiple_attributes' );
/* ------------------------------------------------------------------------
Manipulating the WordPress Product response
------------------------------------------------------------------------ */
function filter_woocommerce_rest_prepare_product_object( $response, $object, $request ) {
if( empty( $response->data ) ) {
return $response;
}
$attribute_taxonomies = wc_get_attribute_taxonomies();
// Loop through the attributes on current product
$attributes = $response->data['attributes'];
foreach($attributes as $attrkey => $attribute) {
/* ########################################################
- Adding new swatch key to attribute response for color attributes,
which holds the hex code for each swatch color option
######################################################## */
// Get an array of attributes whose attribute type is color
$color_type_attribute_taxonomies = array_filter($attribute_taxonomies, function($attribute_taxonomy) {
return $attribute_taxonomy->attribute_type == 'color';
});
// Loop through the color type attributes
foreach($color_type_attribute_taxonomies as $tax_object) {
//Check if current attribute is a color type attribute
if ($attribute['id'] == $tax_object->attribute_id) {
// Get current attribute's options
$options = $response->data['attributes'][$attrkey]['options'];
// Get current attribute's terms
$color_terms = get_terms('pa_' . $tax_object->attribute_name);
foreach( $options as $option ) {
foreach($color_terms as $term) {
if ($term->name == $option) {
// Add a new swatch with hex value for each color option
$response->data['attributes'][$attrkey]['swatches'][$option] = get_term_meta( $term->term_id, 'product_attribute_color', true);
}
}
}
}
}
/* ########################################################
- Adding attribute taxonomy to the attribute response
- Adding attribute identifier to the attribute response
- Adding more detailed option data to the attribute options response
######################################################## */
foreach($attribute_taxonomies as $attribute_taxonomy) {
if ($attribute['id'] == $attribute_taxonomy->attribute_id) {
/* Add slug to current attribute response */
$response->data['attributes'][$attrkey]['taxonomy'] = ('pa_' . $attribute_taxonomy->attribute_name);
/* Add attribute identifier to current attribute response */
$response->data['attributes'][$attrkey]['slug'] = $attribute_taxonomy->attribute_name;
/* Replace default options data with detailed options data for current attribute */
$options = $response->data['attributes'][$attrkey]['options'];
$new_options = array();
$attribute_terms = get_terms('pa_' . $attribute_taxonomy->attribute_name);
foreach( $options as $option ) {
foreach($attribute_terms as $attribute_term) {
if ($attribute_term->name == $option) {
$new_options[] = (object) [
id => $attribute_term->term_id,
name => $attribute_term->name,
slug => $attribute_term->slug,
taxonomy => $attribute_term->taxonomy,
description => $attribute_term->description,
count => $attribute_term->count
];
}
}
}
$response->data['attributes'][$attrkey]['options'] = $new_options;
}
}
}
/* ########################################################
- Replacing Variation IDs with Variation details
######################################################## */
// Get the current product object
$variation_ids = $response->data['variations'];
$detailed_variations = array_map(function($variation_id) {
$variation = wc_get_product($variation_id);
return (object) [
'variation_id' => $variation->get_id(),
'image_url' => wp_get_attachment_url($variation->get_image_id()),
'variation_regular_price' => $variation->get_regular_price(),
'variation_sale_price' => $variation->get_sale_price(),
'attributes' => $variation->get_attributes(),
'is_on_sale' => $variation->is_on_sale()
];
}, $variation_ids);
$response->data['variations'] = $detailed_variations;
/* ########################################################
- Get ALL Variation attributes for a product
######################################################## */
if ($response->data['type'] == 'variable') {
$variation_attributes = wc_get_product($response->data['id'])->get_variation_attributes();
$variation_attributes = array_map(function($attribute) {
return (array_values($attribute));
}, $variation_attributes);
$response->data['variation_attributes'] = $variation_attributes;
}
/* Return new response */
return $response;
}
add_filter( 'woocommerce_rest_prepare_product_object', 'filter_woocommerce_rest_prepare_product_object', 10, 3 );
/* ------------------------------------------------------------------------
API Endpoint to get all product filters for a specific category
------------------------------------------------------------------------ */
add_action('rest_api_init', function () {
$namespace = 'starward/';
register_rest_route( $namespace, '/products/filters/category/(?P<category_id>.*?)', array(
'methods' => 'GET',
'callback' => 'get_product_category_filters',
));
});
function get_product_category_attribute_terms($category_id) {
// Get products in category id selected
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => -1, // return all products (offset ignored with -1)
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $category_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
$products_response = new WP_Query($args);
$products = $products_response->posts;
// Get product attribute details
$attribute_taxonomies = wc_get_attribute_taxonomies();
// Initialize response array
$category_attribute_terms = array();
// For each product attribute
foreach($attribute_taxonomies as $attribute_taxonomy) {
// Get all attribute options for each product in the category
$options = array_map(function($product) use ($attribute_taxonomy) {
return get_the_terms($product->ID, 'pa_' . $attribute_taxonomy->attribute_name);
}, $products);
// Remove duplicate options
$unique_options =
array_values(
array_unique(
array_merge(...$options),
SORT_REGULAR
)
);
// Push attribute details and options to attribute terms array
$category_attribute_terms[] = (object) [
'id' => $attribute_taxonomy->attribute_id,
'name' => $attribute_taxonomy->attribute_name,
'label' => $attribute_taxonomy->attribute_label,
'slug' => 'pa_' . $attribute_taxonomy->attribute_name,
'options' => $unique_options
];
}
return $category_attribute_terms;
}
function get_product_category_subcategories($category_id) {
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $category_id,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
return $subcats;
}
function get_product_category_price_min_max($category_id) {
// Get products ordered by price (lowest to highest)
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'asc',
'ignore_sticky_posts' => 1,
'posts_per_page' => -1, // return all products (offset ignored with -1)
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => $category_id,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
'operator' => 'NOT IN'
)
)
);
$products_response = new WP_Query($args);
$products = $products_response->posts;
// Get lowest and highest price from first and last product
$min_price = wc_get_product( reset($products)->ID )->get_price();
$max_price = wc_get_product( end($products)->ID )->get_price();
return (object) [
'min_price' => $min_price,
'max_price' => $max_price
];
}
function get_product_category_filters($data) {
// Get the category id from the request query
$category_id = $data['category_id'];
// Return category filters object
return (object) [
'attributes' => get_product_category_attribute_terms($category_id),
'subcategories' => get_product_category_subcategories($category_id),
'price' => get_product_category_price_min_max($category_id)
];
}
?>