Automattic\WooCommerce\StoreApi\Schemas\V1
OrderItemSchema::get_variation_data_from_order_item
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.
Method of the class: OrderItemSchema{}
No Hooks.
Returns
array. Formatted variation data.
Usage
// protected - for code of main (parent) or child class $result = $this->get_variation_data_from_order_item( $order_item, $product );
- $order_item(WC_Order_Item_Product) (required)
- Order item instance.
- $product(WC_Product) (required)
- Product instance.
OrderItemSchema::get_variation_data_from_order_item() OrderItemSchema::get variation data from order item code WC 11.3.0
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 );
}