1
votes

I am working on a Woocommerce shipping plugin where I have a class method with the following that is triggered on a custom do_action

try {
    $client = new SoapClient( plugin_dir_path( dirname( __FILE__ ) ) . 'includes/wsdl/Test.wsdl', ['trace' => true, 'exceptions' => true ]  );
    $results = $client->__soapCall('CreateSomml', ['parameters' =>  ['Details' => $bill->__toArray() ]]);
} catch (SoapFault $e) {
    wc_add_wp_error_notices(new WP_Error('soap', $e->getMessage(), isset($client)? $client->__getLastRequest() : $bill->__toArray() ));
    // print $client->__getLastRequest();
    // throw $e;
}

which tries to connect to a web service and would catch any SoapFault and add an error notice but on running the code I run into an error

Fatal error: Call to a member function get() on null in ...../wp-content/plugins/woocommerce/includes/wc-notice-functions.php on line 80

which based on the documentation leads to

function wc_add_notice( $message, $notice_type = 'success' ) {
    if ( ! did_action( 'woocommerce_init' ) ) {
        wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
        return;
    }

    $notices = WC()->session->get( 'wc_notices', array() );

    // Backward compatibility
    if ( 'success' === $notice_type ) {
        $message = apply_filters( 'woocommerce_add_message', $message );
    }

    $notices[ $notice_type ][] = apply_filters( 'woocommerce_add_' . $notice_type, $message );

    WC()->session->set( 'wc_notices', $notices );
}

Line 80 is return

I assume it could be

WC()->session->set( 'wc_notices', $notices ); But not sure why the error.

The action is being triggered from the backend admin management. hat could be the problem?

I'm running PHP 5.6.27 with Wordpress 4.8 and Woocommerce 3.2.1

UPDATE:

been trying to find the error and all I can come up with was

Fatal error: Uncaught Error: Call to a member function get() on null in /var/www/wordpress/wp-content/plugins/woocommerce/includes/wc-notice-functions.php:80 Stack trace: #0 /var/www/wordpress/wp-content/plugins/woocommerce/includes/wc-notice-functions.php(198): wc_add_notice('Invalid User Cr...', 'error') #1 /var/www/wordpress/wp-content/plugins/wc-trinicargo-shipping/includes/class-wc-trinicargo-shipping-create-waybill.php(80): wc_add_wp_error_notices(Object(WP_Error)) #2 /var/www/wordpress/wp-includes/class-wp-hook.php(296): Wc_Trinicargo_Shipping_Create_Waybill->create() #3 /var/www/wordpress/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters('', Array) #4 /var/www/wordpress/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #5 /var/www/wordpress/wp-content/plugins/wc-trinicargo-shipping/includes/class-wc-trinicargo-shipping-create-waybill.php(68): do_action('wc_trinicargo_c...') #6 /var/www/wordpress/wp-content/plugins/wc-trinicargo-shipping/admin/partials/wc-trinicargo-shipping-init-methods.php(163): Wc_ in /var/www/wordpress/wp-content/plugins/woocommerce/includes/wc-notice-functions.php on line 80

Why is the session not starting? how do I start it?

1
Which do_action are your hooking into? The error suggests that the woocommerce session is not started yet. - Mikk3lRo
@Mikk3lRo it is a custom action I created specifically for this task. How does one "start a session" to fulfil this requirement? - KArneaud
How does it get called, then? It must be triggered from some "native" wordpress hook if you trace back, right? And line 80 in 3.2.1 (newest) is not return, nor set(). It's the get() - so if you have anything else on line 80 you either have a broken update or you've been "playing around" in the code yourself... don't do that :p - Mikk3lRo
@Mikk3lRo haven't been playing around with woocommerce its out of the box. the native hook is probably woocommerce_order_status_processing which in turn class another class method that instantiates and calls the class in question method. how does one establish a WC()->session? - KArneaud
@Kendall did you ever get an answer to this? Having precisely the same issue. - Randy Hall

1 Answers

9
votes

So for anybody looking for a solution to this one - the issue is that the WC()->session isn't yet available here. I ran into the issue trying to add a notice inside of an 'admin_post' action. Initializing an empty session worked for me here:

WC()->session = new WC_Session_Handler();
WC()->session->init();

After those two lines I was able to successfully use wc_add_notice. Hope that helps somebody.