Error while calling get_cart() inside ‘available_payments’ filter
-
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 109The question is: How we should call the cart object at those point (woocommerce_available_payment_gateways)?
Thanks
- The topic ‘Error while calling get_cart() inside ‘available_payments’ filter’ is closed to new replies.