Reputation: 5245
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 );
}
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?
Upvotes: 2
Views: 5228
Reputation: 776
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.
Upvotes: 13