bookingactivities / booking-activities

Wordpress plugin booking system
GNU General Public License v3.0
29 stars 10 forks source link

API REST login triggers Uncaught Error: Call to a member function get_cart() on null #158

Closed rngadam closed 2 years ago

rngadam commented 2 years ago

wp_signon in the context of an API rest call from a third party webapp seems to fail because booking_activities tries to retrieve cart items and $woocommerce object has a null cart associated with it.

error log:

17-Aug-2022 16:58:50 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get_cart() on null in /nas/content/live/tambourdev1/wp-content/plugins/booking-activities/functions/functions-woocommerce.php:124
Stack trace:
#0 /nas/content/live/tambourdev1/wp-content/plugins/booking-activities/controller/controller-woocommerce-frontend.php(76): bookacti_wc_get_cart_items_bookings()
#1 /nas/content/live/tambourdev1/wp-includes/class-wp-hook.php(307): bookacti_change_customer_id_to_user_id('gestion', Object(WP_User))
#2 /nas/content/live/tambourdev1/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters(NULL, Array)
#3 /nas/content/live/tambourdev1/wp-includes/plugin.php(474): WP_Hook->do_action(Array)
#4 /nas/content/live/tambourdev1/wp-includes/user.php(110): do_action('wp_login', 'gestion', Object(WP_User))
#5 /nas/content/live/tambourdev1/wp-content/plugins/foosales/classes/class-foosales-rest-api.php(177): wp_signon(Array, false)
#6 /nas/content/live/tambourdev1/wp-content/plugins/foosales/classes/class-foosales-rest-api.php(241): FooSales_ in /nas/content/live/tambourdev1/wp-content/plugins/booking-activities/functions/functions-woocommerce.php on line 124

the corresponding code in booking-activities/functions/functions-woocommerce.php

function bookacti_wc_get_cart_items_bookings( $cart_items = array(), $filters = array() ) {
        global $woocommerce;
        if( ! $cart_items ) { $cart_items = $woocommerce->cart->get_cart(); }

Should there be a check here to make sure that $woocommerce has a non-null cart in this context?

rngadam commented 2 years ago

This check seems to fix it:

function bookacti_wc_get_cart_items_bookings( $cart_items = array(), $filters = array() ) {
        global $woocommerce;
        if( ! $woocommerce->cart ) { return; }
yoancutillas commented 2 years ago

Thank you for your report! This bug is fixed in the dev branch and will be released in the next update.

The final fix is:

function bookacti_wc_get_cart_items_bookings( $cart_items = array(), $filters = array() ) {
        global $woocommerce;
        if( ! $cart_items && ! empty( $woocommerce->cart ) ) { $cart_items = $woocommerce->cart->get_cart(); }
rngadam commented 2 years ago

Thank you! We'll try the next update and report back.

bobrobes commented 2 years ago

Please, it's the same problem for this ?

Fatal error: Uncaught Error: Call to a member function get_cart() on null in /Users/adlencherif/Local Sites/demo-wordpress/app/public/wp-content/plugins/booking-activities/functions/functions-woocommerce.php on line 124

Error: Call to a member function get_cart() on null in /Users/adlencherif/Local Sites/demo-wordpress/app/public/wp-content/plugins/booking-activities/f

Capture d’écran 2022-08-24 à 09 11 44

unctions/functions-woocommerce.php on line 124

yoancutillas commented 2 years ago

Yes, this is the same error. You can replace wp-content/plugins/booking-activities/functions/functions-woocommerce.php line 124 if( ! $cart_items ) { $cart_items = $woocommerce->cart->get_cart(); } with if( ! $cart_items && ! empty( $woocommerce->cart ) ) { $cart_items = $woocommerce->cart->get_cart(); } as a temporary fix.