Skip to content

Instantly share code, notes, and snippets.

@k-msalehi
Last active April 15, 2024 13:51
Show Gist options
  • Save k-msalehi/ad440889548965bd11cc86566a987fff to your computer and use it in GitHub Desktop.
Save k-msalehi/ad440889548965bd11cc86566a987fff to your computer and use it in GitHub Desktop.
emalls woo api
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'https://example.com',
'ck_xxxxxxxxxxxxxxxxxxxx',
'cs_xxxxxxxxxxxxxxxxxxxx',
[
'version' => 'wc/v3',
]
);
try {
$data = $woocommerce->get('reports/products/totals');
} catch (Exception $e) {
$data = "\n Exception Caught" . $e->getMessage();
}
$key = 'total';
$productsCount = array_sum(array_column($data, $key));
// $productsCount = 2444;
if (isset($_GET['search'])) {
$query['search'] = $_GET['search'];
}
$query['per_page'] = $_GET['size'] ?? 10;
$query['page'] = $_GET['page'] ?? 1;
if ($query['per_page'] > 100)
$query['per_page'] = 100;
$data = $woocommerce->get('products', $query);
$i = 0;
foreach ($data as $product) {
$products['products'][$i]['id'] = $product->id;
$products['products'][$i]['title'] = $product->name;
$products['products'][$i]['url'] = '/products/' . $product->slug . '/';
$products['products'][$i]['price'] = $product->price;
$products['products'][$i]['is_available'] = ($product->stock_status == 'instock') ? true : false;
foreach ($product->attributes as $attribute) {
//233 for guarantee
if ($attribute->id == 233)
foreach ($attribute->options as $option) {
if ($option != 'گارانتی دارد')
$products['products'][$i]['guarantee'] = $option;
}
}
$i++;
}
if (empty($products))
$products['success'] = false;
else
$products['success'] = true;
$products['total'] = $productsCount;
echo (json_encode($products));
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment