Add global attribute terms filtering (#40253)

* Deprecate local_attributes and add attributes filter to variation endpoint

* Add test for variations attributes filter

* Add changelog
This commit is contained in:
louwie17 2023-09-25 16:10:38 -03:00 committed by GitHub
parent 1ca1536b31
commit 48834f88be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 1 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add attributes filter to variations endpoint and deprecate local_attributes filter.

View File

@ -821,8 +821,12 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
// Set post_status.
$args['post_status'] = $request['status'];
// Filter by local attributes.
/**
* @deprecated 8.1.0 replaced by attributes.
* Filter by local attributes.
*/
if ( ! empty( $request['local_attributes'] ) && is_array( $request['local_attributes'] ) ) {
wc_deprecated_argument( 'local_attributes', '8.1', 'Use "attributes" instead.' );
foreach ( $request['local_attributes'] as $attribute ) {
if ( ! isset( $attribute['attribute'] ) || ! isset( $attribute['term'] ) ) {
continue;
@ -837,6 +841,22 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
}
}
// Filter by attributes.
if ( ! empty( $request['attributes'] ) && is_array( $request['attributes'] ) ) {
foreach ( $request['attributes'] as $attribute ) {
if ( ! isset( $attribute['attribute'] ) || ! isset( $attribute['term'] ) ) {
continue;
}
$args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
$args,
array(
'key' => 'attribute_' . $attribute['attribute'],
'value' => $attribute['term'],
)
);
}
}
// Filter by sku.
if ( ! empty( $request['sku'] ) ) {
$skus = explode( ',', $request['sku'] );
@ -950,6 +970,24 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
'validate_callback' => 'rest_validate_request_arg',
);
$params['attributes'] = array(
'description' => __( 'Limit result set to products with specified attributes.', 'woocommerce' ),
'type' => 'array',
'items' => array(
'type' => 'object',
'properties' => array(
'attribute' => array(
'type' => 'string',
'description' => __( 'Attribute slug.', 'woocommerce' ),
),
'term' => array(
'type' => 'string',
'description' => __( 'Attribute term.', 'woocommerce' ),
),
),
),
);
return $params;
}

View File

@ -718,4 +718,67 @@ class Product_Variations_API extends WC_REST_Unit_Test_Case {
// Removed four.
$this->assertEquals( 2, count( $product->get_children() ) );
}
/**
* Test getting variations with an attributes filter.
*
* @since 8.1.0
*/
public function test_get_variations_with_attributes_filter() {
wp_set_current_user( $this->user );
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product();
$color_attribute_data = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_attribute( 'color', array( 'red', 'blue', 'yellow' ) );
$color_attribute = new WC_Product_Attribute();
$color_attribute->set_id( $color_attribute_data['attribute_id'] );
$color_attribute->set_name( $color_attribute_data['attribute_taxonomy'] );
$color_attribute->set_options( $color_attribute_data['term_ids'] );
$color_attribute->set_position( 1 );
$color_attribute->set_visible( true );
$color_attribute->set_variation( true );
$local_attribute = new WC_Product_Attribute();
$local_attribute->set_id( 0 );
$local_attribute->set_name( 'Local' );
$local_attribute->set_options( array( 'Local1', 'Local2', 'Local3' ) );
$local_attribute->set_visible( true );
$local_attribute->set_variation( true );
$attributes = array();
$attributes[] = $color_attribute;
$attributes[] = $local_attribute;
$product->set_attributes( $attributes );
$product->save();
$request = new WP_REST_Request( 'POST', '/wc/v3/products/' . $product->get_id() . '/variations/generate' );
$request->set_body_params( array( 'delete' => true ) );
$response = $this->server->dispatch( $request );
// Filter by single global attribute.
$request = new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' );
$request->set_query_params( array( 'attributes' => array(
array( 'attribute' => 'pa_color', 'term' => 'red' )
) ) );
$response = $this->server->dispatch( $request );
$variations = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 3, count( $variations ) );
// Filter by global and local attribute.
$request = new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' );
$request->set_query_params( array( 'attributes' => array(
array( 'attribute' => 'pa_color', 'term' => 'red' ),
array( 'attribute' => 'local', 'term' => 'Local1' ),
) ) );
$response = $this->server->dispatch( $request );
$variations = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 1, count( $variations ) );
// Filter local attribute.
$request = new WP_REST_Request( 'GET', '/wc/v3/products/' . $product->get_id() . '/variations' );
$request->set_query_params( array( 'attributes' => array(
array( 'attribute' => 'local', 'term' => 'Local1' ),
) ) );
$response = $this->server->dispatch( $request );
$variations = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 3, count( $variations ) );
}
}