| 1 |
<?php |
| 2 |
/** |
| 3 |
* Product_Variations_Controller. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\API; |
| 9 |
|
| 10 |
\defined( 'ABSPATH' ) || die; |
| 11 |
|
| 12 |
if ( ! class_exists( 'WC_REST_Product_Variations_Controller' ) ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
use Exception; |
| 17 |
use WC_Data; |
| 18 |
use WC_REST_Product_Variations_Controller; |
| 19 |
use WCPOS\WooCommercePOS\Logger; |
| 20 |
use WCPOS\WooCommercePOS\Services\Settings; |
| 21 |
use WP_Error; |
| 22 |
use WP_Query; |
| 23 |
use WP_REST_Request; |
| 24 |
use WP_REST_Response; |
| 25 |
use WP_REST_Server; |
| 26 |
|
| 27 |
/** |
| 28 |
* Product Tgas controller class. |
| 29 |
* |
| 30 |
* @NOTE: methods not prefixed with wcpos_ will override WC_REST_Product_Variations_Controller methods |
| 31 |
*/ |
| 32 |
class Product_Variations_Controller extends WC_REST_Product_Variations_Controller { |
| 33 |
use Traits\Product_Helpers; |
| 34 |
use Traits\Query_Helpers; |
| 35 |
use Traits\Uuid_Handler; |
| 36 |
use Traits\WCPOS_REST_API; |
| 37 |
|
| 38 |
/** |
| 39 |
* Endpoint namespace. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $namespace = 'wcpos/v1'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Store the request object for use in lifecycle methods. |
| 47 |
* |
| 48 |
* @var WP_REST_Request |
| 49 |
*/ |
| 50 |
protected $wcpos_request; |
| 51 |
|
| 52 |
/** |
| 53 |
* Dispatch request to parent controller, or override if needed. |
| 54 |
* |
| 55 |
* @param mixed $dispatch_result Dispatch result, will be used if not empty. |
| 56 |
* @param WP_REST_Request $request Request used to generate the response. |
| 57 |
* @param string $route Route matched for the request. |
| 58 |
* @param array $handler Route handler used for the request. |
| 59 |
*/ |
| 60 |
public function wcpos_dispatch_request( $dispatch_result, WP_REST_Request $request, $route, $handler ) { |
| 61 |
$this->wcpos_request = $request; |
| 62 |
|
| 63 |
add_filter( 'woocommerce_rest_prepare_product_variation_object', array( $this, 'wcpos_variation_response' ), 10, 3 ); |
| 64 |
add_action( 'woocommerce_rest_insert_product_variation_object', array( $this, 'wcpos_insert_product_variation_object' ), 10, 3 ); |
| 65 |
add_filter( 'woocommerce_rest_product_variation_object_query', array( $this, 'wcpos_product_variation_query' ), 10, 2 ); |
| 66 |
add_filter( 'posts_search', array( $this, 'wcpos_posts_search' ), 10, 2 ); |
| 67 |
|
| 68 |
/* |
| 69 |
* Check if the request is for all products and if the 'posts_per_page' is set to -1. |
| 70 |
* Optimised query for getting all product IDs. |
| 71 |
*/ |
| 72 |
if ( -1 == $request->get_param( 'posts_per_page' ) && null !== $request->get_param( 'fields' ) ) { |
| 73 |
return $this->wcpos_get_all_posts( $request ); |
| 74 |
} |
| 75 |
|
| 76 |
return $dispatch_result; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Register routes. |
| 81 |
*/ |
| 82 |
public function register_routes(): void { |
| 83 |
parent::register_routes(); |
| 84 |
|
| 85 |
register_rest_route( |
| 86 |
$this->namespace, |
| 87 |
'/products/variations', |
| 88 |
array( |
| 89 |
array( |
| 90 |
'methods' => WP_REST_Server::READABLE, |
| 91 |
'callback' => array( $this, 'wcpos_get_all_items' ), |
| 92 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 93 |
'args' => $this->get_collection_params(), |
| 94 |
), |
| 95 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 96 |
) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Add custom fields to the product schema. |
| 102 |
*/ |
| 103 |
public function get_item_schema() { |
| 104 |
$schema = parent::get_item_schema(); |
| 105 |
|
| 106 |
// Add the 'barcode' property if 'properties' exists and is an array. |
| 107 |
if ( isset( $schema['properties'] ) && \is_array( $schema['properties'] ) ) { |
| 108 |
$schema['properties']['barcode'] = array( |
| 109 |
'description' => /* translators: REST API schema field label or error message. */ __( 'Barcode', 'woocommerce-pos' ), |
| 110 |
'type' => 'string', |
| 111 |
'context' => array( 'view', 'edit' ), |
| 112 |
'readonly' => false, |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
// Check for 'stock_quantity' and allow decimal |
| 117 |
// Note: 'number' is the valid JSON schema type for decimals (not 'float'). |
| 118 |
if ( $this->wcpos_allow_decimal_quantities() && |
| 119 |
isset( $schema['properties']['stock_quantity'] ) && |
| 120 |
\is_array( $schema['properties']['stock_quantity'] ) ) { |
| 121 |
$schema['properties']['stock_quantity']['type'] = 'number'; |
| 122 |
} |
| 123 |
|
| 124 |
return $schema; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* Modify the collection params. |
| 130 |
*/ |
| 131 |
public function get_collection_params() { |
| 132 |
$params = parent::get_collection_params(); |
| 133 |
|
| 134 |
// Check if 'per_page' parameter exists and has a 'minimum' key before modifying. |
| 135 |
if ( isset( $params['per_page'] ) && \is_array( $params['per_page'] ) ) { |
| 136 |
$params['per_page']['minimum'] = -1; |
| 137 |
} |
| 138 |
|
| 139 |
// Ensure 'orderby' is set and is an array before attempting to modify it. |
| 140 |
if ( isset( $params['orderby']['enum'] ) && \is_array( $params['orderby']['enum'] ) ) { |
| 141 |
// Define new sorting options. |
| 142 |
$new_sort_options = array( |
| 143 |
'sku', |
| 144 |
'barcode', |
| 145 |
'stock_quantity', |
| 146 |
'stock_status', |
| 147 |
); |
| 148 |
// Merge new options, avoiding duplicates. |
| 149 |
$params['orderby']['enum'] = array_unique( array_merge( $params['orderby']['enum'], $new_sort_options ) ); |
| 150 |
} |
| 151 |
|
| 152 |
return $params; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Filter the variation response. |
| 157 |
* |
| 158 |
* @param WP_REST_Response $response The response object. |
| 159 |
* @param WC_Data $variation Product data. |
| 160 |
* @param WP_REST_Request $request Request object. |
| 161 |
* |
| 162 |
* @return WP_REST_Response $response The response object. |
| 163 |
*/ |
| 164 |
public function wcpos_variation_response( WP_REST_Response $response, WC_Data $variation, WP_REST_Request $request ): WP_REST_Response { |
| 165 |
$data = $response->get_data(); |
| 166 |
|
| 167 |
// Add the UUID to the product response. |
| 168 |
$this->maybe_add_post_uuid( $variation ); |
| 169 |
|
| 170 |
// Add the barcode to the product response. |
| 171 |
$data['barcode'] = $this->wcpos_get_barcode( $variation ); // @phpstan-ignore-line |
| 172 |
|
| 173 |
// Check if the response has an image. |
| 174 |
if ( isset( $data['image'] ) && ! empty( $data['image'] ) && isset( $data['image']['id'] ) ) { |
| 175 |
// Replace the full size 'src' with the URL of the medium size image. |
| 176 |
$medium_image_data = image_downsize( $data['image']['id'], 'medium' ); |
| 177 |
|
| 178 |
if ( $medium_image_data ) { |
| 179 |
$data['image']['src'] = $medium_image_data[0]; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* Backwards compatibility for WooCommerce < 8.3 |
| 185 |
* |
| 186 |
* WooCommerce added 'parent_id' and 'name' to the variation response in 8.3 |
| 187 |
*/ |
| 188 |
if ( ! isset( $data['parent_id'] ) ) { |
| 189 |
$data['parent_id'] = $variation->get_parent_id(); |
| 190 |
} |
| 191 |
if ( ! isset( $data['name'] ) ) { |
| 192 |
$data['name'] = \function_exists( 'wc_get_formatted_variation' ) ? wc_get_formatted_variation( $variation, true, false, false ) : ''; // @phpstan-ignore-line |
| 193 |
} |
| 194 |
|
| 195 |
// Parse the meta data before returning the response. |
| 196 |
$data['meta_data'] = $this->wcpos_parse_meta_data( $variation ); |
| 197 |
|
| 198 |
// Estimate response size and log if excessive. |
| 199 |
$this->wcpos_estimate_response_size( $data, $variation->get_id(), 'Variation' ); |
| 200 |
|
| 201 |
$response->set_data( $data ); |
| 202 |
|
| 203 |
return $response; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Fires after a single object is created or updated via the REST API. |
| 208 |
* |
| 209 |
* @param WC_Data $object Inserted object. |
| 210 |
* @param WP_REST_Request $request Request object. |
| 211 |
* @param bool $creating True when creating object, false when updating. |
| 212 |
*/ |
| 213 |
public function wcpos_insert_product_variation_object( WC_Data $object, WP_REST_Request $request, $creating ): void { |
| 214 |
$barcode_field = $this->wcpos_get_barcode_field(); |
| 215 |
if ( $request->has_param( 'barcode' ) ) { |
| 216 |
$barcode = $request->get_param( 'barcode' ); |
| 217 |
if ( '_sku' === $barcode_field ) { |
| 218 |
$object->set_sku( $barcode ); |
| 219 |
$object->save(); |
| 220 |
} elseif ( '_global_unique_id' === $barcode_field ) { |
| 221 |
$object->set_global_unique_id( $barcode ); |
| 222 |
$object->save(); |
| 223 |
} else { |
| 224 |
$object->update_meta_data( $barcode_field, $barcode ); |
| 225 |
$object->save_meta_data(); |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Filter to adjust the WordPress search SQL query |
| 232 |
* - Search for the variation SKU and barcode |
| 233 |
* - Do not search variation description. |
| 234 |
* |
| 235 |
* @param string $search Search string. |
| 236 |
* @param WP_Query $wp_query WP_Query object. |
| 237 |
* |
| 238 |
* @return string |
| 239 |
*/ |
| 240 |
public function wcpos_posts_search( string $search, WP_Query $wp_query ) { |
| 241 |
global $wpdb; |
| 242 |
|
| 243 |
if ( empty( $search ) ) { |
| 244 |
return $search; // skip processing - no search term in query. |
| 245 |
} |
| 246 |
|
| 247 |
$q = $wp_query->query_vars; |
| 248 |
$n = ! empty( $q['exact'] ) ? '' : '%'; |
| 249 |
$search_terms = (array) $q['search_terms']; |
| 250 |
|
| 251 |
// Fields in the main 'posts' table. |
| 252 |
$post_fields = array(); // nothing at the moment for variations. |
| 253 |
|
| 254 |
// Meta fields to search. |
| 255 |
$meta_fields = array( '_sku' ); |
| 256 |
$barcode_field = $this->wcpos_get_barcode_field(); |
| 257 |
if ( '_sku' !== $barcode_field ) { |
| 258 |
$meta_fields[] = $barcode_field; |
| 259 |
} |
| 260 |
|
| 261 |
$search_conditions = array(); |
| 262 |
|
| 263 |
foreach ( $search_terms as $term ) { |
| 264 |
$term = $n . $wpdb->esc_like( $term ) . $n; |
| 265 |
|
| 266 |
// Search in meta fields. |
| 267 |
foreach ( $meta_fields as $field ) { |
| 268 |
$search_conditions[] = $wpdb->prepare( '(pm1.meta_value LIKE %s AND pm1.meta_key = %s)', $term, $field ); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
if ( ! empty( $search_conditions ) ) { |
| 273 |
$search = ' AND (' . implode( ' OR ', $search_conditions ) . ') '; |
| 274 |
if ( ! is_user_logged_in() ) { |
| 275 |
$search .= " AND ($wpdb->posts.post_password = '') "; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return $search; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Filters the JOIN clause of the query. |
| 284 |
* |
| 285 |
* @param string $join The JOIN clause of the query. |
| 286 |
* @param WP_Query $query The WP_Query instance (passed by reference). |
| 287 |
* |
| 288 |
* @return string |
| 289 |
*/ |
| 290 |
public function wcpos_posts_join_to_posts_search( string $join, WP_Query $query ) { |
| 291 |
global $wpdb; |
| 292 |
|
| 293 |
if ( ! empty( $query->query_vars['s'] ) && false === strpos( $join, 'pm1' ) ) { |
| 294 |
$join .= " LEFT JOIN {$wpdb->postmeta} pm1 ON {$wpdb->posts}.ID = pm1.post_id "; |
| 295 |
} |
| 296 |
|
| 297 |
return $join; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Filters the GROUP BY clause of the query. |
| 302 |
* |
| 303 |
* @param string $groupby The GROUP BY clause of the query. |
| 304 |
* @param WP_Query $query The WP_Query instance (passed by reference). |
| 305 |
* |
| 306 |
* @return string |
| 307 |
*/ |
| 308 |
public function wcpos_posts_groupby_posts_search( string $groupby, WP_Query $query ) { |
| 309 |
global $wpdb; |
| 310 |
|
| 311 |
if ( ! empty( $query->query_vars['s'] ) ) { |
| 312 |
$groupby = "{$wpdb->posts}.ID"; |
| 313 |
} |
| 314 |
|
| 315 |
return $groupby; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Filter the query arguments for a request. |
| 320 |
* |
| 321 |
* @param array $args Key value array of query var to query value. |
| 322 |
* @param WP_REST_Request $request The request used. |
| 323 |
* |
| 324 |
* @return array $args Key value array of query var to query value. |
| 325 |
*/ |
| 326 |
public function wcpos_product_variation_query( array $args, WP_REST_Request $request ) { |
| 327 |
if ( ! empty( $request['search'] ) ) { |
| 328 |
// We need to set the query up for a postmeta join. |
| 329 |
add_filter( 'posts_join', array( $this, 'wcpos_posts_join_to_posts_search' ), 10, 2 ); |
| 330 |
add_filter( 'posts_groupby', array( $this, 'wcpos_posts_groupby_posts_search' ), 10, 2 ); |
| 331 |
} |
| 332 |
|
| 333 |
// if POS only products are enabled, exclude online-only products. |
| 334 |
if ( $this->wcpos_pos_only_products_enabled() ) { |
| 335 |
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_variation_exclude_online_only' ), 10, 2 ); |
| 336 |
} |
| 337 |
|
| 338 |
// Check for wcpos_include/wcpos_exclude parameter. |
| 339 |
// NOTE: do this after POS visibility filter so that takes precedence. |
| 340 |
if ( isset( $request['wcpos_include'] ) || isset( $request['wcpos_exclude'] ) ) { |
| 341 |
add_filter( 'posts_where', array( $this, 'wcpos_posts_where_product_variation_include_exclude' ), 20, 2 ); |
| 342 |
} |
| 343 |
|
| 344 |
return $args; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Filters the WHERE clause of the query. |
| 349 |
* |
| 350 |
* @param string $where The WHERE clause of the query. |
| 351 |
* @param WP_Query $query The WP_Query instance (passed by reference). |
| 352 |
* |
| 353 |
* @return string |
| 354 |
*/ |
| 355 |
public function wcpos_posts_where_product_variation_exclude_online_only( string $where, WP_Query $query ) { |
| 356 |
global $wpdb; |
| 357 |
|
| 358 |
$settings_instance = Settings::instance(); |
| 359 |
$online_only = $settings_instance->get_online_only_variations_visibility_settings(); |
| 360 |
$online_only_ids = isset( $online_only['ids'] ) && \is_array( $online_only['ids'] ) ? $online_only['ids'] : array(); |
| 361 |
|
| 362 |
// Exclude online-only product IDs if POS only products are enabled. |
| 363 |
if ( ! empty( $online_only_ids ) ) { |
| 364 |
$online_only_ids = array_map( 'intval', (array) $online_only_ids ); |
| 365 |
$ids_format = implode( ',', array_fill( 0, \count( $online_only_ids ), '%d' ) ); |
| 366 |
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID NOT IN ($ids_format) ", $online_only_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name and format are safe. |
| 367 |
} |
| 368 |
|
| 369 |
return $where; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Filters the WHERE clause of the query. |
| 374 |
* |
| 375 |
* @param string $where The WHERE clause of the query. |
| 376 |
* @param WP_Query $query The WP_Query instance (passed by reference). |
| 377 |
* |
| 378 |
* @return string |
| 379 |
*/ |
| 380 |
public function wcpos_posts_where_product_variation_include_exclude( string $where, WP_Query $query ) { |
| 381 |
global $wpdb; |
| 382 |
|
| 383 |
// Handle 'wcpos_include'. |
| 384 |
if ( ! empty( $this->wcpos_request['wcpos_include'] ) ) { |
| 385 |
$include_ids = array_map( 'intval', (array) $this->wcpos_request['wcpos_include'] ); |
| 386 |
$ids_format = implode( ',', array_fill( 0, \count( $include_ids ), '%d' ) ); |
| 387 |
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID IN ($ids_format) ", $include_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name and format are safe. |
| 388 |
} |
| 389 |
|
| 390 |
// Handle 'wcpos_exclude'. |
| 391 |
if ( ! empty( $this->wcpos_request['wcpos_exclude'] ) ) { |
| 392 |
$exclude_ids = array_map( 'intval', (array) $this->wcpos_request['wcpos_exclude'] ); |
| 393 |
$ids_format = implode( ',', array_fill( 0, \count( $exclude_ids ), '%d' ) ); |
| 394 |
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID NOT IN ($ids_format) ", $exclude_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name and format are safe. |
| 395 |
} |
| 396 |
|
| 397 |
return $where; |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* Returns array of all product ids, name. |
| 402 |
* |
| 403 |
* @param WP_REST_Request $request Full details about the request. |
| 404 |
* |
| 405 |
* @return WP_Error|WP_REST_Response |
| 406 |
*/ |
| 407 |
public function wcpos_get_all_posts( $request ) { |
| 408 |
global $wpdb; |
| 409 |
|
| 410 |
// Start timing execution. |
| 411 |
$start_time = microtime( true ); |
| 412 |
|
| 413 |
$modified_after = $request->get_param( 'modified_after' ); |
| 414 |
$fields = $request->get_param( 'fields' ); |
| 415 |
$parent_id = (int) $this->wcpos_request->get_param( 'product_id' ); |
| 416 |
$id_with_modified_date = array( 'id', 'date_modified_gmt' ) === $fields; |
| 417 |
|
| 418 |
// Build the SELECT clause based on requested fields. |
| 419 |
$select_fields = $id_with_modified_date ? 'ID as id, post_modified_gmt as date_modified_gmt' : 'ID as id'; |
| 420 |
|
| 421 |
// Initialize the SQL query. |
| 422 |
$sql = "SELECT DISTINCT {$select_fields} FROM {$wpdb->posts}"; |
| 423 |
$sql .= " WHERE {$wpdb->posts}.post_type = 'product_variation' AND {$wpdb->posts}.post_status = 'publish'"; |
| 424 |
|
| 425 |
// If the '_pos_visibility' condition needs to be applied. |
| 426 |
if ( $this->wcpos_pos_only_products_enabled() ) { |
| 427 |
$settings_instance = Settings::instance(); |
| 428 |
$online_only = $settings_instance->get_online_only_variations_visibility_settings(); |
| 429 |
|
| 430 |
if ( isset( $online_only['ids'] ) && \is_array( $online_only['ids'] ) && ! empty( $online_only['ids'] ) ) { |
| 431 |
$online_only_ids = array_map( 'intval', (array) $online_only['ids'] ); |
| 432 |
$ids_format = implode( ',', array_fill( 0, \count( $online_only_ids ), '%d' ) ); |
| 433 |
$sql .= $wpdb->prepare( " AND ID NOT IN ($ids_format) ", $online_only_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared -- format string is safe. |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
// Add modified_after condition if provided. |
| 438 |
if ( $modified_after ) { |
| 439 |
$modified_after_date = wp_date( 'Y-m-d H:i:s', strtotime( $modified_after ) ); |
| 440 |
$sql .= $wpdb->prepare( ' AND post_modified_gmt > %s', $modified_after_date ); |
| 441 |
} |
| 442 |
|
| 443 |
// Dynamically add the post_parent clause if a parent ID is provided. |
| 444 |
if ( $parent_id ) { |
| 445 |
$sql = $wpdb->prepare( $sql . " AND {$wpdb->posts}.post_parent = %d", $parent_id ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $sql is built with prepare() above. |
| 446 |
} |
| 447 |
|
| 448 |
try { |
| 449 |
// Execute the query. |
| 450 |
$results = $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- SQL is built with prepare() above. |
| 451 |
$formatted_results = $this->wcpos_format_all_posts_response( $results ); |
| 452 |
|
| 453 |
// Get the total number of orders for the given criteria. |
| 454 |
$total = \count( $formatted_results ); |
| 455 |
|
| 456 |
// Collect execution time and server load. |
| 457 |
$execution_time = microtime( true ) - $start_time; |
| 458 |
$execution_time_ms = number_format( $execution_time * 1000, 2 ); |
| 459 |
$server_load = $this->get_server_load(); |
| 460 |
|
| 461 |
$response = rest_ensure_response( $formatted_results ); |
| 462 |
$response->header( 'X-WP-Total', (string) $total ); |
| 463 |
$response->header( 'X-Execution-Time', $execution_time_ms . ' ms' ); |
| 464 |
$response->header( 'X-Server-Load', json_encode( $server_load ) ); |
| 465 |
|
| 466 |
return $response; |
| 467 |
} catch ( Exception $e ) { |
| 468 |
Logger::log( 'Error fetching product variation IDs: ' . $e->getMessage() ); |
| 469 |
|
| 470 |
return new WP_Error( |
| 471 |
'woocommerce_pos_rest_cannot_fetch', |
| 472 |
'Error fetching product variation IDs.', |
| 473 |
array( 'status' => 500 ) |
| 474 |
); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
/** |
| 479 |
* Endpoint for getting all product variations, eg: search for sku or barcode. |
| 480 |
* |
| 481 |
* @param WP_REST_Request $request Full details about the request. |
| 482 |
*/ |
| 483 |
public function wcpos_get_all_items( $request ) { |
| 484 |
return parent::get_items( $request ); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
/** |
| 489 |
* Prepare objects query. |
| 490 |
* |
| 491 |
* @param WP_REST_Request $request Full details about the request. |
| 492 |
* |
| 493 |
* @return array|WP_Error |
| 494 |
*/ |
| 495 |
protected function prepare_objects_query( $request ) { |
| 496 |
$args = parent::prepare_objects_query( $request ); |
| 497 |
$barcode_field = $this->wcpos_get_barcode_field(); |
| 498 |
|
| 499 |
// Add custom 'orderby' options. |
| 500 |
if ( isset( $request['orderby'] ) ) { |
| 501 |
switch ( $request['orderby'] ) { |
| 502 |
case 'sku': |
| 503 |
$args['meta_key'] = '_sku'; |
| 504 |
$args['orderby'] = 'meta_value'; |
| 505 |
|
| 506 |
break; |
| 507 |
case 'barcode': |
| 508 |
$args['meta_key'] = '_sku' !== $barcode_field ? $barcode_field : '_sku'; |
| 509 |
$args['orderby'] = 'meta_value'; |
| 510 |
|
| 511 |
break; |
| 512 |
case 'stock_quantity': |
| 513 |
$args['meta_key'] = '_stock'; |
| 514 |
$args['orderby'] = 'meta_value_num'; |
| 515 |
|
| 516 |
break; |
| 517 |
case 'stock_status': |
| 518 |
$args['meta_key'] = '_stock_status'; |
| 519 |
$args['orderby'] = 'meta_value'; |
| 520 |
|
| 521 |
break; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
return $args; |
| 526 |
} |
| 527 |
} |
| 528 |
|