How to get shipping rate with method

2019-09-01 05:17发布

I am using woo-commerce REST API for getting shipping methods Woocommerce REST API

I have called $woocommerce->get('shipping/zones/3/methods') but not receiving any output it's given errorUncaught Automattic\WooCommerce\HttpClient\HttpClientException following is my code

<?php
    include('../wp-load.php');
    require_once( 'woo-lib/autoload.php' );
    require_once( 'auth_keys.php' );
    use Automattic\WooCommerce\Client;


    if(isset($keys) && !empty($keys)){
       $woocommerce = new Client(
            $keys['siteurl'], 
            $keys['consumer_key'], 
            $keys['consumer_secret'],
            [
                'wp_api' => true,
                'version' => 'wc/v2',
            ]
        ); 
    }



     print_r($woocommerce->get('shipping/zones/3/methods'));

die;

If someone know about Woocommerce REST API then please let me know how could i get all shipping methods with it's cost (also zone vise)

1条回答
孤傲高冷的网名
2楼-- · 2019-09-01 05:45

The error means that your code produced a "HttpClientException" exception and you did not handled it. To catch an exception you can use an try/catch bloc.

With your code it could be look like this:

include('../wp-load.php');
require_once( 'woo-lib/autoload.php' );
require_once( 'auth_keys.php' );

use Automattic\WooCommerce\HttpClient\HttpClientException;
use Automattic\WooCommerce\Client;


if(isset($keys) && !empty($keys))
{

    $woocommerce = new Client(
        $keys['siteurl'], 
        $keys['consumer_key'], 
        $keys['consumer_secret'],
        [
            'wp_api' => true,
            'version' => 'wc/v2',
        ]
    ); 

    try {

        $result = $woocommerce->get('shipping/zones/3/methods');

    } catch ( HttpClientException $e ) {

        // print out the exception message => maybe it tells you the error
        print_r($e->getMessage() );
    }

    print_r($result);

}
查看更多
登录 后发表回答