Support using category_id and tag_id in wc_get_product() (#40436)

* Support using category_id and tag_id in wc_get_product()

* add changelog file

* add unit tests for searching products by category slug, category id, tag slug, and tag id

* Add changefile(s) from automation for the following project(s): woocommerce

* remove the `unset` for `category_id`

* appease the linter

* rename tag_id to product_tag_id so that we don't interfere with WordPress's native use of tag_id

* rename category_id to product_category_id for consistency

---------

Co-authored-by: helgatheviking <507025+helgatheviking@users.noreply.github.com>
Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Leif Singer 2023-09-29 20:06:57 +02:00 committed by GitHub
parent 0c7d01bc8e
commit e06f674288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Support using category_id and tag_id in wc_get_product()

View File

@ -1866,6 +1866,12 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
'field' => 'slug',
'terms' => $query_vars['category'],
);
} elseif ( ! empty( $query_vars['product_category_id'] ) ) {
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $query_vars['product_category_id'],
);
}
// Handle product tags.
@ -1876,6 +1882,12 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
'field' => 'slug',
'terms' => $query_vars['tag'],
);
} elseif ( ! empty( $query_vars['product_tag_id'] ) ) {
$wp_query_args['tax_query'][] = array(
'taxonomy' => 'product_tag',
'field' => 'term_id',
'terms' => $query_vars['product_tag_id'],
);
}
// Handle shipping classes.

View File

@ -227,6 +227,88 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
$variation->delete( true );
}
/**
* @testdox We can search for products by category slugs and category IDs.
*/
public function test_searching_products_by_category() {
$cat1 = wp_insert_term( 'Cat One', 'product_cat' );
$cat1_term = get_term_by( 'id', $cat1['term_id'], 'product_cat' );
$cat2 = wp_insert_term( 'Cat Two', 'product_cat' );
$product1 = WC_Helper_Product::create_simple_product();
$product1->set_name( 'Product 1' );
$product1->set_category_ids( array( $cat1['term_id'] ) );
$product1->save();
$product2 = WC_Helper_Product::create_simple_product();
$product2->set_name( 'Product 2' );
$product2->set_category_ids( array( $cat2['term_id'] ) );
$product2->save();
$product3 = WC_Helper_Product::create_simple_product();
$product3->set_name( 'Product 3' );
$product3->save();
// Search by category slug.
$products = wc_get_products(
array(
'category' => $cat1_term->slug,
)
);
$this->assertCount( 1, $products );
$this->assertEquals( $product1->get_id(), $products[0]->get_id() );
// Search by category ID.
$products = wc_get_products(
array(
'product_category_id' => $cat2['term_id'],
)
);
$this->assertCount( 1, $products );
$this->assertEquals( $product2->get_id(), $products[0]->get_id() );
}
/**
* @testdox We can search for products by tag slugs and tag IDs.
*/
public function test_searching_products_by_tag() {
$tag1 = wp_insert_term( 'Tag One', 'product_tag' );
$tag1_term = get_term_by( 'id', $tag1['term_id'], 'product_tag' );
$tag2 = wp_insert_term( 'Tag Two', 'product_tag' );
$product1 = WC_Helper_Product::create_simple_product();
$product1->set_name( 'Product 1' );
$product1->set_tag_ids( array( $tag1['term_id'] ) );
$product1->save();
$product2 = WC_Helper_Product::create_simple_product();
$product2->set_name( 'Product 2' );
$product2->set_tag_ids( array( $tag2['term_id'] ) );
$product2->save();
$product3 = WC_Helper_Product::create_simple_product();
$product3->set_name( 'Product 3' );
$product3->save();
// Search by category slug.
$products = wc_get_products(
array(
'tag' => $tag1_term->slug,
)
);
$this->assertCount( 1, $products );
$this->assertEquals( $product1->get_id(), $products[0]->get_id() );
// Search by category ID.
$products = wc_get_products(
array(
'product_tag_id' => $tag2['term_id'],
)
);
$this->assertCount( 1, $products );
$this->assertEquals( $product2->get_id(), $products[0]->get_id() );
}
/**
* Tests wc_get_products() with dimension parameters.
*