Thanks @lukecavanagh, Yes no reference for order meta in the API docs. I have set an order custom field, then updating the custom field value, once the order is submitted. The custom field is used to filter orders using the WC API by a 3rd party. We can return orders through the API, but we need to return only the orders with the custom field and value. FYI, using the php library (API wrapper) https://github.com/woothemes/wc-api-php/
Create field
function gum_woo_order_fields_custom( $fields ) {
$fields['order']['aaatex_qb' ] = array(
'label' => __('QB Import', 'woocommerce'),
'placeholder' => _x('aaatex 1', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('hidden2'),
'clear' => true,
//'default' => 'new',
);
return $fields;
}
Update field
function gum_woo_order_fields_custom_update( $order_id ) {
update_post_meta( $order_id, 'aaatex_qb', 'new' );
}
API Get and Response
$store_url = 'http://shop.lifespa.com';
//$store_url = rawurlencode( rtrim( $store_url, '/' ) );
$consumer_key = 'ck_b19dd8998398a762d0a5062e9fd1cef1c38bdcf1';
$consumer_secret = 'cs_12bb4182989d216d02a773586d680d481caef086';
$woocommerce = new Client(
$store_url,
$consumer_key,
$consumer_secret,
[
'wp_api' => true,
'version' => 'wc/v1',
'timeout' => 1000,
'query_string_auth' => false // Force Basic Authentication as query string true and using under HTTPS
]
);
try {
// Array of response results.
//$results = $woocommerce->get('customers');
// Example: ['customers' => [[ 'id' => 8, 'created_at' => '2015-05-06T17:43:51Z', 'email' => ...
// Last request data.
$lastRequest = $woocommerce->http->getRequest();
//$lastRequest->getUrl(); // Requested URL (string).
//$lastRequest->getMethod(); // Request method (string).
//$lastRequest->getParameters(); // Request parameters (array).
//$lastRequest->getHeaders(); // Request headers (array).
//$lastRequest->getBody(); // Request body (JSON).
// Last response data.
$lastResponse = $woocommerce->http->getResponse();
//$lastResponse->getCode(); // Response code (int).
//$lastResponse->getHeaders(); // Response headers (array).
//$lastResponse->getBody(); // Response body (JSON).
} catch (HttpClientException $e) {
print '<pre>';
$e->getMessage(); // Error message.
$e->getRequest(); // Last request data.
$e->getResponse(); // Last response data.
print '</pre>';
}
$api_data = [
'id' => 3520,
'customer_id' => 2,
'status' => 'on-hold',
'filter[meta]' => 'true',
'order_meta' => array(
'aaatex_qb' => 'new',
),
];
$results = $woocommerce->get('orders', $api_data );
print '<pre>';
print_r($results);
print '</pre>';
-
This reply was modified 8 years, 5 months ago by
Jayson T Cote. Reason: Render code better