PHP Curl orders with multi line_items

6 1 4

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>". $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?

Reply 1 (1)

Shopify Partner
40 5 5


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>". $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>"


Please feel free to give it a try and let me know if you find it helpful.

Cheetos || Recurpay Subscriptions

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution