Skip Navigation

[Fermé] Calling Woocommerce get_data() From Content Template Causes Error

This support ticket is created Il y a 7 mois et 3 semaines. There's a good chance that you are reading advice that it now obsolete.

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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 8 réponses, has 2 voix.

Last updated by Shane Il y a 7 mois et 3 semaines.

Assigned support staff: Shane.

Auteur
Publications
#2380323

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.

#2380409

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dave,

Can we try to test that the correct ID is being passed.

Use this below.

	
[order_info_html id="[wpv-post-id]"]

Note the addition of the qoutes.

Secondly add this to your shortcode.

var_dump($defaults['id']);

Once you've done this please let me know if your shortcode is displaying the correct post Id for your product.

Thanks,
Shane

#2380657
Screenshot (45).png
Screenshot (44).png

Hi Shane,

I think the tests you mention I basically covered originally, but just to be sure I've done as you asked as I definitely didn't use the double-quotes.

[order_info_html id="[wpv-post-id]"]

This acted exactly the same as all other attempts at using a dynamic post ID and caused the same errors (see Screenshot 44.png).

print_r("Requested Output Test = ");
var_dump( $defaults['id'] );

Output Requested Output Test = int(2279), as expected (see Screenshot 45.png).

So unfortuntely this leaves us exactly where we were with any number passed directly working and any dynamic number causing the errors. Hopefully this answers your initial questions.

#2380681

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dave,

Would you mind allowing me to have admin access to the website so that I can have a more detailed look at this for you ?

Please where applicable please provide me with a link to an example page where I can see the issue.

I've enabled the private fields for your next response.

I would like to look at the code and go through it line by line to see if I can identify why its failing.

Thanks,
Shane

#2380695

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dave,

Though the message is appearing on the screen regarding the JSON error I can see that the content template does get updated correctly.
hidden link

As you can see i've made a few changes to the content template and they are being reflected.

Thanks,
Shane

#2380697

Hi Shane,

I did mention in my original post that the Content Template does get updated, but if you refresh the editor or try to reopen it, it will white screen on you because of the JSON error. The only way to get it back is to disable the shortcode in the snippet and then it will reload correctly.

This is why I've gotten stuck with this, it is clear the error is only affecting the editor, but it's affecting it in a critical way, even if the front end is ok.

#2381423

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dave,

This has not been my experience thus far. What happens for me is that the content template is loading correctly as well as saving without any JSON errors.

Can you double check the template to see if it still occurs cause on my end the content template saves as intended with the updates i've made.
hidden link

Thanks
Shane

#2382211

I've just tested it again and it is reloading fine now. The only difference is that previously I was working on a Chrome OS machine, and now I'm on a Windows 10 one as I can't get to the Chrome OS one at the moment. I will retest this on the Chrome OS setup when I can, but Monday at the latest.

It's still really weird if it is that as I'm using the Chrome browser on Windows 10.

Regardless, yes it is updating, but there's no way it should be throwing a JSON and PHP error still because the code clearly works fine, and I'm worried about the longevity of the project if it's going to have errors present in it.

#2382339

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Dave,

Unfortunately I cannot say why this function is throwing the error.

Given that its a function that Woocommerce uses there isn't much I can do in terms of debugging. Given that it saves and the function is set to only run on the frontend then I wouldn't worry too much.

But then you can contact the Woocommerce support team to see if they can shed some light on this ones as to why the function throws an error when using it in a content template.

Thanks,
Shane

Le sujet ‘[Fermé] Calling Woocommerce get_data() From Content Template Causes Error’ est fermé à de nouvelles réponses.