WC_REST_Products_V2_Controller::get_attributes()protectedWC 1.0

Get the attributes for a product or product variation.

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

Хуков нет.

Возвращает

Массив.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_attributes( $product );
$product(WC_Product|WC_Product_Variation) (обязательный)
Product instance.

Код WC_REST_Products_V2_Controller::get_attributes() WC 8.7.0

protected function get_attributes( $product ) {
	$attributes = array();

	if ( $product->is_type( 'variation' ) ) {
		$_product = wc_get_product( $product->get_parent_id() );
		foreach ( $product->get_variation_attributes() as $attribute_name => $attribute ) {
			$name = str_replace( 'attribute_', '', $attribute_name );

			if ( empty( $attribute ) && '0' !== $attribute ) {
				continue;
			}

			// Taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`.
			if ( 0 === strpos( $attribute_name, 'attribute_pa_' ) ) {
				$option_term  = get_term_by( 'slug', $attribute, $name );
				$attributes[] = array(
					'id'     => wc_attribute_taxonomy_id_by_name( $name ),
					'name'   => $this->get_attribute_taxonomy_name( $name, $_product ),
					'slug'   => $name,
					'option' => $option_term && ! is_wp_error( $option_term ) ? $option_term->name : $attribute,
				);
			} else {
				$attributes[] = array(
					'id'     => 0,
					'name'   => $this->get_attribute_taxonomy_name( $name, $_product ),
					'slug'   => $name,
					'option' => $attribute,
				);
			}
		}
	} else {
		foreach ( $product->get_attributes() as $attribute ) {
			$attributes[] = array(
				'id'        => $attribute['is_taxonomy'] ? wc_attribute_taxonomy_id_by_name( $attribute['name'] ) : 0,
				'name'      => $this->get_attribute_taxonomy_name( $attribute['name'], $product ),
				'slug'      => $attribute['name'],
				'position'  => (int) $attribute['position'],
				'visible'   => (bool) $attribute['is_visible'],
				'variation' => (bool) $attribute['is_variation'],
				'options'   => $this->get_attribute_options( $product->get_id(), $attribute ),
			);
		}
	}

	return $attributes;
}