How do i update a nested value via. the API? - api

How do i update a nested value via. the API? i want to update my custom field custom_product_fields_magento_product_id
Request path: /api/product/f9529c4f40e94fa6ae7439f97090cc9e
Request type: PATCH
Body:
{
"translated.customFields.custom_product_fields_magento_product_id" : "123"
}
Where am i going wrong? i can easily update product values that arent nested, like "productNumber" : "value"

You can use
{
"customFields": {
"custom_product_fields_magento_product_id":"123"
}
}
The value will be automatically saved to translation, the language of which is specified in the request header or in the default language.

Related

Vue: Setting Data by matching route query

I'm attempting to set data fields provided by an array based on the Vue Router query. For example, when someone lands on my website using example.com/?location=texas, I want to set the location data by an array.
An example the array:
locations {
{
slug: "texas",
tagline: "Welcome to Texas",
}, {
slug: "california",
tagline: "Welcome to California",
}
}
I know this should be done using a computed property, however I am unable to get anything functioning. I've tried simple tests like if (this.slug.location === "texas"), and I cannot get the location data to populate. I would also like to provide default data in case there are no route matches.
Any help is extremely appreciated!
Edit:
I can accomplish this in a very manual way. Right now, I'm setting the query in data by the following:
slug: this.$route.query.location
I can display specific text by doing something like:
h3(v-if="slug === 'texas'") This will show for texas
h3(v-else-if="slug === 'california'") This will show for California
h3(v-else) This is default
The issue with this approach is there are various elements I need to customize depending on the slug. Is there any way I can create an array, and move whichever array matches a key in an array to the data??
You should be able to access a query param using the following (link to Vue Router documentation):
this.$route.query.location
So based on what you listed I would do something like...
export default {
computed: {
displayBasedOnLocationQueryParam() {
switch(this.$route.query.location) {
case 'texas':
return 'Welcome to Texas'
default:
return 'hello there, generic person'
}
}
}
}
Note that I'm not using your array explicitly there. The switch statement can be the sole source of that logic, if need be.

Trello REST API: Delete Custom Field item from a Card

How do I delete a Custom Field item from a card, using Trello REST API?
The documentation only mentions updating, not removing an already set value.
The closest I'll get, is setting an empty value, like this:
{
"value": {
"text": ""
}
}
When I'll then fetch the card in JSON format (card url + .json at the end), it's still shows up among other customFieldItems, with an empty value.
If I manually clear the value using Trello in the browser, the custom field item is removed from the card, and no longer shows up in the json format.
How can I achieve the same thing, but using the REST API ?
According to the documentation, this can be done by setting an empty string for value.
i.e. instead of
{
"value": {
"text": ""
}
}
you want to send a request with
{
"value": ""
}
It took me a while to figure this out because the documentation is kind of confusing and has empty strings for everything else as well, but this one particular empty string is useful.

WooCommerce API - Is there a way to _delete_ meta values?

Is there a way to delete meta values through the WooCommerce API? We have wound up with duplicate meta values and we can't find a way to remove them.
You can send update with metadata key and without value to unset metadata.
So let's assume you want to update order metadata. You can do it by sending PUT request for your order:
PUT https://{yoursite.com}/wp-json/wc/v3/orders/{id}
Body:
{
"meta_data":[
{
"key":"{your_meta_key}",
"value":"{your_meta_value}"
}
]
}
Then if you want to remove this meta key you can send:
PUT https://{yoursite.com}/wp-json/wc/v3/orders/{id}
Body:
{
"meta_data":[
{
"key":"{your_meta_key}"
}
]
}

Setting custom field values on a card with the Trello API

