Format item meta

This commit is contained in:
Mike Jolley 2016-08-16 13:55:10 +01:00
parent 99be0167b4
commit b64c55de49
2 changed files with 53 additions and 5 deletions

View File

@ -50,6 +50,12 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
*/
protected $dp = '2';
/**
* Stores the request.
* @var array
*/
protected $request = array();
/**
* Initialize orders actions.
*/
@ -133,6 +139,20 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
}
}
// Remove order id
unset( $data['order_id'] );
// Format meta data
$hideprefix = 'true' === $this->request['all_item_meta'] ? null : '_';
$item_meta = $item->get_formatted_meta_data( $hideprefix );
foreach ( $item_meta as $key => $values ) {
// Label was used in previous version of API - set it here
$item_meta[ $key ]->label = $values->display_key;
}
$data['meta'] = $item_meta;
return $data;
}
@ -146,6 +166,7 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
public function prepare_item_for_response( $post, $request ) {
global $wpdb;
$this->request = $request;
$this->dp = $request['dp'];
$format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' );
$format_date = array( 'date_created', 'date_modified', 'date_completed' );
@ -167,13 +188,23 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
// Format the order status
$data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status'];
// Format meta data
$data['meta'] = $data['meta_data'];
unset( $data['meta_data'] );
// Format line items
foreach ( $format_line_items as $key ) {
$data[ $key ] = array_map( array( $this, 'get_order_item_data' ), $data[ $key ] );
}
// refunds
foreach ( $order->get_refunds() as $refund ) {
$data['refunds'][] = array(
'id' => $refund->id,
'refund' => $refund->get_refund_reason() ? $refund->get_refund_reason() : '',
'total' => '-' . wc_format_decimal( $refund->get_refund_amount(), $this->dp ),
);
}
/*
// Add addresses.
@ -225,6 +256,25 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
'meta' => $item_meta,
);
sku
price
"product_id": 0,
"variation_id": 0,
"qty": 0,
"tax_class": "",
"subtotal": "0.00",
"subtotal_tax": "0.00",
"total": "0.00",
"total_tax": "0.00",
"taxes": {
"subtotal": [],
"total": []
},
"meta_data":
$item_line_taxes = maybe_unserialize( $item['line_tax_data'] );
if ( isset( $item_line_taxes['total'] ) ) {
$line_tax = array();

View File

@ -73,8 +73,6 @@ class WC_Order_Item extends WC_Data implements ArrayAccess {
foreach ( $data as $key => $value ) {
if ( is_callable( array( $this, "set_$key" ) ) ) {
$this->{"set_$key"}( $value );
} else {
$this->_data[ $key ] = $value;
}
}
}
@ -235,7 +233,7 @@ class WC_Order_Item extends WC_Data implements ArrayAccess {
if ( is_numeric( $item ) && ! empty( $item ) ) {
$data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item ) );
} elseif ( ! empty( $item->id ) ) {
} elseif ( ! empty( $item->order_item_id ) ) {
$data = $item;
} else {
$data = false;