• 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?!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I assume code:17 refers to WC()->customer->get_billing_country() as opposed to $order->get_billing_country(). The WC()->customer object apparently does not have a get_billing_country() method. Or WC()->customer is not returning a valid customer object. Research WC documentation to confirm if that’s a valid customer method or not. If it is, then we need to know why WC()->customer is not a valid customer object. It could be out of context for when the filter fires.

    You need to do some basic debugging to learn where the data is not as expected. You could var_dump() different variables, properties, or function returns to see what’s going on. Where something is not as expected, figure out a different way to get what’s needed. For example, if WC()->customer is invalid, maybe the customer can be determined from the order.

    You can also ask for assistance in the dedicated support forum for WooCommerce, but the more detail you can work out for yourself, the more specific the answers will be.

Viewing 1 replies (of 1 total)
  • The topic ‘Client area snippet that makes issues in admin area’ is closed to new replies.