A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
Hi, I have a page with a list of order, I need insert into DB but how I can uderstand how many "line_items" for create a "for cicle"?
$products_obj_url = $api_url . '/admin/api/2022-07/orders.json?status=any&limit=250&since_id='.$id;}
$products_content = @file_get_contents( $products_obj_url );
$products_json = json_decode( $products_content, true );
$orders = $products_json['orders'];
//echo "</table>".$products_obj_url."<table>";
foreach($orders as $orders){
echo "<tr>
<td>".$conto."</td>
<td>". $orders['id']. "</td>
<td>". $orders['line_items'][0]['id']. "</td>
<td>". $orders['line_items'][0]['name']. "</td>
<td>". $orders['line_items'][0]['price']. "</td>
<td>". $orders['line_items'][0]['product_id']. "</td>
<td>". $orders['line_items'][0]['sku']. "</td>
<td>". $orders['line_items'][0]['quantity']. "</td>
";
in this case I read only the first "item", I need understand how many items there are and after create a cicle "for"
How I can to do?
Dear @SCoccia,
Thank you for reaching out to the Shopify community.
Based on my understanding of your question regarding orders and their line items in a loop, I would like to inform you that a Shopify order resource contains an array called "line_items." This array can be iterated to display individual line items within a loop.
Please find below the revised code for better clarity:
$products_obj_url = $api_url."/admin/api/2022-07/orders.json?status=any&limit=250&since_id=".$id;
$products_content = @file_get_contents($products_obj_url); // Please ensure you are making a curl request along with the credentials to get the data from shopify
$products_json = json_decode($products_content, true);
$orders = $products_json["orders"] ?? [];
foreach($orders as $order) {
$line_items = $order["line_items"] ?? [];
$line_items_count = count($line_items);
foreach($line_items as $line_item) {
echo "<tr>
<td>".$conto."</td>
<td>". $order['id']. "</td>
<td>". $line_item['id']. "</td>
<td>". $line_item['name']. "</td>
<td>". $line_item['price']. "</td>
<td>". $line_item['product_id']. "</td>
<td>". $line_item['sku']. "</td>
<td>". $line_item['quantity']. "</td>"
</tr>";
}
}
Please feel free to give it a try and let me know if you find it helpful.