Fix orderby slug and include parameters.

This commit is contained in:
Jeff Stieler 2021-10-04 10:54:18 -04:00 committed by vedanshujain
parent c99af9f33e
commit 43b770ce1f
2 changed files with 13 additions and 12 deletions

View File

@ -207,10 +207,7 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
$args['post_type'] = $this->post_type;
}
$orderby = $request->get_param( 'orderby' );
$order = $request->get_param( 'order' );
$ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order );
$ordering_args = WC()->query->get_catalog_ordering_args( $args['orderby'], $args['order'] );
$args['orderby'] = $ordering_args['orderby'];
$args['order'] = $ordering_args['order'];
if ( $ordering_args['meta_key'] ) {

View File

@ -568,31 +568,35 @@ const { productsApi } = require('../../endpoints/products');
} );
} );
// This case will remain skipped until orderby slug is fixed.
// See: https://github.com/woocommerce/woocommerce/issues/30354#issuecomment-925955099.
it.skip( 'slug', async () => {
it( 'slug', async () => {
const productNamesBySlugAsc = [
'Polo', // The Polo isn't published so it has an empty slug.
...productNamesAsc.filter( p => p !== 'Polo' ),
];
const productNamesBySlugDesc = [ ...productNamesBySlugAsc ].reverse();
const result1 = await productsApi.listAll.products( {
order: 'asc',
orderby: 'slug',
per_page: productNamesAsc.length,
per_page: productNamesBySlugAsc.length,
} );
expect( result1.statusCode ).toEqual( 200 );
// Verify all results are in ascending order.
result1.body.forEach( ( { name }, idx ) => {
expect( name ).toBe( productNamesAsc[ idx ] );
expect( name ).toBe( productNamesBySlugAsc[ idx ] );
} );
const result2 = await productsApi.listAll.products( {
order: 'desc',
orderby: 'slug',
per_page: productNamesDesc.length,
per_page: productNamesBySlugDesc.length,
} );
expect( result2.statusCode ).toEqual( 200 );
// Verify all results are in descending order.
result2.body.forEach( ( { name }, idx ) => {
expect( name ).toBe( productNamesDesc[ idx ] );
expect( name ).toBe( productNamesBySlugDesc[ idx ] );
} );
} );
@ -675,7 +679,7 @@ const { productsApi } = require('../../endpoints/products');
// This case will remain skipped until orderby include is fixed.
// See: https://github.com/woocommerce/woocommerce/issues/30354#issuecomment-925955099.
it.skip( 'include', async () => {
it( 'include', async () => {
const includeIds = [
sampleData.groupedProducts[ 0 ].id,
sampleData.simpleProducts[ 3 ].id,