WC_Shortcode_Products::set_categories_query_args()protectedWC 3.2.0

Set categories query args.

Метод класса: WC_Shortcode_Products{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->set_categories_query_args( $query_args );
$query_args(массив) (обязательный) (передается по ссылке — &)
Query args.

Список изменений

С версии 3.2.0 Введена.

Код WC_Shortcode_Products::set_categories_query_args() 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,
		);
	}
}