Search code examples
phpwordpresswoocommercefatal-errorwoocommerce-subscriptions

WooCommerce subscriptions: Critical error message after checkout


We have recently discovered an error message once an order is placed (shown on the view order endpoint of WooCommerce). At the very bottom it says "There has been a critical error on this website." but nothing seems to be broken/not working. I have since enabled debug mode which has allowed us to seemingly narrow it down to a custom plugin with the following stack trace:

Fatal error: Uncaught Error: Call to undefined function wcs_get_all_user_actions_for_subscription() in /wp-content/plugins/custommanager/custommanager.php:75 Stack trace: #0 /wp-includes/class-wp-hook.php(287): addCancelButton(Object(WC_Order)) #1 /wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) #2 /wp-includes/plugin.php(484): WP_Hook->do_action(Array) #3 /wp-content/plugins/woocommerce/templates/order/order-details-customer.php(60): do_action('woocommerce_ord...', Object(WC_Order)) #4 /wp-content/plugins/woocommerce/includes/wc-core-functions.php(249): include('/home/mysite...') #5 /wp-content/plugins/woocommerce/templates/order/order-details.php(105): wc_get_template('order/order-det...', Array) #6 /wp-content/plugins/custommanager/custommanager.php on line 75

Line 75 of custommanager is the following function:

function addCancelButton($subscription) {
    $actions = wcs_get_all_user_actions_for_subscription( $subscription, get_current_user_id() );

    if(!empty($actions)){
        foreach ( $actions as $key => $action ){
            if(strtolower($action['name']) == "cancel"){
                $cancelLink = esc_url( $action['url'] );
                echo "<br/><p><a href='$cancelLink' class='button cancel'>Cancel Subscription</a></p>";
            }
        }
    }
}

The cancel button this generates is showing and works within our subscription management page but appears to be the cause of the critical error message showing on every order (subscription or not)

Is anyone able to point us in the right direction to resolve this?


Solution

  • To avoid this problem, you may try to use conditional function function_exists() as follows:

    function addCancelButton($subscription) {
        if( function_exists('wcs_get_all_user_actions_for_subscription') ){
            $actions = wcs_get_all_user_actions_for_subscription( $subscription, get_current_user_id() );
        
        
            if( $actions ){
                foreach ( $actions as $key => $action ){
                    if(strtolower($action['name']) == "cancel"){
                        $cancelLink = esc_url( $action['url'] );
                        echo "<br/><p><a href='$cancelLink' class='button cancel'>Cancel Subscription</a></p>";
                    }
                }
            }
        }
    }
    

    It could works.