WC_Product_Variable_Data_Store_CPT::read_variation_attributes()publicWC 1.0

Loads an array of attributes used for variations, as well as their possible values.

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

Хуков нет.

Возвращает

Массив.

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

$WC_Product_Variable_Data_Store_CPT = new WC_Product_Variable_Data_Store_CPT();
$WC_Product_Variable_Data_Store_CPT->read_variation_attributes( $product );
$product(WC_Product) (обязательный) (передается по ссылке — &)
Product object.

Код WC_Product_Variable_Data_Store_CPT::read_variation_attributes() WC 8.7.0

public function read_variation_attributes( &$product ) {
	global $wpdb;

	$variation_attributes = array();
	$attributes           = $product->get_attributes();
	$child_ids            = $product->get_children();
	$cache_key            = WC_Cache_Helper::get_cache_prefix( 'product_' . $product->get_id() ) . 'product_variation_attributes_' . $product->get_id();
	$cache_group          = 'products';
	$cached_data          = wp_cache_get( $cache_key, $cache_group );

	if ( false !== $cached_data ) {
		return $cached_data;
	}

	if ( ! empty( $attributes ) ) {
		foreach ( $attributes as $attribute ) {
			if ( empty( $attribute['is_variation'] ) ) {
				continue;
			}

			// Get possible values for this attribute, for only visible variations.
			if ( ! empty( $child_ids ) ) {
				$format     = array_fill( 0, count( $child_ids ), '%d' );
				$query_in   = '(' . implode( ',', $format ) . ')';
				$query_args = array( 'attribute_name' => wc_variation_attribute_name( $attribute['name'] ) ) + $child_ids;
				$values     = array_unique(
					$wpdb->get_col(
						$wpdb->prepare(
							"SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id IN {$query_in}", // @codingStandardsIgnoreLine.
							$query_args
						)
					)
				);
			} else {
				$values = array();
			}

			// Empty value indicates that all options for given attribute are available.
			if ( in_array( null, $values, true ) || in_array( '', $values, true ) || empty( $values ) ) {
				$values = $attribute['is_taxonomy'] ? wc_get_object_terms( $product->get_id(), $attribute['name'], 'slug' ) : wc_get_text_attributes( $attribute['value'] );
				// Get custom attributes (non taxonomy) as defined.
			} elseif ( ! $attribute['is_taxonomy'] ) {
				$text_attributes          = wc_get_text_attributes( $attribute['value'] );
				$assigned_text_attributes = $values;
				$values                   = array();

				// Pre 2.4 handling where 'slugs' were saved instead of the full text attribute.
				if ( version_compare( get_post_meta( $product->get_id(), '_product_version', true ), '2.4.0', '<' ) ) {
					$assigned_text_attributes = array_map( 'sanitize_title', $assigned_text_attributes );
					foreach ( $text_attributes as $text_attribute ) {
						if ( in_array( sanitize_title( $text_attribute ), $assigned_text_attributes, true ) ) {
							$values[] = $text_attribute;
						}
					}
				} else {
					foreach ( $text_attributes as $text_attribute ) {
						if ( in_array( $text_attribute, $assigned_text_attributes, true ) ) {
							$values[] = $text_attribute;
						}
					}
				}
			}
			$variation_attributes[ $attribute['name'] ] = array_unique( $values );
		}
	}

	wp_cache_set( $cache_key, $variation_attributes, $cache_group );

	return $variation_attributes;
}