Skip to content
Advertisement

How to display a $_SESSION variable in custom woocommerce checkout field?

I am able to verify by viewing the “Application” tab in the dev tools that my session variable has carried over to the checkout page; and I have the WC hook setup that allows me to change the “default”/value of the field I want to insert data into.

However, I am unable to get them to read each other.

JavaScript

As you can see from the attached image, my session variable is present on that page. How now, do I get it to add that session variable to $fields[‘order’][‘order_comments’][‘default’]?

Any help is appreciated! Thanks.

enter image description here

Advertisement

Answer

Use session_start(); before accessing the $_SESSION variable:

JavaScript

Alternatively you can use jQuery with sessionStorage:

JavaScript

The code has been tested and works. Add it to your active theme’s functions.php.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement