Skip to content

Instantly share code, notes, and snippets.

@jtcote
Created September 8, 2016 18:08
Show Gist options
  • Save jtcote/c550bb1654673cc6e27fd1b40952b17c to your computer and use it in GitHub Desktop.
Save jtcote/c550bb1654673cc6e27fd1b40952b17c to your computer and use it in GitHub Desktop.
Woocommerce API Example
require_once 'lib/woocommerce-api.php';
$consumer_key = 'ck_b19dd8998398a762d0a5062e9fd1cef1c38bdcf1';
$consumer_secret = 'cs_12bb4182989d216d02a773586d680d481caef086';
$store_url = 'http://shop.lifespa.com/';
$options = array(
'debug' => true,
'return_as_array' => true,
'validate_url' => false,
'timeout' => 30,
'ssl_verify' => false,
);
try {
// Initialize the class
$wc_api = new WC_API_Client( $store_url, $consumer_key, $consumer_secret, $options );
} catch ( WC_API_Client_Exception $e ) {
echo $e->getMessage() . PHP_EOL;
echo $e->getCode() . PHP_EOL;
if ( $e instanceof WC_API_Client_HTTP_Exception ) {
print_r( $e->get_request() );
print_r( $e->get_response() );
}
}
// Get All Orders
$orders_all = $wc_api->orders->get();
//Get Completed Orders
$orders_completed = $wc_api->orders->get(NULL, array(
'filter[meta]' => 'true',
'status' => 'completed'
//'aaatex_qb' => 'new',
));
//Get On Hold Orders
$orders_onhold = $wc_api->orders->get(NULL, array(
'status' => 'on-hold'
//'aaatex_qb' => 'new',
));
//Get Orders based on order_meta Value
$orders_meta = $wc_api->orders->get(NULL, array(
'status' => 'on-hold',
'filter[meta]' => 'true',
'order_meta' => array(
'aaatex_qb' => 'new',
),
));
echo '<h2>API Request Result</h2>';
echo '<ul>';
foreach ( $orders_meta AS $array => $rows ) {
foreach ( $rows AS $key => $value ) {
if ( 'new' != $value['order_meta']['aaatex_qb'] ) continue;
echo '<li>';
echo 'order_id: ' .$value['id'] .' ';
echo 'aaatex_qb: ' .$value['order_meta']['aaatex_qb'] .'<br />';
echo '</li>';
}
}
echo '</ul>';
echo '<pre>';
print_r( $orders_meta );
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment