admin管理员组

文章数量:1122832

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(
    '/', 
    '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

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

Share Improve this question edited Oct 7, 2020 at 6:53 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Oct 7, 2020 at 6:27 Pranav PatelPranav Patel 211 gold badge1 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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,
    ]
);

本文标签: Getting in an exception for get product detail by id in woocommerce REST api