WC_Shortcode_Products::set_attributes_query_args()protectedWC 3.2.0

Set attributes query args.

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

Хуков нет.

Возвращает

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

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

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

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

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

Код WC_Shortcode_Products::set_attributes_query_args() WC 8.7.0

protected function set_attributes_query_args( &$query_args ) {
	if ( ! empty( $this->attributes['attribute'] ) || ! empty( $this->attributes['terms'] ) ) {
		$taxonomy = strstr( $this->attributes['attribute'], 'pa_' ) ? sanitize_title( $this->attributes['attribute'] ) : 'pa_' . sanitize_title( $this->attributes['attribute'] );
		$terms    = $this->attributes['terms'] ? array_map( 'sanitize_title', explode( ',', $this->attributes['terms'] ) ) : array();
		$field    = 'slug';

		if ( $terms && is_numeric( $terms[0] ) ) {
			$field = 'term_id';
			$terms = array_map( 'absint', $terms );
			// Check numeric slugs.
			foreach ( $terms as $term ) {
				$the_term = get_term_by( 'slug', $term, $taxonomy );
				if ( false !== $the_term ) {
					$terms[] = $the_term->term_id;
				}
			}
		}

		// If no terms were specified get all products that are in the attribute taxonomy.
		if ( ! $terms ) {
			$terms = get_terms(
				array(
					'taxonomy' => $taxonomy,
					'fields'   => 'ids',
				)
			);
			$field = 'term_id';
		}

		// We always need to search based on the slug as well, this is to accommodate numeric slugs.
		$query_args['tax_query'][] = array(
			'taxonomy' => $taxonomy,
			'terms'    => $terms,
			'field'    => $field,
			'operator' => $this->attributes['terms_operator'],
		);
	}
}