skip to Main Content

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?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. Please include the missing function from WCS

    function addCancelButton( $subscription ) {
        require_once( WP_PLUGIN_DIR . '/woocommerce-subscriptions/includes/wcs-user-functions.php' );
        $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>";
                }
            }
        }
    }
    

    It can be achieved through the filter hook wcs_view_subscription_actions

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search