WC_Shortcode_Products::set_attributes_query_args()protectedWC 3.2.0

Set attributes 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_attributes_query_args( $query_args );
$query_args(array) (required) (passed by reference — &)
Query args.

Changelog

Since 3.2.0 Introduced.

WC_Shortcode_Products::set_attributes_query_args() code WC 9.4.2

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