Error while calling get_cart() inside ‘available_payments’ filter

[ad_1]

I wrote a function to disable “bacs” payment only if in cart there aren’t products of a certain category.
To do that I used:

'add_filter('woocommerce_available_payment_gateways', 'my_callback_function');

Inside ‘my_callback_function’ I get the cart using:

$cart = WC()->cart->get_cart();

And I get this:

PHP message: get_cart was called incorrectly. Get cart should not be called before the wp_loaded action.

PHP Fatal error: Uncaught Error: Call to a member function get_cart() on null in /home/user/public_html/wp-content/themes/mytheme/inc/wc/wc-checkout-functions.php:109
Stack trace:

PHP Fatal error: Uncaught Error: Call to a member function get_cart() on null in /home/user/public_html/wp-content/themes/mytheme/inc/wc/wc-checkout-functions.php:109
Stack trace:

0 /home/user/public_html/wp-includes/class-wp-hook.php(308): personalizzata_enable_bacs_payment_method(Array)

1 /home/user/public_html/wp-includes/plugin.php(205): WP_Hook->apply_filters(Array, Array)

2 /home/user/public_html/wp-content/plugins/woocommerce/includes/class-wc-payment-gateways.php(163): apply_filters(‘woocommerce_ava…’, Array)

3 /home/user/public_html/wp-content/plugins/woocommerce-paypal-payments/modules/ppcp-wc-gateway/services.php(1322): WC_Payment_Gateways->get_available_payment_gateways()

4 /home/user/public_html/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 /hom in /home/user/public_html/wp-content/themes/mytheme/inc/wc/wc-checkout-functions.php on line 109

The question is: How we should call the cart object at those point (woocommerce_available_payment_gateways)?

Thanks

  • This topic was modified 45 minutes ago by giangel84. Reason: path names
  • This topic was modified 44 minutes ago by giangel84. Reason: path disclosure info

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer