WC_Shortcode_Products::set_categories_query_args()protectedWC 3.2.0

Set categories query args.

Method of the class: WC_Shortcode_Products{}

No Hooks.

Return

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() code WC 8.7.0

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,
		);
	}
}