Add suggested-products product endpoint (#43720)
* init approach of related products endpoint * register related prodducts endpoint * rename response prop with ids * filter categoris from endpoint params * filter categories when pulling related products * changelog * extend from WC_REST_CRUD_Controller class * iterate over defininf endpoint params * doc * merge product terms with params * introduce `combine` param * return products collection instead of plain array * fixinf eslint issues * pass proper fn args to bound fns * do not call parent register method * post type is defined by parent controller class * remove related products controller * extend products controller with `related-products`endpoint * filter post__in param * when no related products found return empty array * change and register endpoint params * remove commented line * add a custom method to extend the params * update * fix typo * ensure to respect the products limit * fix eslint issue * remove unrequired endpoint id param
This commit is contained in:
parent
4c29a65378
commit
0940201c37
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: add
|
||||
|
||||
Add linked-products product endpoint
|
|
@ -36,6 +36,34 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
|||
*/
|
||||
private $search_sku_in_product_lookup_table = '';
|
||||
|
||||
/**
|
||||
* Suggested product ids.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $suggested_products_ids = array();
|
||||
|
||||
/**
|
||||
* Register the routes for products.
|
||||
*/
|
||||
public function register_routes() {
|
||||
parent::register_routes();
|
||||
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/suggested-products',
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_suggested_products' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_suggested_products_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the images for a product or product variation.
|
||||
*
|
||||
|
@ -235,6 +263,15 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
|||
$args['meta_key'] = $ordering_args['meta_key']; // WPCS: slow query ok.
|
||||
}
|
||||
|
||||
/*
|
||||
* When the suggested products ids is not empty,
|
||||
* filter the query to return only the suggested products,
|
||||
* overwriting the post__in parameter.
|
||||
*/
|
||||
if ( ! empty( $this->suggested_products_ids ) ) {
|
||||
$args['post__in'] = $this->suggested_products_ids;
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
|
@ -1483,6 +1520,47 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
|||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new options for the suggested-products endpoint.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_suggested_products_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['categories'] = array(
|
||||
'description' => __( 'Limit result set to specific product categorie ids.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
$params['tags'] = array(
|
||||
'description' => __( 'Limit result set to specific product tag ids.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
|
||||
$params['limit'] = array(
|
||||
'description' => __( 'Limit result set to specific amount of suggested products.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'default' => 5,
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
'sanitize_callback' => 'absint',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get product data.
|
||||
*
|
||||
|
@ -1538,4 +1616,36 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
|||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the suggested products.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return object
|
||||
*/
|
||||
public function get_suggested_products( $request ) {
|
||||
$categories = $request->get_param( 'categories' );
|
||||
$tags = $request->get_param( 'tags' );
|
||||
$exclude_ids = $request->get_param( 'exclude' );
|
||||
$limit = $request->get_param( 'limit' ) ? $request->get_param( 'limit' ) : 5;
|
||||
|
||||
$data_store = WC_Data_Store::load( 'product' );
|
||||
$this->suggested_products_ids = $data_store->get_related_products(
|
||||
$categories,
|
||||
$tags,
|
||||
$exclude_ids,
|
||||
$limit,
|
||||
null // No need to pass the product ID.
|
||||
);
|
||||
|
||||
// When no suggested products are found, return an empty array.
|
||||
if ( empty( $this->suggested_products_ids ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
// Ensure to respect the limit, since the data store may return more than the limit.
|
||||
$this->suggested_products_ids = array_slice( $this->suggested_products_ids, 0, $limit );
|
||||
|
||||
return parent::get_items( $request );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue