vippsas/vipps-woocommerce

Fatal error when WooCommerce Gateway Stripe plugin is also active - PHP 8.2+

knutsp opened this issue · 7 comments

`[07-Jun-2024 09:30:33 UTC] PHP Fatal error: Uncaught Error: Call to a member function get() on null in /wp-content/plugins/woo-vipps/VippsCheckout.class.php:808

Stack trace:
#0 /wp-includes/class-wp-hook.php(324): VippsCheckout->{closure}(Array)
#1 /wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array)
#2 /wp-content/plugins/woocommerce/includes/class-wc-checkout.php(300): apply_filters('woocommerce_che...', Array)
#3 /wp-content/plugins/woocommerce-gateway-stripe/includes/payment-methods/class-wc-stripe-upe-payment-gateway.php(362): WC_Checkout->get_checkout_fields('billing')
#4 /wp-content/plugins/woocommerce-gateway-stripe/includes/class-wc-stripe-blocks-support.php(250): WC_Stripe_UPE_Payment_Gateway->javascript_params()
#5 /wp-content/plugins/woocommerce-gateway-stripe/includes/class-wc-stripe-blocks-support.php(175): WC_Stripe_Blocks_Support->get_gateway_javascript_params()
#6 /wp-content/plugins/woocommerce/src/Blocks/Payments/PaymentMethodRegistry.php(62): WC_Stripe_Blocks_Support->get_payment_method_data()
#7 /wp-content/plugins/woocommerce/src/Blocks/Payments/Api.php(92): Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry->get_all_registered_script_data()
#8 /wp-includes/class-wp-hook.php(324): Automattic\WooCommerce\Blocks\Payments\Api->add_payment_method_script_data('')
#9 /wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters('', Array)
#10 /wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#11 /wp-content/plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php(215): do_action('woocommerce_blo...')
#12 /wp-content/plugins/woocommerce/src/Blocks/BlockTypes/AbstractBlock.php(109): Automattic\WooCommerce\Blocks\BlockTypes\MiniCart->enqueue_data()
#13 /wp-includes/class-wp-hook.php(324): Automattic\WooCommerce\Blocks\BlockTypes\AbstractBlock->enqueue_editor_assets('')
#14 /wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#15 /wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#16 /wp-admin/edit-form-blocks.php(272): do_action('enqueue_block_e...')
#17 /wp-admin/post.php(187): require('/customers/c/3/...')`

Ref

$possibly_address = WC()->session->get('vc_address');

With the nullsafe operator, and the line then changed to
$possibly_address = WC()->session?->get('vc_address');
there is no longer a fatal error.

Thank you! I assumed there would always be a session when going to checkout; apparently not. Releasing a fix for this straight away.

I've published 2.1.1 with your fix.

Not so fast! The "fix" I supplied was to supposed demontstrate how the error may go away, not a real fix for this plugin.

The plugin says: 'Requires PHP: 5.6' and the nullsafe operator "?->" was introduced in PHP 8.0. This will fatal many more.
Please revert, and I will make a fix working in PHP 5.6. But why not raise to 7.0 as current WordPress?

Both good points. It suffices to check if ->session exists, but there is no point in supporting 5.6 any longer. I'll bump the version and check for null differently.

This should work even in PHP 5.6:
if ( !is_object( $session = WC()->session ) ) return $fields; $possibly_address = WC()->session->get('vc_address');
A similar check with early return, is made just below these lines.

Yes; I'm testing code now that just checks empty(WC()->session).

Done! PHP requirements are bumped as well. If somebody is stuck on 5, older versions will be available; and if that's not enough we'll try to help.