• Resolved giangel84

    (@giangel84)


    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 2 years ago by giangel84. Reason: path names
    • This topic was modified 2 years ago by giangel84. Reason: path disclosure info
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter giangel84

    (@giangel84)

    Solved:

    The correct way is getting cart by using:

    WC()->cart->get_cart_contents(); instead of WC()->cart->get_cart();

    Hi @giangel84

    Thanks for reaching out!

    I’m glad you were able to find a solution to your inquiry here and thanks for sharing it with the community too! ??

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Error while calling get_cart() inside ‘available_payments’ filter’ is closed to new replies.