I want to unset some WooCommerce session variables because on page load the hooks I'm using haven't fired which leaves previous session data populating the checkout shipping table.
Within any functions accessed from the hook callbacks, I can access WooCommerce session vars like:
WC()->session->get('foo');
However, when outside of those callbacks, WC
is undefined. I tried setting:
global $woocommerce;
and accessing via $woocommerce->session->__unset('foo');
but got the notice:
Trying to get property of non-object
and the error:
Fatal error: Uncaught Error: Call to a member function __unset() on null
So, clearly this $woocommerce
global is not the same as WC
(which I guess is exposed by the hooks?)
I think I can unset these variables using one of the first hooks that will fire on the checkout page, but it got me wondering:
What is the difference between WC()
and $woocommerce
? Aren't they both ways to access WooCommerce methods?