Excluded children categories from WP_Query in product shortcode if cat_operator=AND.
This commit is contained in:
parent
216dce4fab
commit
082c1dfaf3
|
@ -327,6 +327,12 @@ class WC_Shortcode_Products {
|
|||
'terms' => $categories,
|
||||
'field' => $field,
|
||||
'operator' => $this->attributes['cat_operator'],
|
||||
|
||||
/*
|
||||
* When cat_operator is AND, the children categories should be excluded,
|
||||
* as only products belonging to all the children categories would be selected.
|
||||
*/
|
||||
'include_children' => 'AND' === $this->attributes['cat_operator'] ? false : true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -154,6 +154,7 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
|
|||
'terms' => array( 'clothing' ),
|
||||
'field' => 'slug',
|
||||
'operator' => 'IN',
|
||||
'include_children' => true,
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected4, $shortcode4->get_query_args() );
|
||||
|
@ -184,6 +185,7 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
|
|||
'terms' => array( 123 ),
|
||||
'field' => 'term_id',
|
||||
'operator' => 'IN',
|
||||
'include_children' => true,
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected4_id, $shortcode4_id->get_query_args() );
|
||||
|
@ -492,6 +494,63 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
|
|||
);
|
||||
|
||||
$this->assertEquals( $expected14, $shortcode14->get_query_args() );
|
||||
|
||||
// products shortcode -- select multiple categories using AND operator.
|
||||
$shortcode15 = new WC_Shortcode_Products( array(
|
||||
'category' => 'cat1,cat2',
|
||||
'cat_operator' => 'AND',
|
||||
) );
|
||||
$expected15 = array(
|
||||
'post_type' => 'product',
|
||||
'post_status' => 'publish',
|
||||
'ignore_sticky_posts' => true,
|
||||
'no_found_rows' => true,
|
||||
'orderby' => 'title',
|
||||
'order' => 'ASC',
|
||||
'posts_per_page' => -1,
|
||||
'meta_query' => $meta_query,
|
||||
'tax_query' => array_merge( $tax_query, array(
|
||||
array(
|
||||
'taxonomy' => 'product_cat',
|
||||
'terms' => array( 'cat1', 'cat2' ),
|
||||
'field' => 'slug',
|
||||
'operator' => 'AND',
|
||||
'include_children' => false,
|
||||
),
|
||||
) ),
|
||||
'fields' => 'ids',
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected15, $shortcode15->get_query_args() );
|
||||
|
||||
// products shortcode -- exclude multiple categories using NOT IN operator.
|
||||
$shortcode16 = new WC_Shortcode_Products( array(
|
||||
'category' => 'cat1,cat2',
|
||||
'cat_operator' => 'NOT IN',
|
||||
) );
|
||||
$expected16 = array(
|
||||
'post_type' => 'product',
|
||||
'post_status' => 'publish',
|
||||
'ignore_sticky_posts' => true,
|
||||
'no_found_rows' => true,
|
||||
'orderby' => 'title',
|
||||
'order' => 'ASC',
|
||||
'posts_per_page' => -1,
|
||||
'meta_query' => $meta_query,
|
||||
'tax_query' => array_merge( $tax_query, array(
|
||||
array(
|
||||
'taxonomy' => 'product_cat',
|
||||
'terms' => array( 'cat1', 'cat2' ),
|
||||
'field' => 'slug',
|
||||
'operator' => 'NOT IN',
|
||||
'include_children' => true,
|
||||
),
|
||||
) ),
|
||||
'fields' => 'ids',
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected16, $shortcode16->get_query_args() );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue