Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 0704f6d

Browse files
ObliviousHarmonyclaudiosanches
authored andcommitted
Added support for filtering products and variations by their menu_order
1 parent 5a50959 commit 0704f6d

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/Controllers/Version2/class-wc-rest-products-v2-controller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,8 @@ public function get_item_schema() {
21042104
public function get_collection_params() {
21052105
$params = parent::get_collection_params();
21062106

2107+
$params['orderby']['enum'] = array_merge( $params['orderby']['enum'], array( 'menu_order' ) );
2108+
21072109
$params['slug'] = array(
21082110
'description' => __( 'Limit result set to products with a specific slug.', 'woocommerce-rest-api' ),
21092111
'type' => 'string',

unit-tests/Tests/Version2/product-variations.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ public function test_get_variations() {
4949
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
5050
}
5151

52+
/**
53+
* Test getting variations with an orderby clause.
54+
*
55+
* @since 3.9.0
56+
*/
57+
public function test_get_variations_with_orderby() {
58+
wp_set_current_user( $this->user );
59+
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product();
60+
$request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' );
61+
$request->set_query_params( array( 'orderby' => 'menu_order' ) );
62+
$response = $this->server->dispatch( $request );
63+
$variations = $response->get_data();
64+
$this->assertEquals( 200, $response->get_status() );
65+
$this->assertEquals( 2, count( $variations ) );
66+
$this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variations[0]['sku'] );
67+
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
68+
}
69+
5270
/**
5371
* Test getting variations without permission.
5472
*

unit-tests/Tests/Version3/product-variations.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ public function test_get_variations() {
4949
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
5050
}
5151

52+
/**
53+
* Test getting variations with an orderby clause.
54+
*
55+
* @since 3.9.0
56+
*/
57+
public function test_get_variations_with_orderby() {
58+
wp_set_current_user( $this->user );
59+
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product();
60+
$request = new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' );
61+
$request->set_query_params( array( 'orderby' => 'menu_order' ) );
62+
$response = $this->server->dispatch( $request );
63+
$variations = $response->get_data();
64+
$this->assertEquals( 200, $response->get_status() );
65+
$this->assertEquals( 2, count( $variations ) );
66+
$this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variations[0]['sku'] );
67+
$this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] );
68+
}
69+
5270
/**
5371
* Test getting variations without permission.
5472
*

0 commit comments

Comments
 (0)