Update InventoryLevels with PHP

Explorer
83 0 23

Hi there fellow devs!

(I appologize if this posts twice, the first one didn't show up in more than 15 minutes so I posted again)

I've been having some issues trying to get an updated iventory with PHP and need a point in the right direction, any help is greatly appreciated!

The code below doesn't produce any errors/responses so I'm not sure what I'm doing wrong. The code below works for updating products, however inventory levels seem to be much more difficult.

$inv_data = array(
          'inventorylevel' => array(
                             'inventory_item_id' => $post['inventory_id'], 
                             'location_id' => xxxx,                                      
                             'available_adjustment' => -$post['variant_qty'])
           );
$inv_url = "https://$api_key:$api_pass@xxx.myshopify.com/admin/inventory_levels/adjust.json";
$inv_curl = curl_init($inv_url);
curl_setopt($inv_curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($inv_curl, CURLOPT_POSTFIELDS, http_build_query($inv_data));
curl_setopt($inv_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($inv_curl, CURLOPT_HEADER, true);
curl_setopt($inv_curl, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
$inv_result = curl_exec ($inv_curl);       
curl_close($inv_curl);

$inv_result = json_decode($inv_result);
print_r($inv_result);

Thank you in advance!

0 Likes
Highlighted
Shopify Partner
27 0 2

Hi Nick,

Few things:

1. The posted data should not be a multidimensional array according to the docucmentation. You can remove the first array "inventorylevel". See https://help.shopify.com/en/api/reference/inventory/inventorylevel POST request.

2. Can you confirm that you are sending the correct location_id ? Because in the documentation it is clearly mentioned that "Adjusting inventory levels at a non-existent location fails and returns an error".

Use https://help.shopify.com/en/api/reference/inventory/location this reference to get your location id.

If you think you have done above correctly, please also refer another point on the documentation "Adjusting inventory levels for an inventory item that is untracked fails and returns an error".

Please verify above, let us know if you fixed it.

Best,

Navin

1 Like
New Member
3 0 0

Hi Navin,

Thank you for your reply!

I also did not think that first array should be there and while testing with it gone I still got the same results (no response or errors at all). I did confirm that the location ID is correct, we use it in a similar Python application and it works just fine there. I'm not sure why I'm not receiving an error or any kind of response at all, even when the data is changed to something entirely different that should produce an error. It's almost like it's not even running the curl. The (almost) exact same code below is being used to hide a product and works just fine but when changed for inventory I get nothing...

$data = array(
            'product' => array(
                               'id' => $post['product_id'],
                                'published_at' => '')
          );
$url = "https://$api_key:$api_pass@xxx.myshopify.com/admin/products/$product_id.json";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);       
curl_close($curl);

$result = json_decode($result);
print_r($result);

Thanks again for your response to this issue!

Best regards,

Nick

0 Likes
Explorer
83 0 23

Hi Navin,

Thank you for your reply!

I also did not think that first array should be there and while testing with it gone I still got the same results (no response or errors at all). I did confirm that the location ID is correct, we use it in a similar Python application and it works just fine there. I'm not sure why I'm not receiving an error or any kind of response at all, even when the data is changed to something entirely different that should produce an error. It's almost like it's not even running the curl. The (almost) exact same code below is being used to hide a product and works just fine but when changed for inventory levels I get nothing...

$data = array(
            'product' => array(
                               'id' => $post['product_id'],
                                'published_at' => '')
          );
$url = "https://$api_key:$api_pass@xxx.myshopify.com/admin/products/$product_id.json";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);       
curl_close($curl);

$result = json_decode($result);
print_r($result);

Thanks again for your response to this issue!

Best regards,

Nick

0 Likes
Explorer
83 0 23

Thanks again Navin for your response, I have fixed the issue. It looks like "PUT" doesn't update the data. After changing the method to "POST" it worked perfectly.

Edited Code:

$inv_data = array(
	            'inventory_item_id' => $post['inventory_id'], 
	            'location_id' => xxxx,
	            'available_adjustment' => -$post['variant_qty']
            );
$inv_url = "https://$api_key:$api_pass@xxx.myshopify.com/admin/inventory_levels/adjust.json";
$inv_curl = curl_init($inv_url);
curl_setopt($inv_curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($inv_curl, CURLOPT_POSTFIELDS, http_build_query($inv_data));
curl_setopt($inv_curl, CURLOPT_RETURNTRANSFER, 1);
$inv_result = curl_exec ($inv_curl);       
curl_close($inv_curl);

$inv_result = json_decode($inv_result);
print_r($inv_result);

 

0 Likes