• I’m using the latest woocommerce API version and can not return orders only with a custom order_meta value. The API docs don’t seem to show any order properties for order meta. https://woothemes.github.io/woocommerce-rest-api-docs/

    Can anyone help with the correct parameters using the Woocommerce API V1 (latest) to get or retrieve orders only that have a set value for custom order meta?

Viewing 5 replies - 1 through 5 (of 5 total)
  • @askjayson

    I do not see any references for order meta in the docs.

    https://woothemes.github.io/woocommerce-rest-api-docs/#order-properties

    What meta do you need to filter by in orders?

    • This reply was modified 7 years, 9 months ago by Luke Cavanagh. Reason: clarify wording
    • This reply was modified 7 years, 9 months ago by Luke Cavanagh.
    Thread Starter Jayson T Cote

    (@askjayson)

    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 7 years, 9 months ago by Jayson T Cote. Reason: Render code better

    Could you try to use the filter argument for the query?

    /wp-json/wc/v1/orders?filter[meta_key]=your_meta_key&filter[meta_value]=required_meta_value

    This also includes an example filter.
    https://github.com/WP-API/WP-API/issues/2452

    • This reply was modified 7 years, 9 months ago by Luke Cavanagh. Reason: clarify wording
    Thread Starter Jayson T Cote

    (@askjayson)

    @lukecavanagh – that may just do the trick. I thought I had tried both filters. I will let you know how it goes here in a few. Luke, have you had any luck filtering by date as well? I’m assuming its the same parameter structure, i.e.

    
    'filter[created_at_min]' => '2016-01-01' 
    'filter[created_at_max]' => '2016-01-31'
    

    Thanks for your help!

    • This reply was modified 7 years, 9 months ago by Jayson T Cote.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘WC Rest API – custom order_meta’ is closed to new replies.