How can I update custom fields using the BigCommerce API?

不问归期 提交于 2019-12-13 06:13:55

问题


I am looking to update the custom fields on a product.

I found in the guide on how to do it manually via the admin interface

The API docs suggest that you can't directly modify the custom fields on a product, only access them:

My next thought was to update the product

This is the existing custom_field on the product.

{
  "url"=> "https://storename.mybigcommerce.com/api/v2/products/32/customfields.json",
  "resource"=>"/products/32/customfields"
}

When I try to modify the url/resource and send the hash back to update, I am greeted with a 400 Bad Request :(

new_custom_fields = { 
"url" => "https://storename.mybigcommerce.com/api/v2/products/75/customfields.json", 
"resource" => "/products/75/customfields"
}

api.update_products(75, {"custom_fields" => new_custom_fields})
RuntimeError: Failed to parse Bigcommerce response: 400 Bad Request

Thoughts?


回答1:


this seems to be a bug in the Bigcommerce APIs. Currently only GET requests on the custom fields are supported.

http://developer.bigcommerce.com/api/products/customfields

That is probably the reason why you are hitting a 400.




回答2:


Not sure if this will help with Ruby, but it might help those using PHP... I'm able to create a custom field on a product using php. Just need the product id and the values for the custom field, "name" and "text".

$data_array = array('name' => 'gender', 'text' => 'male');
BigCommerce::createProductCustomField('17', $data_array);

I haven't tried updating a custom field, but if creating one works, then the following should also work to update a current custom field:

BigCommerce::updateProductCustomField($product_id, $id, $object);

You'll need the $product_id of the product you want to update, the $id of the custom field that you want to update, and $object should be an array like $data_array above.

More info on PHP client for BC: https://github.com/bigcommerce/bigcommerce-api-php

Good luck!




回答3:


Try this Code :

$headers = array(
"Content-type: application/json",
//"Authorization: Basic " . base64_encode($credentials)
);
$name='Bullet Point ';
$data_array = array('name'=>'Bullet Point','text'=>'Bullet Point value');
$body=json_encode($data_array);
//Get the current url and split it at the '?'
$ch = curl_init('https://www.abc.mybigcommerce.com/api/v2/products/1122/customfields.json'); //open connection
curl_setopt($ch, CURLOPT_TIMEOUT, 60); //set to 60 seconds from BC API Guide v1 PDF example
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //load all header data
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); //comment out this PUT line to change to a POST statement
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_USERPWD, "admin:api-key");
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec($ch); //execute post
curl_close($ch);



来源:https://stackoverflow.com/questions/16405477/how-can-i-update-custom-fields-using-the-bigcommerce-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!