Tell us what you are trying to do?
Build a content template to display Woocommerce order information.
I've written a shortcode that works perfectly in a View to display the order information for the current order in the loop:
/*** Shortcode to return any aspect of a Woocommerce order ***/
/* Accepts parameters 'id' and 'token':
/* 'id' = If ID is blank current loop item ID is used otherwise it needs to be a class ID
/* 'attribute' = The Woocommerce order array item wanted. Defaults to order ID */
function get_order_info($atts) {
//Set the default $atts values
$defaults = array(
'id' => get_the_ID(),
'attribute' => 'id'
);
//Apply default atts if none have been set
$atts = shortcode_atts( $defaults, $atts );
//Get the WC_Order object for the current order in the loop
$order = wc_get_order( $atts['id'] );
//Get the order data
$order_data = $order->get_data();
//Return whichever order data item is requested
return $order_data[$atts['attribute']];
}
add_shortcode("order_info", "get_order_info");
But trying to use this in a content template causes Guttenberg to display:
Updating failed. The response is not a valid JSON response.
And PHP throws this error:
Call to a member function get_data on bool
Both of these errors are the result of this line of code:
$order_data = $order->get_data();
Commenting this stops the errors, but obviously I need this for the shortcodes to run properly. As I understand it, the PHP error is saying that get_data() is returning a boolean, ie FALSE, which means that it's not getting the order ID correctly. The only problem there is that it is getting the order ID, and it's even working on the front end!
Now I know that WordPress shortcodes get a bit picky if they don't return HTML sometimes, so I stripped the shortcode back to this:
function get_order_info_html() {
$order = wc_get_order(2279);
$data = $order->get_data();
return "<span>" . $data['id'] . "</span>";
}
add_shortcode("order_info_html", "get_order_info_html");
Then it works fine. It also works fine if I pass the order ID in via a shortcode attribute. But the moment that I try to get the order ID dynamically using get_the_ID(), the errors are back. Dumping get_the_ID() shows that it is returning the correct value, but no matter where it is used the errors persist, but only on the back end. The front end works correctly, but with these errors I cannot effectively save the content template as reloading it will result in a white screen.
I've also tried using the Toolset Fields and Text block to add the post ID dynamically like this:
[order_info_html id=[wpv-post-id]]
But the same errors occur straight away, so it's something related to the dynamic post ID's for some reason.
I'm not sure if this error has something to do with scope or rendering order or something like that, but I've spent hours trying to figure it out and I can't get any further. I suspect it might be a WordPress / Guttenberg issue but it's working fine in the View but not the Content template, so I thought it was worth asking here.