WC_API_Products::get_attributes()privateWC 2.1

Get the attributes for a product or product variation

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

Хуков нет.

Возвращает

Массив.

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

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

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

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

Код WC_API_Products::get_attributes() WC 8.7.0

private function get_attributes( $product ) {

	$attributes = array();

	if ( $product->is_type( 'variation' ) ) {

		// variation attributes
		foreach ( $product->get_variation_attributes() as $attribute_name => $attribute ) {

			// taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`
			$attributes[] = array(
				'name'   => wc_attribute_label( str_replace( 'attribute_', '', $attribute_name ), $product ),
				'slug'   => str_replace( 'attribute_', '', wc_attribute_taxonomy_slug( $attribute_name ) ),
				'option' => $attribute,
			);
		}
	} else {

		foreach ( $product->get_attributes() as $attribute ) {
			$attributes[] = array(
				'name'      => wc_attribute_label( $attribute['name'], $product ),
				'slug'      => wc_attribute_taxonomy_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;
}