I am trying to use the new Custom Fields methods of the Trello API to set the value of a custom field on a card.
I've created a custom field of type number. When I make a GET request for the custom field by it's id, it returns this custom field object:
{
"id":"5ab13cdb8acaabe576xxxxx",
"idModel":"54ccee71855b401132xxxxx",
"modelType":"board",
"fieldGroup":"837a643fd25acc835affc227xxxxxxxxxxxxxxxxxxxx",
"name":"Test Number Field",
"pos":16384,
"type":"number"
}
Then when I create a new card in the Trello UI (but do not type in a value in the Test Number Field box) and then GET that card using the customFieldItems=true (as documented here), it returns this card object (irrelevant fields removed):
{
"id": "5ab56e6b62abac194d9xxxxx",
"name": "test1",
"customFieldItems": []
}
Note that since I did not type anything into the Test Number Field box in the UI, the customFieldItems property contains an empty array.
Then if I type in the number 1 in the Test Number Field box in the UI and GET the card again, it returns this (irrelevant fields removed):
{
"id": "5ab56e6b62abac194d9xxxxx",
"name": "test1",
"customFieldItems":
[
{
"id": "5ab570c5b43ed17b2dxxxxx",
"value": {
"number": "1"
},
"idCustomField": "5ab13cdb8acaabe5764xxxxx",
"idModel": "5ab56e6b62abac194d9xxxxx",
"modelType": "card"
}
]
}
I want to be able to set the value of this custom field via API.
When I go to the API documentation for "Setting, updating, and removing the value for a Custom Field on a card," (here) I plug in the following information:
Query Auth
key: (our valid/working Trello API key)
token: (our valid/working Trello API token)
PATH PARAMS
idCard: (ID of the card that the Custom Field value should be set/updated for) 5ab56e6b62abac194d9xxxxx
idCustomField (ID of the Custom Field on the card.): 5ab570c5b43ed17b2dxxxxx
QUERY PARAMS
idCustomField (ID of the Custom Field to which the item belongs.): 5ab13cdb8acaabe576xxxxx
modelType (This should always be card.): card
value (An object containing the key and value to set for the card's Custom Field value. The key used to set the value should match the type of Custom Field defined.): {"number": 2}
When I click Try It, I get the response: 400 Bad Request "Invalid custom field item value."
I've tried the following things:
Switching the two idCustomField values (it's confusing that both the path parameter and query parameter have the same name, implying that they are meant to accept the same value, but then they have different descriptions, and the descriptions are vague/confusing).
Setting both idCustomField values to the same thing (for both of the possible IDs)
Setting the value to 2, {"number": "2"}, {number: 2}, {number: "2"} and more.
No matter what I try, I always get "Invalid custom field item value." This behaves the same way whether the card has a value in the custom field or not.
I'm pretty sure that the idCustomField in the path params is being accepted, because when I change one character, it gives me this error instead: "invalid value for idCustomField".
So I don't know if the "Invalid custom field item value." is referring to the query param idCustomField* or the value.
I also don't know if it makes a difference whether or not the card has an existing value in the custom field, but I want to be able to set the value of this custom field regardless of whether or not it currently has a value in the field.
The live example (using XMLHttpRequest) on the Trello documentation page is wrong. You should use the next example using fetch.
var url = "https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?token={yourToken}&key={yourKey}";
var data = {value: { number: "42" }};
fetch(url, { body: JSON.stringify(data), method: 'PUT', headers: {'content-type': 'application/json'}})
.then((resp) => resp.json())
.then((data) => console.log(JSON.stringify(data, null, 2)))
.catch((err) => console.log(JSON.stringify(err, null, 2)))
This example works. And after trying this, I modified the XMLHttpRequest version and it worked too.
var data = null;
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
data = {value: { number: "3" }}; //added
var json = JSON.stringify(data); //added
xhr.open("PUT", 'https://api.trello.com/1/cards/{idCard}/customField/{idCustomField}/item?key={yourkey}&token={yourtoken}');
xhr.setRequestHeader('Content-type','application/json'); //added
xhr.send(json); //modified
The point is that you should 1) set the request Content-type header to application/json and 2) pass the value via JSON object body.
I tried to edit the live example on the documentation but I it was not possible. I hope they will fix it soon.

WooCommerce REST API Custom Fields

Is it possible to access custom fields for orders, products, customers via WooCommerce REST API? If not natively, then what plugins or workarounds or hacks are out there that work? Thanks!
Answering my own question:
It is possible using the following: (using v3 legacy API)
To send custom fields back to the server:
(For Orders)
{
"order_meta": {
"key": "value"
}
}
To retrieve custom fields from server use this filter with your end point:
http://www.example.com/wc-api/v3/orders?filter[meta]=true
This works for Products as well.
As mentioned in the comment after WooCommerce creates an order via the API it will fire woocommerce_api_create_order hook, you can make use of it.
Add the following code to your theme's functions.php file
add_action( 'woocommerce_api_create_order', 'my_woocommerce_api_create_order', 10, 2);
function my_woocommerce_api_create_order( $order_id, $data ) {
// $data contains the data was posted, add code to extract the required
// fields and process it as required
}
Similarly look at the code in plugins/woocommerce/includes/api/*.php files, find the suitable action or filter hook for the end point and use it.
SIMPLE SOLUTION THAT WORKED FOR ME (using REST API REQUEST):
URL: https:///wp-json/wc/v3/orders/1234
METHOD: PUT
BODY:
{
"status": "completed",
"meta_data": [{
"key": "is_web_server_handled",
"value": "1"
}]
}