完成订单时WooCommerce订单操作挂钩错误

时间:2020-10-30 10:19:00

标签: php wordpress woocommerce

因此,我正在使用woocommerce_order_actions挂钩将一些新操作添加到列表中。一切正常,但是现在当我尝试将订单状态更改为已完成时,出现了一个奇怪的错误。

function add_order_actions($actions) {
    global $theorder;
    $user = wp_get_current_user();

    if(array_intersect(['administrator'], $user->roles)) {
       $actions['wc_action_reset_test'] = 'Reset test result';
    }

    return $actions;
}
add_action('woocommerce_order_actions', 'add_order_actions');

因此,如果我将订单状态更改为立即完成并按更新,它将显示来自wordpress的严重错误页面。并没有真正给出错误消息。

该如何解决?

1 个答案:

答案 0 :(得分:1)

很难说没有出错,但是您可以添加一些额外的检查以防止出错,在代码中添加注释并附带说明。

function action_woocommerce_order_actions( $actions ) {
    global $theorder;

    // This is used by some callbacks attached to hooks such as woocommerce_order_actions which rely on the global to determine if actions should be displayed for certain orders.
    if ( ! is_object( $theorder ) ) {
        $theorder = wc_get_order( $post->ID );
    }

    // Get current user
    $user = wp_get_current_user();

    // Safe usage
    if ( ! ( $user instanceof WP_User ) ) {
        return;
    }

    // In array
    if ( in_array( 'administrator', $user->roles ) ) {
        $actions['wc_action_reset_test'] = __( 'Reset test result', 'woocommerce' );
    }

    return $actions;
}
add_action( 'woocommerce_order_actions', 'action_woocommerce_order_actions', 10, 1 );