Skip to content

Instantly share code, notes, and snippets.

@susanBuck
Created May 29, 2024 16:39
Show Gist options
  • Save susanBuck/8f94a18c97438327830344bfbd68afb5 to your computer and use it in GitHub Desktop.
Save susanBuck/8f94a18c97438327830344bfbd68afb5 to your computer and use it in GitHub Desktop.
Examples for Jason
<!doctype html>
<html lang='en'>
<head>
<title>Shopify</title>
<meta charset='utf-8'>
<link href=data:, rel=icon>
</head>
<body>
@if (isset($productId))
<h1>Showing only orders containing Product ID {{ $productId }}</h1>
@else
<h1>Showing all orders</h1>
@endif
<h2>Order Count: {{ $count }}</h2>
@foreach ($orders as $order)
<h2>Order {{ $order['node']['name'] }}</h2>
Created At: {{ $order['node']['createdAt'] }}<br>
Items:
@foreach ($order['node']['lineItems']['edges'] as $lineItems)
<li>{{ $lineItems['node']['title'] }} ({{ str_replace('gid://shopify/ProductVariant/', '', $lineItems['node']['variant']['id']) }})</li>
@endforeach
@endforeach
</body>
</html>
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PHPShopify\ShopifySDK;
class ShopifyController extends Controller
{
/**
*
*/
public function index(Request $request)
{
# Config info redacted since this is a public file; if using, please sub in your config info
$config = [
'ShopUrl' => 'xyz.myshopify.com',
'AccessToken' => 'xyz',
'ApiKey' => 'xyz',
'SharedSecret' => 'xyz',
];
# Initialize ShopifySDK with the above config
$shopify = new ShopifySDK($config);
# Build a query
# Ref: https://shopify.dev/docs/api/admin-graphql/2024-04/queries/orders?language=PHP
# Confirmed max limit on queries is 250; for more than that they want you to use their
# bulk operation system described here:
# https://shopify.dev/docs/api/usage/bulk-operations/queries
$graphQL = <<<Query
{
orders(first: 250) {
edges {
node {
id
name
createdAt
lineItems(first: 250) {
edges {
node {
title
quantity
variant {
id
title
}
}
}
}
}
}
}
}
Query;
# Execute the query
$results = $shopify->GraphQL->post($graphQL);
# Extract just the order data from the results
$orders = $results['data']['orders']['edges'];
# dump($orders);
# Return a view to display the results
return view('shopify')->with([
'orders' => $orders,
'count' => count($orders)
]);
}
/**
*
*/
public function filtered(Request $request)
{
# Config info redacted since this is a public file; if using, please sub in your config info
$config = [
'ShopUrl' => 'xyz.myshopify.com',
'AccessToken' => 'xyz',
'ApiKey' => 'xyz',
'SharedSecret' => 'xyz',
];
# Initialize ShopifySDK with the above config
$shopify = new ShopifySDK($config);
$graphQL = <<<Query
{
orders(first: 250) {
edges {
node {
id
name
createdAt
lineItems(first: 250) {
edges {
node {
title
quantity
variant {
id
title
}
}
}
}
}
}
}
}
Query;
# Execute the query
$results = $shopify->GraphQL->post($graphQL);
# Extract just the order data from the results
$orders = $results['data']['orders']['edges'];
# Specify product ID to filter orders by
$productId = 44957054828841; # Happy Skin
$filteredOrders = [];
foreach($orders as $order) {
foreach ($order['node']['lineItems']['edges'] as $lineItems) {
if(str_contains($lineItems['node']['variant']['id'], $productId)) {
$filteredOrders[] = $order;
}
}
}
# Return a view to display the results
return view('shopify')->with([
'orders' => $filteredOrders,
'count' => count($filteredOrders),
'productId' => $productId
]);
}
}
Route::get('/shopify', [ShopifyController::class, 'index']);
Route::get('/shopify-filtered', [ShopifyController::class, 'filtered']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment