【问题标题】:Auto completed status for all existing processing orders in WooCommerceWooCommerce 中所有现有处理订单的自动完成状态
【发布时间】:2017-04-23 14:31:01
【问题描述】:

我在 WooCommerce 上使用这个小代码from this answer 来自动完成支付处理订单:

/**
 * AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );

  // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
  if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cod' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cheque' ) ) {
    return;
    } 
  // "completed" updated status for paid Orders with all others payment methods
    else {
        $order->update_status( 'completed' );
    }
} 

但问题是我使用了一个特殊的 SMS 支付网关,它的 API 桥接在“cod”支付方式上,订单有时会在这个“woocommerce_thankyou”挂钩上保持暂停状态。

所以我需要一直扫描“处理”订单,以完整状态传递它们。我尝试了不同的东西和钩子,但我无法按预期工作。

我该怎么做?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce orders


    【解决方案1】:

    要让它工作,你只需要一个小函数,它会扫描所有在“init”钩子上处于“处理”状态的订单,并将此状态更新为“已完成”。

    代码如下:

    function auto_update_orders_status_from_processing_to_completed(){
        // Get all current "processing" customer orders
        $processing_orders = wc_get_orders( $args = array(
            'numberposts' => -1,
            'post_status' => 'wc-processing',
        ) );
        if(!empty($processing_orders))
            foreach($processing_orders as $order)
                $order->update_status( 'completed' );
    }
    add_action( 'init', 'auto_update_orders_status_from_processing_to_completed' );
    

    此代码已经过测试并且可以工作。

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

    建议和更新

    电子邮件通知发送两次存在一个小错误已在此处解决
    Avoid repetitive emails notification on some auto completed orders

    【讨论】:

    • 很棒的这段代码完全可以满足我的需求。感谢 Loïc 的帮助,你拯救了我的周末;)
    【解决方案2】:

    WooCommerce 虚拟订单可以在付款后自动标记为“已完成”,只需将一些代码添加到自定义插件或您的主题 functions.php 文件中。默认情况下,WooCommerce 将在成功付款后将虚拟可下载订单标记为“已完成”,这是有道理的,但一些店主甚至希望能够在付款时自动将虚拟订单标记为已完成,例如在网站的情况下在不需要采取进一步行动的情况下接受捐赠。为此,请使用以下代码,该代码基于核心虚拟可下载已完成订单状态:

    add_filter( 'woocommerce_payment_complete_order_status', 'virtual_order_payment_complete_order_status', 10, 2 );
    
    function virtual_order_payment_complete_order_status( $order_status, $order_id ) {
      $order = new WC_Order( $order_id );
    
      if ( 'processing' == $order_status &&
           ( 'on-hold' == $order->status || 'pending' == $order->status || 'failed' == $order->status ) ) {
    
        $virtual_order = null;
    
        if ( count( $order->get_items() ) > 0 ) {
    
          foreach( $order->get_items() as $item ) {
    
            if ( 'line_item' == $item['type'] ) {
    
              $_product = $order->get_product_from_item( $item );
    
              if ( ! $_product->is_virtual() ) {
                // once we've found one non-virtual product we know we're done, break out of the loop
                $virtual_order = false;
                break;
              } else {
                $virtual_order = true;
              }
            }
          }
        }
    
        // virtual order, mark as completed
        if ( $virtual_order ) {
          return 'completed';
        }
      }
    
      // non-virtual order, return original status
      return $order_status;
    }
    

    您也可以使用插件自动完成订单

    这是插件网址:https://wordpress.org/plugins/woocommerce-autocomplete-order/screenshots/

    请让我知道哪个对你有用。

    感谢。

    【讨论】:

    • 对不起 HK 但它对我不起作用,但 Loïc 的先前回答正是我想要的。感谢任何方式:)
    • 随时@NacerChikhi
    猜你喜欢
    • 2016-04-17
    • 2019-06-01
    • 2022-10-14
    • 2020-01-17
    • 2020-08-11
    • 2011-03-14
    • 2018-02-20
    • 2018-02-27
    • 1970-01-01
    相关资源
    最近更新 更多