I have a slight problem when trying to use woocommerce rest api.
I have next structure:
...../plugins/woocommerce/
save-parsed-products-ajax.php
vendor/
automattic/
WooCommerce/
HttpClient/...
Client.php
...
autoload.php
save-parsed-products-ajax.php
<?php
$consumer_key = 'here_is_my_key'; // here was my real valid consumer key
$consumer_secret = 'here_is_secret'; // here was my real consumer secret
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'https://www.mywebsite.ru/', // here was my real website url
$consumer_key,
$consumer_secret,
[
'wp_api' => true,
'version' => 'wc/v3',
'query_string_auth' => true // Force Basic Authentication as query string true and using under HTTPS
]
);
print_r($woocommerce->get('products'));
?>
output
Fatal error: Uncaught Error: Class 'Automattic\WooCommerce\Client' not found in
/var/www/u1111184/data/www/mywebsite.ru/wp-content/plugins/woocommerce/save-parsed-products-
ajax.php:11
Stack trace: #0 {main} thrown in /var/www/u1111184/data/www/mywebsite.ru/wp-
content/plugins/woocommerce/save-parsed-products-ajax.php on line 11
I found this video that did same steps I did but my is not working. Could anyone help?
You can try following code
require_once ('..\vendor\autoload.php');
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
$woocommerce = new Client(
'https://www.mywebsite.ru',
'ck_****************************************',
'cs_****************************************',
[
'wp_api' => true,
'version' => 'wc/v3',
'query_string_auth' => true
]
);
print_r($woocommerce->get('products'));
Error will remain same until we do not use exception class
Related
require __DIR__ . '/woo-api/vendor/autoload.php';
use Automattic\WooCommerce\HttpClient\HttpClientException;
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'https://mysite.info/',
$ck,
$cs,
[
'wp_api' => true, 'version' => 'wc/v3',
]
);
$data = ['status' => 'processing'];
$woocommerce->put('orders/1242', $data); // Not Working
$woocommerce->get('products')); // Working
$woocommerce->get('products')); // Working
$woocommerce->get('customers',array('per_page' => 100, 'page' => $page)));// Working
i'm trying to update order status .. but this is not working ! :(
is there anything wrong with this code ?
$woocommerce->get('products')); => working
$woocommerce->get('customers',array('per_page'=>100,'page' => $page)) => working
With the same code i can get the customers data, product data...
but when i try to Update an order
its giving me "HTTP ERROR 500"
And the error log is mentioned Bellow Error Log :: –
/home2/wpqwerty/public_html/wooapi/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(422): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()
#1 /home2/wpqwerty/public_html/woo-api/vendor/automattic/woocommerce/src/WooCommerce/Client.php(69): Automattic\WooCommerce\HttpClient\HttpClient->request('orders/1242', 'PUT', Array)
#2 /home2/wpqwerty/public_html/test_api.php(16): Automattic\WooCommerce\Client->put('orders/1242', Array) #3 {main} thrown in /home2/wpqwerty/public_html/wooapi/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 378
Within login.php in the Auth0 starter app for PHP, if you run the following code, which comes straight from the documentation, you won't be able to get the user information.
I.e., $auth0->getUser(); will return NULL.
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/dotenv-loader.php';
$auth0 = new Auth0\SDK\Auth0([
'domain' => $_ENV['AUTH0_DOMAIN'],
'client_id' => $_ENV['AUTH0_CLIENT_ID'],
'redirect_uri' => $_ENV['AUTH0_CALLBACK_URL'],
'audience' => $_ENV['AUTH0_AUDIENCE'],
'scope' => 'openid profile email',
]);
$auth0->login();
It took me a while to figure this out, but you'll need to add the client secret within the new instance of auth0. I.e. add the following snippet: 'client_secret' => $_ENV['AUTH0_CLIENT_SECRET'],
The final code should look like:
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/dotenv-loader.php';
$auth0 = new Auth0\SDK\Auth0([
'domain' => $_ENV['AUTH0_DOMAIN'],
'client_id' => $_ENV['AUTH0_CLIENT_ID'],
'redirect_uri' => $_ENV['AUTH0_CALLBACK_URL'],
'audience' => $_ENV['AUTH0_AUDIENCE'],
'scope' => 'openid profile email',
]);
$auth0->login();
At this point, when you call $auth0->getUser(); it will return the full user object.
Creating a order from php rest api of woo-commerce version: wc/v3 and getting error
other apis are working fine tried with v2 still getting the same error
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$wc = new Client(
'https://example.com/',
'client key',
'client secret',
[
'wp_api' => true,
'version' => 'wc/v3',
]
);
$data = [
'payment_method' => 'cod',
....
'shipping_lines' => [
[
'method_id' => 'flat_rate',
'method_title' => 'Flat Rate',
'total' => 10
]
]
];
print_r($wc->post('orders', $data));
Getting this error, if i remove shipping lines working fine and creating the order
[07-May-2019 06:39:45 UTC] PHP Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: Error: Invalid parameter(s): shipping_lines [rest_invalid_param] in /home/gathhnaw/public_html/mapi/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:350
Stack trace:
#0 /home/gathhnaw/public_html/mapi/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(386): Automattic\WooCommerce\HttpClient\HttpClient->lookForErrors(Object(stdClass))
#1 /home/gathhnaw/public_html/mapi/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(422): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()
#2 /home/gathhnaw/public_html/mapi/vendor/automattic/woocommerce/src/WooCommerce/Client.php(56): Automattic\WooCommerce\HttpClient\HttpClient->request('orders', 'POST', Array)
#3 /home/gathhnaw/public_html/mapi/create_order.php(46): Automattic\WooCommerce\Client->post('orders', Array)
#4 {main}
thrown in /home/gathhnaw/public_html/mapi/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 350
There seems to be a typo or documentation error on woo api. Just surround the total value between single quotes and run it
https://www.infocaptor.com/dashboard/woocommerce-rest-api-php-example-error-creating-order
I use woocommerce rest api for managing products and other things. Here are the step I follow in order to do it:
Step:1 Goto woocommerce settings->advance->
Legacy API
Step:2 Create REST API
Step:3 Code
$autoloader = dirname( __FILE__ ) . '/vendor/autoload.php';
if ( is_readable( $autoloader ) ) {
require_once $autoloader;
}
use wordpress\CorePhp\WooCommerce\Client;
$woocommerce = new Client(
'http://localhost/wordpress',
'ck_44d1f5a2a193274e4dfba6ee7ec764cedd345000',
'cs_6be79b8bd5ec0dbb1828309db24ae338abce44b3',
[
'wp_api' => true,
'version' => 'wc/v2'
]
);
?>
Fatal error: Uncaught Error: Class
'wordpress\CorePhp\WooCommerce\Client' not found in
C:\xampp\htdocs\wordpress\CorePhp\getproducts.php:9 Stack trace: #0
{main} thrown in C:\xampp\htdocs\wordpress\CorePhp\getproducts.php on
line 9
i solve above issue there is issue with not install autoload in project folder that's why it give error here are the code which working proper :
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
$woocommerce = new Client(
'http://localhost/wordpress',
'your consumer key',
'your secret key',
[
'wp_api' => true,
'version' => 'wc/v2'
]
);
/*var_dump($woocommerce);*/
?>
I am trying to use zeuxisoo/slim-whoops to show errors, but for some reason I am only getting white screen without any message show. This is the code I am using (I don't know if it does matter, I am using quick PHP host (php -S localhost:8000):
require __DIR__ . '/../vendor/autoload.php';
$app = new Slim\App([
'settings' => [
'displayErrorDetails' => true,
'debug' => true,
'whoops.editor' => 'sublime',
]
]);
$app->add(new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware);
Make sure that your file begins with <?php and that after your $app->add(...); you call $app->run();:
<?php
require __DIR__ . '/../vendor/autoload.php';
$app = new Slim\App([
'settings' => [
'displayErrorDetails' => true,
'debug' => true,
'whoops.editor' => 'sublime',
]
]);
$app->add(new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware);
$app->run();