Automattic\WooCommerce\StoreApi\Schemas\V1

OrderItemSchema::get_variation_data_from_order_itemprotectedWC 1.0

Get variation data from order item metadata.

Gets the customer's actual attribute choices from the order metadata. This fixes variations set to "Any" returning empty values.

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

Хуков нет.

Возвращает

Массив. Formatted variation data.

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

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

Код OrderItemSchema::get_variation_data_from_order_item() WC 11.0.1

protected function get_variation_data_from_order_item( $order_item, $product ) {
	$variation_data = array();
	$meta_data      = $order_item->get_meta_data();

	$parent_attributes = $this->get_parent_product_attributes( $product );

	foreach ( $meta_data as $meta ) {
		$meta_key   = $meta->key;
		$meta_value = $meta->value;

		if ( empty( $meta_key ) || ! is_scalar( $meta_value ) || '' === $meta_value || strpos( $meta_key, '_' ) === 0 ) {
			continue;
		}

		$is_variation_attribute = false;
		$normalized_key         = $meta_key;

		if ( strpos( $meta_key, 'attribute_' ) === 0 ) {
			$normalized_key = substr( $meta_key, strlen( 'attribute_' ) );
		}

		if ( strpos( $normalized_key, 'pa_' ) === 0 ) {
			$is_variation_attribute = true;
		} else {
			foreach ( $parent_attributes as $attribute ) {
				if ( strtolower( $attribute->get_name() ) === strtolower( $normalized_key ) ) {
					$is_variation_attribute = true;
					break;
				}
			}
		}

		if ( $is_variation_attribute ) {
			$variation_data[ wc_variation_attribute_name( $normalized_key ) ] = $meta_value;
		}
	}

	return $this->format_variation_data( $variation_data, $product );
}