Client area snippet that makes issues in admin area
-
I’m setting up a WordPress/WooCommerce site, and I’m experiencing an issue that is driving me crazy…
I inserted a code snippet to filter payment gateway according to customer country (there’re plenty of them around, all of them very similar). Something like this (copied from https://www.prodjex.com/2018/02/disable-payment-gateways-based-country-woocommerce/ ):add_filter( 'woocommerce_available_payment_gateways', 'rudr_gateway_by_country' ); function rudr_gateway_by_country( $gateways ) { if( is_admin() ) { return $gateways; } if( is_wc_endpoint_url( 'order-pay' ) ) { // Pay for order page $order = wc_get_order( wc_get_order_id_by_order_key( $_GET[ 'key' ] ) ); $country = $order->get_billing_country(); } else { // Cart page $country = WC()->customer->get_billing_country(); } if ( 'SI' === $country ) { if ( isset( $gateways[ 'cpsw_stripe' ] ) ) { unset( $gateways[ 'cpsw_stripe' ] ); } } return $gateways; }
In client area the snippet works fine.
But when this snippet is enabled, in admin area it’s impossible to edit any page or post; doing “update”, I’m just getting the “Updating failed.” message…
If I disable the snippet, everything in admin area works fine again.
Looking into logs, when the error in admin area happens, I can find:[20-Feb-2023 18:21:45 UTC] PHP Fatal error: Uncaught Error: Call to a member function get_billing_country() on null in /usr/www/users/eurossd/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()'d code:17 Stack trace: #0 /usr/www/users/eurossd/wp-includes/class-wp-hook.php(308): rudr_gateway_by_country(Array) #1 /usr/www/users/eurossd/wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array) #2 /usr/www/users/eurossd/wp-content/plugins/woocommerce/includes/class-wc-payment-gateways.php(163): apply_filters('woocommerce_ava...', Array) #3 /usr/www/users/eurossd/wp-content/plugins/woocommerce-paypal-payments/modules/ppcp-wc-gateway/services.php(1322): WC_Payment_Gateways->get_available_payment_gateways() #4 /usr/www/users/eurossd/wp-content/plugins/woocommerce-paypal-payments/lib/packages/Dhii/Container/DelegatingContainer.php(117): WooCommerce\PayPalCommerce\WcGateway\WCGatewayModule::WooCommerce\PayPalCommerce\WcGateway\{closure}(Object(WooCommerce\PayPalCommerce\Vendor\Dhii\Container\DelegatingContainer)) #5 /usr/www/users/eurossd in /usr/www/users/eurossd/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()'d code on line 17
I did several attempts:
- I tried to insert the snippet in functions.php, or using two different snippet management plugins: noting change.
- I tried to change the theme, going on vanilla twenty-twentyone: nothing change
- I tried to disable few plugins (but not all of them): nothing change
Any idea?!
- The topic ‘Client area snippet that makes issues in admin area’ is closed to new replies.