【问题标题】:WooCommerce Critical error at order status change订单状态更改时的 WooCommerce 严重错误
【发布时间】:2021-12-03 04:33:06
【问题描述】:

我正在使用此代码添加管理操作以更改批量订单状态。 我有名为“wc-to-order”的自定义构建状态。 不幸的是,使用 Woo 批量操作中的代码时出现错误。 谁能帮我解决?

这是代码:

add_action( 'admin_action_wc-to-order', 'add_new_order_status_wc_on_hold' ); // admin_action_{action name}
function add_new_order_status_wc_on_hold() {
 
    // if an array with order IDs is not presented, exit the function
    if( !isset( $_REQUEST['post'] ) && !is_array( $_REQUEST['post'] ) )
        return;
 
    foreach( $_REQUEST['post'] as $order_id ) {
 
        $order = new WC_Order( $order_id );
        $order_note = 'That\'s what happened by bulk edit:';
        $order->update_status( 'wc-to-order', $order_note, true ); 
 
    }
 
    
    $location = add_query_arg( array(
        'post_type' => 'shop_order',
        'wc-to-order' => 1, // markED_awaiting_shipment=1 is just the $_GET variable for notices
        'changed' => count( $_REQUEST['post'] ), // number of changed orders
        'ids' => join( $_REQUEST['post'], ',' ),
        'post_status' => 'all'
    ), 'edit.php' );
 
    wp_redirect( admin_url( $location ) );
    exit;
 
}

我在调试日志中得到了这个

  PHP Fatal error:  Uncaught TypeError: join(): Argument #2 ($array) must be of type ?array, string given in /public_html/sitename/wp-content/themes/themename/functions.php:648

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce woocommerce-theming


    【解决方案1】:

    从日志看来,$_REQUEST['post'] 可能不是一个数组。在下面的代码中,我们首先检查它是否是一个数组。如果不是,那么我们将其转换为数组。

    add_action( 'admin_action_wc-to-order', 'add_new_order_status_wc_on_hold' ); // admin_action_{action name}
    function add_new_order_status_wc_on_hold() {
    
    // if an array with order IDs is not presented, exit the function
    if( !isset( $_REQUEST['post'] ) ) {
       return;
    }
     
    //check if it is an array. If not, cast it as array
    $orders_array = is_array($_REQUEST['post']) ? $_REQUEST['post'] : array($_REQUEST['post']);
    
         foreach( $orders_array as $order_id ) {
     
            $order = new WC_Order( $order_id );
            $order_note = 'That\'s what happened by bulk edit:';
            $order->update_status( 'wc-to-order', $order_note, true ); 
     
        }
     
        
        $location = add_query_arg( array(
            'post_type' => 'shop_order',
            'wc-to-order' => 1, // markED_awaiting_shipment=1 is just the $_GET variable for notices
            'changed' => count( $orders_array ), // number of changed orders
            'ids' => join( $orders_array, ',' ),
            'post_status' => 'all'
        ), 'edit.php' );
     
        wp_redirect( admin_url( $location ) );
        exit;
     
    }
    
    

    这样我们确保数组总是提供给join函数。

    【讨论】:

      猜你喜欢
      • 2017-05-24
      • 2018-08-30
      • 1970-01-01
      • 2022-10-05
      • 2018-01-01
      • 2019-02-01
      • 2022-09-30
      • 2020-01-23
      • 2021-04-08
      相关资源
      最近更新 更多