0

I need to create an order in woocommerce REST API

I have tried following code

require('../wp-blog-header.php'); 

header("HTTP/1.1 200 OK"); 
header("Status: 200 All rosy");

require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

// Local Server
$woocommerce = new Client(
    'https://exmapledomain.com/', 
    'key1', 
    'key2',
    [
        'wp_api' => true,
        'timeout' => 45000,
        'version' => 'wc/v3',
    ]
);

print_r($woocommerce->get('products/803'));exit;

I am getting error like below

2020-10-07T11:38:40+05:30 CRITICAL Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: Error: Sorry, you cannot view this resource. [woocommerce_rest_cannot_view] in /opt/bitnami/apps/wordpress/htdocs/app/src/WooCommerce/HttpClient/HttpClient.php:324 Stack trace:

#0 /opt/bitnami/apps/wordpress/htdocs/app/src/WooCommerce/HttpClient/HttpClient.php(349): Automattic\WooCommerce\HttpClient\HttpClient->lookForErrors(Array)

#1 /opt/bitnami/apps/wordpress/htdocs/app/src/WooCommerce/HttpClient/HttpClient.php(385): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()

#2 /opt/bitnami/apps/wordpress/htdocs/app/src/WooCommerce/Client.php(82): Automattic\WooCommerce\HttpClient\HttpClient->request('products/803', 'GET', Array, Array)

#3 /opt/bitnami/apps/wordpress/htdocs/app/orderApi.php(38): Automattic\WooCommerce\Client->get('products/803')

#4 {main} thrown in /opt/bitnami/apps/wordpress/htdocs/app/src/WooCommerce/HttpClient/HttpClient.php on line 324

tech versions
1.PHP version:7.3.18
2.wordpress:4.5.2
3.woocommerce:5.5.1
4.REST api package :4.5.2

1 Answer 1

0

I have resolve that error by add following line in code.

'query_string_auth' => true

code should be like below

 // Local Server
$woocommerce = new Client(
    'https://exmapledomain.com/', 
    'key1', 
    'key2',
    [
        'wp_api' => true,
        'timeout' => 45000,
        'version' => 'wc/v3',
        'query_string_auth' => true,
    ]
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.