WC_REST_Products_Controller::get_suggested_products()publicWC 1.0

Get the suggested products.

Method of the class: WC_REST_Products_Controller{}

No Hooks.

Return

Object.

Usage

$WC_REST_Products_Controller = new WC_REST_Products_Controller();
$WC_REST_Products_Controller->get_suggested_products( $request );
$request(WP_REST_Request) (required)
Request object.

WC_REST_Products_Controller::get_suggested_products() code WC 8.7.0

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 );
}