WC_Shortcode_Products::set_categories_query_args
Set categories query args.
Method of the class: WC_Shortcode_Products{}
No Hooks.
Returns
null. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->set_categories_query_args( $query_args );
- $query_args(array) (required) (passed by reference — &)
- Query args.
Changelog
| Since 3.2.0 | Introduced. |
WC_Shortcode_Products::set_categories_query_args() WC Shortcode Products::set categories query args code WC 10.6.2
protected function set_categories_query_args( &$query_args ) {
if ( ! empty( $this->attributes['category'] ) ) {
$categories = array_map( 'sanitize_title', explode( ',', $this->attributes['category'] ) );
$field = 'slug';
if ( is_numeric( $categories[0] ) ) {
$field = 'term_id';
$categories = array_map( 'absint', $categories );
// Check numeric slugs.
foreach ( $categories as $cat ) {
$the_cat = get_term_by( 'slug', $cat, 'product_cat' );
if ( false !== $the_cat ) {
$categories[] = $the_cat->term_id;
}
}
}
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'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,
);
}
}