Created
November 24, 2020 21:03
-
-
Save DustinHartzler/3c64352f5c54fcc6761d1cc8eff35f14 to your computer and use it in GitHub Desktop.
Example for pulling in data from the REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<h1>REST API Application</h1> | |
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
use Automattic\WooCommerce\Client; | |
use Automattic\WooCommerce\HttpClient\HttpClientException; | |
$woocommerce = new Client('https://ENTER_YOUR_URL_HERE.com', | |
'ck_XXXXXXXXXXXXX', | |
'cs_XXXXXXXXXXXXX', | |
[ | |
'wp_api' => true, 'version' => 'wc/v1',]); | |
try { | |
$results = $woocommerce->get('orders', ['per_page'=>5]); | |
$products = $woocommerce->get('products', ['per_page'=>7]); | |
$customers = $woocommerce->get('customers', ['per_page'=>5]); | |
$result = count($results); | |
$customer = count($customers); | |
$product = count($products); | |
//you can set any date which you want | |
$query = ['date_min' => '2017-10-01', 'date_max' => '2017-10-30']; | |
$sales = $woocommerce->get('reports/sales', $query); | |
$sale = $woocommerce->get('total'); | |
} | |
catch(HttpClientException $e) { | |
$e->getMessage(); // Error message. | |
$e->getRequest(); // Last request data. | |
$e->getResponse(); // Last response data. | |
} | |
?> | |
<div class="container"> | |
<h2 class="sub-header">Orders List</h2> | |
<div class='table-responsive'> | |
<table id='myTable' class='table table-striped table-bordered'> | |
<thead> | |
<tr> | |
<th>Order #</th> | |
<th>Customer</th> | |
<th>Amount Spent</th> | |
<th>Order Date</th> | |
<th>Status</th> | |
<th>Actions</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
foreach($results as $details){ | |
echo "<tr><td>" . $details->id."</td> | |
<td>" . $details->{billing}->first_name . " " . $details->{billing}->last_name."</td> | |
<td>" . $details->total."</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> | |
</div> | |
</div> | |
<div class="container"> | |
<h2 class="sub-header">Products List</h2> | |
<div class='table-responsive'> | |
<table id='myTable' class='table table-striped table-bordered'> | |
<thead> | |
<tr> | |
<th>SKU</th> | |
<th>Name</th> | |
<th>Status</th> | |
<th>Price</th> | |
<th>Total Sales</th> | |
<th>Picture</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
foreach($products as $product){ | |
echo "<tr><td>" . $product->sku."</td> | |
<td>" . $product->name."</td> | |
<td>" . $product->status."</td> | |
<td>" . $product->price."</td> | |
<td>" . $product->total_sales."</td> | |
<td><img height='50px' width='50px' src='".$product->images{0}->src ."'></td></tr>"; | |
} | |
?> | |
</tbody> | |
</table> | |
</div> | |
</div> | |
<div class="container"> | |
<h2 class="sub-header">Customers List</h2> | |
<div class='table-responsive'> | |
<table id='myTable' class='table table-striped table-bordered'> | |
<thead> | |
<tr> | |
<th>Email</th> | |
<th>Name</th> | |
<th>Billing Address</th> | |
<th>Total Orders</th> | |
<th>Total spent</th> | |
<th>Avatar</th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
foreach($customers as $customer){ | |
echo "<tr><td>" . $customer->email."</td> | |
<td>" . $customer->first_name.$customer->last_name."</td> | |
<td>" . $customer->billing->address_1."</td> | |
<td>" . $customer->orders_count."</td> | |
<td>" . $customer->total_spent."</td> | |
<td><img height='50px' width='50px' src='".$customer->avatar_url."'></td></tr>"; | |
} | |
?> | |
</tbody> | |
</table> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment