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

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}"
}
]
}

Related

How to store tags in two separate collections that are in many-to-many relationship?

A user is allowed to create items, optionally add up to 10 tags. There's going to be a tag cloud on one of the pages, so storing them in a separate collection seems justified.
The Tag schema is modeled as following:
module.exports = mongoose.model("Tag", new mongoose.Schema({
label: {
type: String,
required: true,
lowercase: true,
maxLength: 35
}
}));
The Item schema has a tags key which references documents in Tag:
tags: {
type: [{ type: ObjectId, ref: "Tag" }],
validate: {
validator: n => n <= 10
}
},
Now, it would be easier to just store tags as subdocuments inside each item but as I said, I'll have a tag cloud later. Reading them from the items collection just to render the tag cloud will probably be a bit expensive, so duplicating them seems almost necessary and the lesser of the two devils. Please correct me if I'm wrong.
Anyway, the main question is: how do I add tags in this scenario then? The request content will be a JSON object, sent to /api/collections/items with the POST method. How can I make sure tags are added to both collections and the limit is respected? Because as it stands, the validator for tags in Item only checks the number of references stored, not the amount of tags assigned to an item. Am I wrong?
Ideally, the controller for adding items should be able to process such requests in a single operation:
async function addItem(req, res) {
try {
let item = await Item.create(req.body);
res.status(201).json(item);
} catch (err) {
res.status(400).json({ response: err.message });
}
}
Or am I going to have to first add tags and do something like item.tags.push(tags) afterwards?

How do i update a nested value via. the 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.

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 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"
}]
}

Retrieve the traffic variable in RankedReport of Adobe Sitecatalyst

Is it possible to fetch a traffic variable sProp1 along with PageName and Url when issuing a GetReport request of a Ranked report?
Let us say I have the following passed to SiteCatalyst (Omniture) via Java Script.
s.pageName = o_title; // Page Name
s.channel = o_structure; // SiteSection
s.prop1 = o_inode // Traffic variable also the Primary Key Id of the PageName
Now, if I run a RankedReport, I will get the following
PageName
PageViews
Url
Along with this info, I will also be interested in fetching the s.prop1 value. I will need the s.prop1 value to use it on my db to query the first sentence of the article from db (and other metadata) and show the results on the results page that shows the most popular pages. Can this be achieved? I mean is it possible to get the traffic variable associated with the pageName?
Thanks,
Rag
OK. I think I if I add another element (prop1) I get the breakdown. {
"reportDescription":{
"reportSuiteID":"*",
"dateFrom":"2013-03-06",
"dateTo":"2013-03-15",
"metrics":[
{
"id":"pageviews",
"name":"Page Views",
"type":"number"
}
],
"sortBy":"pageviews",
"elements":[
{
"id":"siteSection"
},
{
"id":"page"
},
{
"id":"prop1"
}
],
"locale":"en_US"
},
"validate":"false"
}