【问题标题】:WooCommerce subscriptions: Critical error message after checkoutWooCommerce 订阅:结帐后出现严重错误消息
【发布时间】:2021-04-22 17:35:35
【问题描述】:

我们最近发现下订单后出现错误消息(显示在 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>";
            }
        }
    }
}

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

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

【问题讨论】:

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


    【解决方案1】:

    为避免此问题,您可以尝试使用条件函数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>";
                    }
                }
            }
        }
    }
    

    它可以工作。

    【讨论】:

    • 这似乎已经解决了问题(不确定是否是缓存问题,因为在客人结帐时它已经消失但在登录和购买时仍然显示)非常感谢。
    【解决方案2】:

    请包含 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实现

    【讨论】:

    • 很遗憾这没有任何效果,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-11-23
    • 2015-09-09
    • 2021-05-17
    • 2021-06-19
    • 2016-03-26
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多