Error in a custom dashboard using WooCommerce REST API

Viewed 121

I'm working on a custom WooCommerce dashboard found here https://github.com/shahroznawaz/woo-dashboard

The problem is I'm getting an error "Fatal error: Uncaught Error: Cannot use object of type stdClass as array in E:\xampp\htdocs\woo\index.php:31 Stack trace: #0 {main} thrown in E:\xampp\htdocs\woo\index.php on line 31"

I used a var_dump($woocommerce); to check if the API returns true and it does.

Things I did:

  1. Installed the official WooCommerce API package using composer
  2. On the client site, enabled REST API (tested with insomnia - works)
  3. Created index.php on my localhost where the dashboard should be shown

The PHP code I used is

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

use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
$woocommerce = new Client(
    'https://myexample.com/', // My store URL
    'ck_xxxxxxxxxxxxxxxxxxxxxxxx', 
    'cs_xxxxxxxxxxxxxxxxxxxxxxxx', 
    [
        'wp_api' => true, 
        'version' => 'wc/v2'
        
    ]
);
//var_dump($woocommerce);
//$products = $woocommerce->get('products');
try {

    $results = $woocommerce->get('orders');
    $products = $woocommerce->get('products');
    $customers = $woocommerce->get('customers');
    $result = count($results);
    $customer = count($customers);
    $product = count($products);
    $query = ['date_min' => '2017-10-01', 'date_max' => '2021-10-30'];
    $sales = $woocommerce->get('reports/sales', $query);
    $sale = $sales[0]["total_sales"]; // This is where the error is thrown

    // Last request data.

    $lastRequest = $woocommerce->http->getRequest();
    $lastRequest->getUrl();
    $lastRequest->getMethod();
    $lastRequest->getParameters();
    $lastRequest->getHeaders();
    $lastRequest->getBody();

    // Last response data.

    $lastResponse = $woocommerce->http->getResponse();
    $lastResponse->getCode();
    $lastResponse->getHeaders();
    $lastResponse->getBody();
}

catch(HttpClientException $e) {
    $e->getMessage();
    $e->getRequest();
    $e->getResponse();
}

if (isset($_POST['btn-update'])) {
    $status = $_POST['bookId'];
    $st = $_POST['ostatus'];

    $woocommerce->put('orders/' . $status, array(
        'status' => $st
    ));
    header('Location: https://myexample.com/');
}


if (isset($_POST['btn-delete'])) {
    $oid = $_POST['cId'];

    $woocommerce->delete('orders/' . $oid, ['force' => true]);
    header('Location: https://myexample.com/');
}

When I try to comment out these two lines for troubleshooting

$sales = $woocommerce->get('reports/sales', $query);
$sale = $sales[0]["total_sales"];

then I'm getting an error when outputting a table of data

<table id='myTable' class='table table-striped table-bordered'>
                                        <thead>
                                            <tr>
                                                <th>Order #</th>
                                                <th>Customer</th>
                                                <th>Address</th>
                                                <th>Contact</th>
                                                <th>Order Date</th>
                                                <th>Status</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php 
                foreach($results as $details){
// The error points to the code below
                echo "<tr><td>" . $details["id"]."</td>
                          <td>" . $details["billing"]["first_name"].$details["billing"]["last_name"]."</td>
                          <td>" . $details["shipping"]["address_1"]."</td>
                          <td>" . $details["billing"]["phone"]."</td>
                          <td>" . $details["date_created"]."</td>
                          <td>" . $details["status"]."</td>
                          <td><a class='open-AddBookDialog btn btn-primary' data-target='#myModal' data-id=".$details['id']." data-toggle='modal'>Update</a>
                          <a class='open-deleteDialog btn btn-danger' data-target='#myModal1' data-id=".$details['id']." data-toggle='modal'>Delete</a></td></tr>";
                }
                ?>
                                        </tbody>
                                    </table>

Error message I'm getting is "Fatal error: Uncaught Error: Cannot use object of type stdClass as array in E:\xampp\htdocs\woo\index.php:154 Stack trace: #0 {main} thrown in E:\xampp\htdocs\woo\index.php on line 154"

enter image description here

I can't figure out why I'm getting this errors, any help is appreciated.

UPDATE:
I figured out how to fix the first error. I replaced $sale = $sales[0]["total_sales"]; with $sale = $sales[0]->total_sales; which seems to be working fine now.

But I'm still getting an error "Cannot use object of type stdClass as array" for this part here:

foreach($results as $details){

                echo "<tr><td>" . $details["id"]."</td>
                          <td>" . $details["billing"]["first_name"].$details["billing"]["last_name"]."</td>
                          <td>" . $details["shipping"]["address_1"]."</td>
                          <td>" . $details["billing"]["phone"]."</td>
                          <td>" . $details["date_created"]."</td>
                          <td>" . $details["status"]."</td>
                          <td><a class='open-AddBookDialog btn btn-primary' data-target='#myModal' data-id=".$details['id']." data-toggle='modal'>Update</a>
                          <a class='open-deleteDialog btn btn-danger' data-target='#myModal1' data-id=".$details['id']." data-toggle='modal'>Delete</a></td></tr>";
                }

Why can't I use this as array?

0 Answers
Related