BNMetrics / Laravel-ShopifyAPI

Shopify Laravel API Wrapper ( with Billing support!)
MIT License
53 stars 30 forks source link

Creating or Modifying Error: {"metafields":"expected Hash to be a Array"} #5

Closed baberuth22 closed 6 years ago

baberuth22 commented 7 years ago

I can create or modify products, pages, articles, collections etc. But, when I try to pass metafields, I receive the error:

{"metafields":"expected Hash to be a Array"}

My metafields look like this in PHP:

[metafields] => Array
    (
        [0] => Array
            (
                [namespace] => atb
                [key] => sections
                [value] => test val
                [value_type] => string
            )

    )

Shopify expects:

"metafields": [
  {
    "key": "new",
    "value": "newvalue",
    "value_type": "string",
    "namespace": "global"
  }

They look identical. What is going wrong? I can't seem to get the correct format to post metafields. Have you had any luck with them?

BNMetrics commented 7 years ago

Hi there @baberuth22 , If you are trying to create a meta field, the keys must be strings instead of array, try doing this:

$metafield = [
       'metafield' => [
                'namespace' => your_name_space,
                'key' => your_key,
                'value' => your_val,
                'value_type' => 'string'
             ]
];

then call $this->api->createMetafields($metafield)

baberuth22 commented 7 years ago

Hi @BNMetrics,

Thanks for the response. Those keys are actually strings. I am not creating metafields individually, I am passing metafields in with the entity that is created or modified.

For example, here is a page in json rather than PHP:

json_encode($params):

{
    "page": {
        "title": "Simple Test",
        "shop_id": 12457089,
        "body_html": "",
        "author": "Eri Panky",
        "template_suffix": "",
        "metafields": [
            {
                "namespace": "atb",
                "key": "sections",
                "value": "test val",
                "value_type": "string"
            }
        ],
        "images": [],
        "variants": []
    }
}
$call = 'pages/239988233';
$this->api = Shopify::retrieve($shop_url, $password);
$this->api->modify($call, $params);

https://help.shopify.com/api/reference/product

BNMetrics commented 7 years ago

@baberuth22 If you are trying to modify an entity ( such as product ), and adding in metafield, try do this:

$data = [
         'id' => $your_product_id,
         'metafields' => [ 
                [
                 'namespace' => your_name_space,
                'key' => your_key,
                'value' => your_val,
                'value_type' => 'string'
                ]
          ]
]

As you can see, the metafields field is allowed to be an array of metafields instead of a single array for one metafield, in that case, even if you are passing in one metafield, it should be a two dimensional array.

pass it to your specific product by:

$this->api->modifyProducts($your_product_id, $data)
baberuth22 commented 7 years ago

@BNMetrics json_encode the array you just created. It is identical to mine. The inner array is converted to an object when you json_encode.

I don't believe the ID needs to be passed in because my updates and inserts work if I unset the metafields array key.

BNMetrics commented 7 years ago

@baberuth22 After digging around, this is apparently to do with Guzzle sending multi-dimension arrays in a bad format, https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/resolved-line_items-expected-hash-to-be-a-array-json-creating-order-402241 In the mean time, I will try and find a a way to resolve this.

baberuth22 commented 7 years ago

@BNMetrics That's what I was thinking. Any luck finding a resolution?

baberuth22 commented 7 years ago

I started looking at a different solution, but I'd really like to use this project.

@BNMetrics Have you looked into this issue more? I am trying to fix it but I don't want to have to fork guzzle.

BNMetrics commented 7 years ago

@baberuth22 Hi there, Sorry for the late response, I had been quite busy with my work. You are correct, I think we might need to make an extension for guzzle to have it work. I will try and see if I can do that soon. Another solution would be to use curl_setopt() to handle two dimensional arrays.

Either way, I will try to see if I can fix it. If you have any suggestions, I'm happy to see it. :)

roberttolton commented 7 years ago

I think I'm also running into this problem trying to do fulfillments.. any ideas on the fix?

roberttolton commented 6 years ago

@BNMetrics @baberuth22 Any luck guys?

roberttolton commented 6 years ago

Perhaps for these calls it might be best to work-around this package and use Guzzle or curl manually..? If there's no in-package solution?

RRStoyanov commented 6 years ago

@roberttolton @BNMetrics

check #10

BNMetrics commented 6 years ago

@baberuth22 @baberuth22 As of v1.0.4, this issue has been fixed! Thanks @RRStoyanov ! :)