Fatal Error When Creating Orders from WooCommerce Admin
-
When attempting to create an order from the WooCommerce backend (admin panel), the plugin throws a fatal error:
Fatal error: Uncaught Error: Call to a member function get() on null in
/includes/class-hc-wcma-checkout.php:288The
hc_wcma_get_session_or_post()method in class-hc-wcma-checkout.php (Line: ~288) attempts to accessWC()->session->get()without checking if the session object exists. In the WordPress admin context (when creating orders manually),WC()->sessionisnullbecause there is no active user session, causing the fatal error.The solutions is to add a null check before attempting to access the WooCommerce session object. The session should only be accessed if it exists (frontend checkout), otherwise fall back to POST data.
My proposed fixed is to replace the
hc_wcma_get_session_or_post()method with the following code:private static function hc_wcma_get_session_or_post( $session_key, $post_key ) {
if ( WC()->session ) {
$value = WC()->session->get( $session_key );
if ( ! empty( $value ) ) {
return $value;
}
}
// PHPCS:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified earlier.
return isset( $_POST[ $post_key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $post_key ] ) ) : '';
}The same null check should also be applied to the session clearing code in the
save_new_address_from_order()method (around line 360) to prevent similar issues
The topic ‘Fatal Error When Creating Orders from WooCommerce Admin’ is closed to new replies.