【问题标题】:Set WooCommerce order status when order is created from processing to pending当订单从处理中创建为待处理时设置 WooCommerce 订单状态
【发布时间】:2018-02-09 15:33:17
【问题描述】:

创建 woocommerce 订单时,订单状态为“处理中”。我需要将默认订单状态更改为“待处理”。

我怎样才能做到这一点?

【问题讨论】:

    标签: php wordpress woocommerce checkout orders


    【解决方案1】:

    默认订单状态由支付方式或支付网关设置。

    您可以尝试使用这个自定义挂钩函数,但它不起作用 (因为这个挂钩在支付方式和支付网关之前被触发)

    add_action( 'woocommerce_checkout_order_processed', 'changing_order_status_before_payment', 10, 3 );
    function changing_order_status_before_payment( $order_id, $posted_data, $order ){
        $order->update_status( 'pending' );
    }
    

    显然,每种支付方式(和支付网关)都在设置订单状态(取决于支付网关的交易响应)......

    对于货到付款方式,可以使用专用过滤器挂钩进行调整,请参阅:
    Change Cash on delivery default order status to "On Hold" instead of "Processing" in Woocommerce

    现在您可以更新订单状态使用woocommerce_thankyou挂钩:

    add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
    function woocommerce_thankyou_change_order_status( $order_id ){
        if( ! $order_id ) return;
    
        $order = wc_get_order( $order_id );
    
        if( $order->get_status() == 'processing' )
            $order->update_status( 'pending' );
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    经过测试和工作

    注意:钩子woocommerce_thankyou在每次加载收到的订单页面时都会触发,因此需要小心使用...
    现在上面的函数只会在第一次更新订单状态。如果客户重新加载页面,IF 语句中的条件将不再匹配,不会发生任何其他事情。


    相关话题:WooCommerce: Auto complete paid Orders (depending on Payment methods)

    【讨论】:

      【解决方案2】:

      此条件可防止在初始更改 3 分钟后刷新页面时客户端不自觉地更改状态。

      add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
      function woocommerce_thankyou_change_order_status( $order_id ){
          if( ! $order_id ) return;
      
          $order = wc_get_order( $order_id );
      
          $time_order     = strtotime($order->get_date_created());
          $time_current   = time();
          $time_interval  = $time_current - $time_order;
      
          //Case refresh page after 3 minutes at order, no changed status
          if( $order->get_status() == 'processing' && $time_interval < 180 ) {
              $order->update_status( 'pending' );
          }
      }
      

      【讨论】:

        【解决方案3】:
        // Rename order status 'Processing' to 'Order Completed' in admin main view - different hook, different value than the other places
        add_filter( 'wc_order_statuses', 'wc_renaming_order_status' );
        function wc_renaming_order_status( $order_statuses ) {
            foreach ( $order_statuses as $key => $status ) {
                if ( 'wc-processing' === $key ) 
                    $order_statuses['wc-processing'] = _x( 'Order Completed', 'Order status', 'woocommerce' );
            }
            return $order_statuses;
        }
        

        【讨论】:

          猜你喜欢
          • 2016-08-04
          • 2022-10-05
          • 2018-08-01
          • 1970-01-01
          • 2023-03-27
          • 2013-12-23
          • 2017-06-16
          • 2018-02-20
          • 2020-11-26
          相关资源
          最近更新 更多