Automattic\WooCommerce\StoreApi\Schemas\V1
OrderItemSchema::get_item_data
Get order item metadata as a list.
Keyed by meta row ID at source; the Store API sends a list, so the ID moves into id.
Метод класса: OrderItemSchema{}
Хуков нет.
Возвращает
array.
Использование
// private - только в коде основоного (родительского) класса $result = $this->get_item_data( $order_item );
- $order_item(WC_Order_Item) (обязательный)
- Order item instance.
Код OrderItemSchema::get_item_data() OrderItemSchema::get item data WC 11.2.2
private function get_item_data( $order_item ) {
$item_data = [];
// A callback appending with `$formatted_meta[] =` gets PHP's next integer key, not a row ID.
// Mirror what `get_formatted_meta_data()` skips for this call's `_` prefix and `$include_all`:
// a row it leaves out never keys an entry, so counting its ID lets that key land on a hidden
// row saved right after the visible ones. Read rows first; formatting rewrites keys and values.
$meta_row_ids = [];
/**
* `get_meta_data()` is documented as returning bare objects.
*
* @var \WC_Meta_Data[] $meta_rows
*/
$meta_rows = $order_item->get_meta_data();
foreach ( $meta_rows as $meta ) {
if ( empty( $meta->id ) || '' === $meta->value || ! is_scalar( $meta->value ) || 0 === strpos( (string) $meta->key, '_' ) ) {
continue;
}
$meta_row_ids[ $meta->id ] = true;
}
$formatted_meta_data = $order_item->get_all_formatted_meta_data();
// A `woocommerce_order_item_get_formatted_meta_data` callback can return anything, and a bad
// one must not take the endpoint down.
if ( ! is_array( $formatted_meta_data ) ) {
return $item_data;
}
foreach ( $formatted_meta_data as $meta_id => $meta ) {
// Only public fields are meant to ship. Casting an object reaches past them and
// publishes mangled names, so let it serialize itself first, then read what is public.
if ( $meta instanceof \JsonSerializable ) {
$meta = $meta->jsonSerialize();
}
if ( is_object( $meta ) ) {
$meta = get_object_vars( $meta );
}
if ( ! is_array( $meta ) ) {
continue;
}
// Union keeps the left operand, so a callback's own `id` cannot shadow the row ID.
$item_data[] = [ 'id' => isset( $meta_row_ids[ $meta_id ] ) ? $meta_id : null ] + $meta;
}
return $item_data;
}