Hope you're doing great! I am trying to print the WC()->session->get inside a shortcode but getting error , bellow is my code and error

function set_session(){

    global $woocommerce;
    WC()->session->set( 'some_data_get' ,' some_data_get' );    

add_action( 'init', 'set_session' );

function show_session(){

   global $woocommerce;
   var_dump(  WC()->session->get( 'some_data_get' ) );

add_shortcode( 'show_session', 'show_session' );


here is error

Fatal error: Uncaught Error: Call to a member function get() on null in E:\OFFICE-SERVER\htdocs\aspw\wp-content\themes\twentynineteen\functions.php:335 
  • 1
    Well, if you call do_shortcode('[show_session]'); just like that (i.e. not from a proper hook), then that would cause the error. Have you actually tried adding [show_session] manually to the post content?
    – Sally CJ
    Jul 27 '19 at 16:23
  • 1
    thanks ! you right Jul 27 '19 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.