Support Forum
Support › MB REST API › Getting Fatal Error on psoting/updating Posts via Rest APIResolved
Uploading a json file to WC-RestApi/v3 produces an error:
[16-Nov-2023 18:47:14 UTC] PHP Fatal error: Uncaught TypeError: MetaBox\RestApi\Post::update(): Argument #2 ($post) must be of type WP_Post, WC_Product_Simple given, called in /home/hannl/public_html/wp-includes/rest-api/endpoints/class-wp-rest-controller.php on line 474 and defined in /home/hannl/public_html/wp-content/plugins/mb-rest-api/src/Post.php:7
Stack trace:
#0 /home/hannl/public_html/wp-includes/rest-api/endpoints/class-wp-rest-controller.php(474): MetaBox\RestApi\Post->update()
#1 /home/hannl/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()
#2 /home/hannl/public_html/wp-includes/rest-api/class-wp-rest-server.php(1193): WC_REST_CRUD_Controller->update_item()
#3 /home/hannl/public_html/wp-includes/rest-api/class-wp-rest-server.php(1041): WP_REST_Server->respond_to_request()
#4 /home/hannl/public_html/wp-includes/rest-api/class-wp-rest-server.php(431): WP_REST_Server->dispatch()
#5 /home/hannl/public_html/wp-includes/rest-api.php(424): WP_REST_Server->serve_request()
#6 /home/hannl/public_html/wp-includes/class-wp-hook.php(324): rest_api_loaded()
#7 /home/hannl/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#8 /home/hannl/public_html/wp-includes/plugin.php(565): WP_Hook->do_action()
#9 /home/hannl/public_html/wp-includes/class-wp.php(418): do_action_ref_array()
#10 /home/hannl/public_html/wp-includes/class-wp.php(813): WP->parse_request()
#11 /home/hannl/public_html/wp-includes/functions.php(1336): WP->main()
#12 /home/hannl/public_html/wp-blog-header.php(16): wp()
#13 /home/hannl/public_html/index.php(17): require('...')
#14 {main}
thrown in /home/hannl/public_html/wp-content/plugins/mb-rest-api/src/Post.php on line 7
Hello Michael,
MB Rest API works with WordPress Rest API v2 or v1, it does not work with WooCommerce Rest API v3. Please follow the documentation https://docs.metabox.io/extensions/mb-rest-api/
Hello Peter,
thank you for clarification.
But this was working before. Why can't I update my products any more?
I need MetaBox mostly to add custom fields to products which will be updated by json I get from another database.
Is there a workaround?
Hello,
Maybe it's a compatibility issue between MB Rest API and WooCommerce. According to the error message, the object WC_Product_Simple is not considered as a WP_Post. Please share some screenshots or a screen record when you update your product via Rest API so I can understand steps you do on your site.
I started this project in October last year and I could post/update the woo commerce simple product without any problem. In summer, I got an error 500 but the fields were still filled with the correct data.
Now, I get the error and the fields don’t contain any value.
Here is a sample json I send to the Rest API:
<div> POST https://hannl.bw-works.com/wp-json/wc/v3/products/1477
"meta_box": {
"relatedbook": [],
"relatedlist": "",
"herobooks": [],
"lesetipp": [],
"rel_products": [],
"untertitel": "Metatesti",
"format": "24 x 17",
"einband": "Softcover",
"illus": "mit zahlreichen Fotos",
"umfang": "384",
"autor": "Mark Andrews",
"autor_2": "Michael Bergmeister",
"autor_3": "Andreas Leibetseder",
"translator": "Kirsten Borchardt",
"translator_2": "",
"translator_3": "",
"isbn": "9783854457350",
"preis_at": "27",
"preis_ch": "",
"pubdate": "20221121",
"zitat": "Unterhält jeden, in dessen Vergangenheit eine Flasche schwarzer Farbe herumlungert",
"zitatquelle": "Victoria Segal, Mojo",
"zitat_2": "Das Buch gibt einen genauen Einblick, warum Eldritch zu einem der schwierigsten Menschen wurde, mit denen man in der Branche arbeiten kann …",
"zitatquelle_2": "on-magazine.co.uk",
"zitat_3": "",
"zitatquelle_3": "",
"ebook": "1",
"ebook_isbn": "9783854450000",
"ebookpreis": "12",
"version": "",
"uebersetzter-und-buecher_from": [],
"autor-und-buecher_to": [],
"3dcover": {
"width": 150,
"height": 150,
"file": "2022\/10\/som3d.jpg",
"filesize": 34023,
"sizes": {
"medium": {
"file": "som3d-238x300.jpg",
"width": 238,
"height": 300,
"mime-type": "image\/jpeg",
"filesize": 18220,
"url": "https:\/\/hannl.bw-works.com\/wp-content\/uploads\/2022\/10\/som3d-238x300.jpg"
}
</div>
This is the response in Insomnia:
<div><styletype="text/css">.wp-die-message{
display: none;
}</style><linkrel="stylesheet"href="https://hannl.bw-works.com/wp-content/plugins/query-monitor/assets/query-monitor.css?ver=3.15.0"media="all"/><divdata-qm="qm"><divid="qm-fatal"data-qm-message="Uncaught Error: MetaBox\RestApi\Post::update(): Argument #2 ($post) must be of type WP_Post, WC_Product_Simple given, called in /home/hannl/public_html/wp-includes/rest-api/endpoints/class-wp-rest-controller.php on line 474"data-qm-file="wp-content/plugins/mb-rest-api/src/Post.php"data-qm-line="7"><divclass="qm-fatal-wrap"><p><b>Fatalerror</b>: UncaughtError: MetaBox\RestApi\Post: : update(): Argument#2($post)mustbeoftypeWP_Post,
WC_Product_Simplegiven,
calledin/home/hannl/public_html/wp-includes/rest-api/endpoints/class-wp-rest-controller.phponline474<br>in<b>/home/hannl/public_html/wp-content/plugins/mb-rest-api/src/Post.php</b>online<b>7</b></p><p>Callstack: </p><ol><li><code>MetaBox\RestApi\Post: : update()</code><br><spanclass="qm-info qm-supplemental">wp-includes/rest-api/endpoints/class-wp-rest-controller.php: 474</span></li><li><code>WP_REST_Controller: : update_additional_fields_for_object()</code><br><spanclass="qm-info qm-supplemental">wp-content/plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-crud-controller.php: 245</span></li><li><code>WC_REST_CRUD_Controller: : update_item()</code><br><spanclass="qm-info qm-supplemental">wp-includes/rest-api/class-wp-rest-server.php: 1193</span></li><li><code>WP_REST_Server: : respond_to_request()</code><br><spanclass="qm-info qm-supplemental">wp-includes/rest-api/class-wp-rest-server.php: 1041</span></li><li><code>WP_REST_Server: : dispatch()</code><br><spanclass="qm-info qm-supplemental">wp-includes/rest-api/class-wp-rest-server.php: 431</span></li><li><code>WP_REST_Server: : serve_request()</code><br><spanclass="qm-info qm-supplemental">wp-includes/rest-api.php: 424</span></li><li><code>rest_api_loaded()</code><br><spanclass="qm-info qm-supplemental">wp-includes/class-wp-hook.php: 324</span></li><li><code>WP_Hook: : apply_filters()</code><br><spanclass="qm-info qm-supplemental">wp-includes/class-wp-hook.php: 348</span></li><li><code>WP_Hook: : do_action()</code><br><spanclass="qm-info qm-supplemental">wp-includes/plugin.php: 565</span></li><li><code>do_action_ref_array()</code><br><spanclass="qm-info qm-supplemental">wp-includes/class-wp.php: 418</span></li><li><code>WP: : parse_request()</code><br><spanclass="qm-info qm-supplemental">wp-includes/class-wp.php: 813</span></li><li><code>WP: : main()</code><br><spanclass="qm-info qm-supplemental">wp-includes/functions.php: 1336</span></li><li><code>wp()</code><br><spanclass="qm-info qm-supplemental">wp-blog-header.php: 16</span></li><li><code>require()</code><br><spanclass="qm-info qm-supplemental">index.php: 17</span></li></ol></div><h2>QueryMonitor</h2></div></div>{
"code": "internal_server_error",
"message": "<p>Es gab einen kritischen Fehler auf deiner Website.<\/p><p><a href="https:\/\/wordpress.org\/documentation\/article\/faq-troubleshooting\/">Erfahre mehr über die Problembehandlung in WordPress.<\/a><\/p>",
"data": {
"status": 500
},
"additional_errors": []
}
</div>
Hello,
If you update the Meta Box custom fields via Rest API only, please use the Rest API v2 as noted in the documentation https://docs.metabox.io/extensions/mb-rest-api/
POST https://hannl.bw-works.com/wp-json/wp/v2/product/1477
For other/standard data, you can use the Rest API v3 of WooCommerce to update data.
Thank you, that was the problem.