error in woocommerce REST Api integration - php

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);*/
?>

Related

Class "Stripe\Stripe" not found in codeigniter 4

i downloaded stripe php library by using Composer with this command
composer require stripe/stripe-php
then i added this in my app/config/autoload.php
public $psr4 = [ APP_NAMESPACE => APPPATH, // For custom app
namespace 'Config' => APPPATH . 'Config',
'Stripe' =>> ROOTPATH . 'vendor/stripe/stripe-php/init.php' // added here ];
then in controller i added this code but it giving me error Stripe/Stripe Not found
use Stripe;
\Stripe\Stripe::setApiKey(STRIPE_SECRET);
$paymentData = \Stripe\Charge::create ([
"amount" => $this->request->getPost('grandtotal'),
"currency" => "GBP",
"source" => $_REQUEST["stripeToken"],
"description" => "Test payment via Stripe From Rahul"
]);
$paymentData = array('success' => true, 'data'=> $stripe);
echo '<pre>' , var_dump($paymentData) , '</pre>';
i followed one tutorial but it giving me this error, please help me
You could use autoloader to actually include the Stripe classes...
require_once('vendor/autoload.php');

Why is Woocommerce Order Update API Giving "HTTP ERROR 500"?

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

Woo-com Rest api order update is not working

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

Class 'Automattic\WooCommerce\Client' not found

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

WooCommerce Rest API returning invalid paramter 'shipping_lines' in create order

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

Categories