1
<?php
require "../core.php";
require "../Curl.php";

$ch = new Curl();

// echo $functions->ShopifyApi()."/admin/orders.json";
$orders = json_decode($ch->get($functions->ShopifyApi()."/admin/orders.json"), true);
$placements = $orders->orders;

echo $placements;

This is the line giving issues: foreach ($placements as $order) {

foreach ($order->line_items as $item) {




foreach ($order->note_attributes as $attributes) {
    $req      = array();

    $customer = array();

    $req[$attributes->name] = $attributes->value;

and the code continues .... Why when executing the script I am getting the error Warning: Invalid argument supplied for foreach() in /home/xxxxxxxx/public_html/xxx/xxxxx/script.php on line 13

Sample JSON that is mean't to come from: $functions->ShopifyApi()."/admin/orders.json

 {
  "orders": [
    {
      "buyer_accepts_marketing": false,
      "cancel_reason": null,
      "cancelled_at": null,
      "cart_token": "68778783ad298f1c80c3bafcddeea02f",
      "checkout_token": null,
      "closed_at": null,
      "confirmed": false,
      "created_at": "2008-01-10T11:00:00-05:00",
      "currency": "USD",
      "device_id": null,
      "email": "bob.norman@hostmail.com",
      "financial_status": "authorized",
      "fulfillment_status": null,
      "gateway": "authorize_net",
      "id": 450789469,
      "landing_site": "http:\/\/www.example.com?source=abc",
      "location_id": null,
      "name": "#1001",
      "note": null,
      "number": 1,
      "processed_at": "2008-01-10T11:00:00-05:00",
      "reference": "fhwdgads",
      "referring_site": "http:\/\/www.otherexample.com",
      "source_identifier": "fhwdgads",
      "source_url": null,
      "subtotal_price": "398.00",
      "taxes_included": false,
      "test": false,
      "token": "b1946ac92492d2347c6235b4d2611184",
      "total_discounts": "0.00",
      "total_line_items_price": "398.00",
      "total_price": "409.94",
      "total_price_usd": "409.94",
      "total_tax": "11.94",
      "total_weight": 0,
      "updated_at": "2008-01-10T11:00:00-05:00",
      "user_id": null,
      "browser_ip": null,
      "landing_site_ref": "abc",
      "order_number": 1001,
      "discount_codes": [
        {
          "code": "TENOFF",
          "amount": "10.00",
          "type": "percentage"
        }
      ],
      "note_attributes": [
        {
          "name": "custom engraving",
          "value": "Happy Birthday"
        },
        {
          "name": "colour",
          "value": "green"
        }
      ],
      "processing_method": "direct",
      "source": "browser",
      "checkout_id": 450789469,
      "source_name": "web",
      "tax_lines": [
        {
          "price": "11.94",
          "rate": 0.06,
          "title": "State Tax"
        }
      ],
      "tags": "",
      "line_items": [
        {
          "fulfillment_service": "manual",
          "fulfillment_status": null,
          "gift_card": false,
          "grams": 200,
          "id": 466157049,
          "price": "199.00",
          "product_id": 632910392,
          "quantity": 1,
          "requires_shipping": true,
          "sku": "IPOD2008GREEN",
          "taxable": true,
          "title": "IPod Nano - 8gb",
          "variant_id": 39072856,
          "variant_title": "green",
          "vendor": null,
          "name": "IPod Nano - 8gb - green",
          "variant_inventory_management": "shopify",
          "properties": [
            {
              "name": "Custom Engraving Front",
              "value": "Happy Birthday"
            },
            {
              "name": "Custom Engraving Back",
              "value": "Merry Christmas"
            }
          ],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "total_discount": "0.00",
          "tax_lines": [
          ]
        },
        {
          "fulfillment_service": "manual",
          "fulfillment_status": null,
          "gift_card": false,
          "grams": 200,
          "id": 518995019,
          "price": "199.00",
          "product_id": 632910392,
          "quantity": 1,
          "requires_shipping": true,
          "sku": "IPOD2008RED",
          "taxable": true,
          "title": "IPod Nano - 8gb",
          "variant_id": 49148385,
          "variant_title": "red",
          "vendor": null,
          "name": "IPod Nano - 8gb - red",
          "variant_inventory_management": "shopify",
          "properties": [
          ],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "total_discount": "0.00",
          "tax_lines": [
          ]
        },
        {
          "fulfillment_service": "manual",
          "fulfillment_status": null,
          "gift_card": false,
          "grams": 200,
          "id": 703073504,
          "price": "199.00",
          "product_id": 632910392,
          "quantity": 1,
          "requires_shipping": true,
          "sku": "IPOD2008BLACK",
          "taxable": true,
          "title": "IPod Nano - 8gb",
          "variant_id": 457924702,
          "variant_title": "black",
          "vendor": null,
          "name": "IPod Nano - 8gb - black",
          "variant_inventory_management": "shopify",
          "properties": [
          ],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "total_discount": "0.00",
          "tax_lines": [
          ]
        }
      ],
      "shipping_lines": [
        {
          "code": "Free Shipping",
          "price": "0.00",
          "source": "shopify",
          "title": "Free Shipping",
          "tax_lines": [
          ]
        }
      ],
      "billing_address": {
        "address1": "123 Amoebobacterieae St",
        "address2": "",
        "city": "Ottawa",
        "company": "",
        "country": "Canada",
        "first_name": "Bob",
        "last_name": "Bobsen",
        "latitude": 45.41634,
        "longitude": -75.6868,
        "phone": "(555)555-5555",
        "province": "Ontario",
        "zip": "K2P0V6",
        "name": "Bob Bobsen",
        "country_code": "CA",
        "province_code": "ON"
      },
      "shipping_address": {
        "address1": "123 Amoebobacterieae St",
        "address2": "",
        "city": "Ottawa",
        "company": "",
        "country": "Canada",
        "first_name": "Bob",
        "last_name": "Bobsen",
        "latitude": 45.41634,
        "longitude": -75.6868,
        "phone": "(555)555-5555",
        "province": "Ontario",
        "zip": "K2P0V6",
        "name": "Bob Bobsen",
        "country_code": "CA",
        "province_code": "ON"
      },
      "fulfillments": [
        {
          "created_at": "2015-03-28T13:29:45-04:00",
          "id": 255858046,
          "order_id": 450789469,
          "service": "manual",
          "status": "failure",
          "tracking_company": null,
          "updated_at": "2015-03-28T13:29:45-04:00",
          "tracking_number": "1Z2345",
          "tracking_numbers": [
            "1Z2345"
          ],
          "tracking_url": "http:\/\/wwwapps.ups.com\/etracking\/tracking.cgi?InquiryNumber1=1Z2345&TypeOfInquiryNumber=T&AcceptUPSLicenseAgreement=yes&submit=Track",
          "tracking_urls": [
            "http:\/\/wwwapps.ups.com\/etracking\/tracking.cgi?InquiryNumber1=1Z2345&TypeOfInquiryNumber=T&AcceptUPSLicenseAgreement=yes&submit=Track"
          ],
          "receipt": {
            "testcase": true,
            "authorization": "123456"
          },
          "line_items": [
            {
              "fulfillment_service": "manual",
              "fulfillment_status": null,
              "gift_card": false,
              "grams": 200,
              "id": 466157049,
              "price": "199.00",
              "product_id": 632910392,
              "quantity": 1,
              "requires_shipping": true,
              "sku": "IPOD2008GREEN",
              "taxable": true,
              "title": "IPod Nano - 8gb",
              "variant_id": 39072856,
              "variant_title": "green",
              "vendor": null,
              "name": "IPod Nano - 8gb - green",
              "variant_inventory_management": "shopify",
              "properties": [
                {
                  "name": "Custom Engraving Front",
                  "value": "Happy Birthday"
                },
                {
                  "name": "Custom Engraving Back",
                  "value": "Merry Christmas"
                }
              ],
              "product_exists": true,
              "fulfillable_quantity": 1,
              "total_discount": "0.00",
              "tax_lines": [
              ]
            }
          ]
        }
      ],
      "client_details": {
        "accept_language": null,
        "browser_height": null,
        "browser_ip": "0.0.0.0",
        "browser_width": null,
        "session_hash": null,
        "user_agent": null
      },
      "refunds": [
        {
          "created_at": "2015-03-28T13:29:45-04:00",
          "id": 509562969,
          "note": "it broke during shipping",
          "order_id": 450789469,
          "restock": true,
          "user_id": 799407056,
          "refund_line_items": [
            {
              "id": 104689539,
              "line_item_id": 703073504,
              "quantity": 1,
              "line_item": {
                "fulfillment_service": "manual",
                "fulfillment_status": null,
                "gift_card": false,
                "grams": 200,
                "id": 703073504,
                "price": "199.00",
                "product_id": 632910392,
                "quantity": 1,
                "requires_shipping": true,
                "sku": "IPOD2008BLACK",
                "taxable": true,
                "title": "IPod Nano - 8gb",
                "variant_id": 457924702,
                "variant_title": "black",
                "vendor": null,
                "name": "IPod Nano - 8gb - black",
                "variant_inventory_management": "shopify",
                "properties": [
                ],
                "product_exists": true,
                "fulfillable_quantity": 1,
                "total_discount": "0.00",
                "tax_lines": [
                ]
              }
            },
            {
              "id": 709875399,
              "line_item_id": 466157049,
              "quantity": 1,
              "line_item": {
                "fulfillment_service": "manual",
                "fulfillment_status": null,
                "gift_card": false,
                "grams": 200,
                "id": 466157049,
                "price": "199.00",
                "product_id": 632910392,
                "quantity": 1,
                "requires_shipping": true,
                "sku": "IPOD2008GREEN",
                "taxable": true,
                "title": "IPod Nano - 8gb",
                "variant_id": 39072856,
                "variant_title": "green",
                "vendor": null,
                "name": "IPod Nano - 8gb - green",
                "variant_inventory_management": "shopify",
                "properties": [
                  {
                    "name": "Custom Engraving Front",
                    "value": "Happy Birthday"
                  },
                  {
                    "name": "Custom Engraving Back",
                    "value": "Merry Christmas"
                  }
                ],
                "product_exists": true,
                "fulfillable_quantity": 1,
                "total_discount": "0.00",
                "tax_lines": [
                ]
              }
            }
          ],
          "transactions": [
            {
              "amount": "209.00",
              "authorization": "authorization-key",
              "created_at": "2005-08-05T12:59:12-04:00",
              "currency": "USD",
              "gateway": "bogus",
              "id": 179259969,
              "kind": "refund",
              "location_id": null,
              "message": null,
              "order_id": 450789469,
              "parent_id": null,
              "status": "success",
              "test": false,
              "user_id": null,
              "device_id": null,
              "receipt": {},
              "error_code": null,
              "source_name": "web"
            }
          ]
        }
      ],
      "payment_details": {
        "avs_result_code": null,
        "credit_card_bin": null,
        "cvv_result_code": null,
        "credit_card_number": "•••• •••• •••• 4242",
        "credit_card_company": "Visa"
      },
      "customer": {
        "accepts_marketing": false,
        "created_at": "2015-03-28T13:29:45-04:00",
        "email": "bob.norman@hostmail.com",
        "first_name": "Bob",
        "id": 207119551,
        "last_name": "Norman",
        "last_order_id": 450789469,
        "multipass_identifier": null,
        "note": null,
        "orders_count": 1,
        "state": "disabled",
        "total_spent": "41.94",
        "updated_at": "2015-03-28T13:29:45-04:00",
        "verified_email": true,
        "tags": "",
        "last_order_name": "#1001",
        "default_address": {
          "address1": "Chestnut Street 92",
          "address2": "",
          "city": "Louisville",
          "company": null,
          "country": "United States",
          "first_name": null,
          "id": 207119551,
          "last_name": null,
          "phone": "555-625-1199",
          "province": "Kentucky",
          "zip": "40202",
          "name": "",
          "province_code": "KY",
          "country_code": "US",
          "country_name": "United States",
          "default": true
        }
      }
    }
  ]
}
  • What's the output of `echo $placements;`? – fvu Apr 27 '15 at 15:43
  • 1
    Because $placements is empty. – Masiorama Apr 27 '15 at 15:43
  • So which line is the 13th line? – Med Apr 27 '15 at 15:43
  • This is the line giving issues: foreach ($placements as $order) { – Don Yunger Kman Apr 27 '15 at 15:45
  • $placements is mean't to hold the elements from within the orders elements within this json array from $ch->get($functions->ShopifyApi()."/admin/orders.json") : -> { "orders":[ { "buyer_accepts_marketing":true, "cancel_reason":null, "cancelled_at":null, – Don Yunger Kman Apr 27 '15 at 15:47
  • Are you sure you are traversing correctly orders.json? Try to debug it step by step. – Masiorama Apr 27 '15 at 15:48
  • How would I go about doing so? if I browse to the JSON directly I see the array so I don't see what I am doing wrong. – Don Yunger Kman Apr 27 '15 at 15:51
  • First of all, what is the output of $orders? Share with us the decoded json :) – Masiorama Apr 27 '15 at 15:56
  • @Masiorama it looks like there is a issue around here: orders = json_decode($ch->get($functions->ShopifyApi()."/admin/orders.json"), true); $placements = $orders->orders; If you echo placements it's blank, something you was saying earlier how can I get the JSON through correctly to then be decoded correctly and make the foreach work? – Don Yunger Kman Apr 27 '15 at 15:59
  • I can't show you the actual JSON as it holds customer information, but I will update my question with a snippet of a sample JSON from shopify. – Don Yunger Kman Apr 27 '15 at 16:00
  • My suggestion is to forget the foreach for now. Focus on the row $orders = json_decode($ch->get($functions->ShopifyApi()."/admin/orders.json"), true); and split it in chunks... before decoding, see if the path to the json file is correct, and so on... check also if $functions->ShopifyApi() returns what you expect and so on ;) – Masiorama Apr 27 '15 at 16:02
  • I just realised the issue haha, The $functions->ShopifyApi() already has /admin/orders.json added to it and I put that again in the code lol – Don Yunger Kman Apr 27 '15 at 16:06
  • Actually still not working. – Don Yunger Kman Apr 27 '15 at 16:06
  • @Masiorama Path is correct, $functions->ShopifyApi() returns what is expected – Don Yunger Kman Apr 27 '15 at 16:11
  • So if I do file get contents and then var dump json decode I see the JSON in PHP array form. – Don Yunger Kman Apr 27 '15 at 16:24
  • From what you just said, if it is an array, why are you traversing with a foreach accessing an object $orders->orders instead of an array $orders['orders']? ... just guessing... – Masiorama Apr 27 '15 at 17:44
  • Hi again, about 5 minutes after posting my comment I noticed that and using ['var'] instead of ->var worked instead! Thanks :) – Don Yunger Kman Apr 27 '15 at 19:11

1 Answers1

0

Here's a thread with some examples, good practices and fixes for 'foreach' woes.

    $notes = $order->note_attributes;
    // validations on $notes ie. empty, array etc..

    foreach ((array) $notes as $note) {
       // validations on $note
       // do stuffs...
    }

Hope this helps.

Community
  • 1
  • 1
daxeh
  • 1,083
  • 8
  • 12
  • This would skip the empty case raising of error, correctly, but it is only a portion of the problem ;) – Masiorama Apr 27 '15 at 16:04
  • 1
    first steps in prevention, validations for easier debugging and error handling.. may help perhaps. – daxeh Apr 27 '15 at 16:29