Skip Navigation

[Resolved] Exposing custom taxonomies in WC REST API and the updating throught the REST API

This thread is resolved. Here is a description of the problem and solution.

Problem:

I'm trying to expose / add custom taxonomies to the WC REST API, then updating said taxonomy through the WC REST API.

Solution:

It needs custom PHP codes, for example:

https://toolset.com/forums/topic/exposing-custom-taxonomies-in-wc-rest-api-and-the-updating-throught-the-rest-api/#post-2277189

Relevant Documentation:

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 12 replies, has 2 voices.

Last updated by Diego Walter Ricciardi 10 months ago.

Assigned support staff: Luo Yang.

Author
Posts
#2272513

Tell us what you are trying to do?

I'm trying to expose / add custom taxonomies to the WC REST API, then updating said taxonomy through the WC REST API.

Is there any documentation that you are following?

Not really, using php code to expose the taxonomy and then my own JS dev for updating products through the WC REST API.
The taxonomy appears well exposed when using GET to the API, as follows:

"marca": [
{
"id": 67,
"name": "Frasle",
"slug": "frasle"
}
]

Correctly sends, ID, name and slug, don't need anything else, not even slug actually but whatever.

When I try to update the custom taxonomy I get the following log:

Exception: Request failed for hidden link returned code 500. Truncated server response: {"code":"internal_server_error","message":"<p>Ha habido un error cr\u00edtico en esta web.<\/p>

Basically a 500 critical error.

Already having admin user+pw from another ticket, I'm sure you can get testing API keys.
It can be tested with Insomnia / Postman.

Thank you!

#2273625

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hello,

There should be some PHP errors in your website, please try theese:
1) In case it is a compatibility problem, please deactivate all other plugins, and switch to WordPress default theme 2021, deactivate all custom PHP/JS code snippets, and test again

2) Also check if there is any PHP error in your website:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/
section "PHP Debugging"

#2274087

It appears the issue is with the php code that is exposing the custom taxonomy to the rest API.

2022-01-24T14:00:38+00:00 CRITICAL Uncaught Error: Call to undefined method WC_Product_Simple::ID() in /home/crudricc/public_html/wp-content/themes/astra-child/functions.php:95
Stack trace:
#0 /home/crudricc/public_html/wp-includes/rest-api/endpoints/class-wp-rest-controller.php(461): product_update_callback(Array, Object(WC_Product_Simple), 'marca', Object(WP_REST_Request), 'product')
#1 /home/crudricc/public_html/wp-content/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-crud-controller.php(245): WP_REST_Controller->update_additional_fields_for_object(Object(WC_Product_Simple), Object(WP_REST_Request))
#2 /home/crudricc/public_html/wp-content/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-controller.php(269): WC_REST_CRUD_Controller->update_item(Object(WP_REST_Request))
#3 /home/crudricc/public_html/wp-includes/rest-api/class-wp-rest-server.php(1140): WC_REST_Controller->batch_items(Object(WP_REST_Request))
#4 /home/crudricc/public_html/wp-includes/rest-api/class-wp-rest-server.php(987): WP_ en /home/crudricc/public_html/wp-content/themes/astra-child/functions.php en la línea 95

The code itselft:

https://pastebin.com/ps53zZ9H

To be prices, since it calls line 95, it would be specifically:

$postId = $post->ID();

Not sure why but apparently the post id is undefined in a way?

#2274595

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

It is a custom PHP codes problem, please try to modify this line from:
$postId = $post->ID();

To:
$postId = $post[ 'id' ];

And test again

#2275181
critical_error.png

I put the modificaction, still shows, but still throws a 500 critical error.
I wonder if it could be related to the fact Toolset in general throws some critical error, when for instance I try to use the customizer

#2275361

Had nothing to do with that, that was due to Elementor 3.5.x, a rollback made the customizer work fine.

#2275857

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for sharing the problem source, since it is Elementor 3.5.x issue, which is out the range of Toolset support, please check it with Elementor support, and close this thread, thanks

#2276233

No, that isn't the issue. Elementor made the customizer work, I was just spitballing what could be the issue.
The expose of the taxonomy still doens't work when trying to use the REST API, doubt it has anything to do with Elementor.

#2277189

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Since it is a custom PHP codes problem, I suggest you try to fix your custom PHP codes by following WP document and Woocommerce document:
https://developer.wordpress.org/reference/functions/register_rest_field/
hidden link

And I have searched it in google, found some related threads, for example:
https://stackoverflow.com/questions/62664811/add-custom-taxonomy-term-in-woocommerce-rest-api

If you still need assistance for it, please provide a test site with the same problem, fill below private message box with login details, I need a live website to test and debug

#2278195

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

I have tried the credentials you provide above, but get below error message:
Error: la contraseña que has introducido para el nombre de usuario toolsetHelp no es correcta.

Please check it, make sure it is an valid admin account, thanks

#2278483

Sorry, it autocompleted wrongly, the password is updated.

#2279637

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

It is a Woocommerce issue, you can follow the thread I mentioned above to modify your custom PHP codes.

I have done below modifications in your website, replace this line from:
$postId = $post[ 'id' ];

With:
$postId = $post->get_id();

Please test again, check if it is fixed.

#2280105

My issue is resolved now. Thank you!