WC_REST_Products_V4_Controller::get_suggested_products
Get the suggested products.
Method of the class: WC_REST_Products_V4_Controller{}
No Hooks.
Returns
Object.
Usage
$WC_REST_Products_V4_Controller = new WC_REST_Products_V4_Controller(); $WC_REST_Products_V4_Controller->get_suggested_products( $request );
- $request(WP_REST_Request) (required)
- Request object.
WC_REST_Products_V4_Controller::get_suggested_products() WC REST Products V4 Controller::get suggested products code WC 10.3.6
woocommerce/includes/rest-api/Controllers/Version4/Products/class-wc-rest-products-v4-controller.php
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 );
}