WooCommerce 订阅:结帐后出现严重错误消息

时间:2021-01-18 12:07:03

标签: php wordpress woocommerce fatal-error woocommerce-subscriptions

我们最近发现了下订单后的错误消息(显示在 WooCommerce 的查看订单端点上)。在最底部显示“此网站出现严重错误”。但似乎没有任何损坏/不起作用。从那以后,我启用了调试模式,这使我们似乎可以将其缩小到具有以下堆栈跟踪的自定义插件:

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

custommanager 的第 75 行是如下函数:

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>";
            }
        }
    }
}

此生成的取消按钮在我们的订阅管理页面中显示并起作用,但似乎是每个订单(订阅与否)上显示的严重错误消息的原因

有人能指出我们解决这个问题的正确方向吗?

2 个答案:

答案 0 :(得分:0)

为了避免这个问题,您可以尝试使用条件函数function_exists(),如下所示:

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>";
                }
            }
        }
    }
}

它可以工作。

答案 1 :(得分:0)

请包括 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>";
            }
        }
    }
}

可以通过过滤器钩子wcs_view_subscription_actions

来实现