Added support for filtering products and variations by their menu_order

This commit is contained in:
Christopher Allford 2019-12-18 13:38:10 -08:00 committed by Claudio Sanches
parent 5a509591e7
commit 0704f6d023
3 changed files with 38 additions and 0 deletions

View File

@ -2104,6 +2104,8 @@ class WC_REST_Products_V2_Controller extends WC_REST_CRUD_Controller {
public function get_collection_params() {
$params = parent::get_collection_params();
$params['orderby']['enum'] = array_merge( $params['orderby']['enum'], array( 'menu_order' ) );
$params['slug'] = array(
'description' => __( 'Limit result set to products with a specific slug.', 'woocommerce-rest-api' ),
'type' => 'string',

View File

@ -49,6 +49,24 @@ class Product_Variations_API_V2 extends WC_REST_Unit_Test_Case {
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
}
/**
* Test getting variations with an orderby clause.
*
* @since 3.9.0
*/
public function test_get_variations_with_orderby() {
wp_set_current_user( $this->user );
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product();
$request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' );
$request->set_query_params( array( 'orderby' => 'menu_order' ) );
$response = $this->server->dispatch( $request );
$variations = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, count( $variations ) );
$this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variations[0]['sku'] );
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
}
/**
* Test getting variations without permission.
*

View File

@ -49,6 +49,24 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
}
/**
* Test getting variations with an orderby clause.
*
* @since 3.9.0
*/
public function test_get_variations_with_orderby() {
wp_set_current_user( $this->user );
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product();
$request = new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' );
$request->set_query_params( array( 'orderby' => 'menu_order' ) );
$response = $this->server->dispatch( $request );
$variations = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 2, count( $variations ) );
$this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variations[0]['sku'] );
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
}
/**
* Test getting variations without permission.
*