【问题标题】:Change the status of orders automatically自动更改订单状态
【发布时间】:2021-11-26 09:59:57
【问题描述】:

如果未处理,我需要在 7 天后自动将订单状态更改为(已取消)。

我需要将状态从:新请求更改为已取消。 要么 我需要将状态从:处理中更改为已取消。

谢谢 阿德南

【问题讨论】:

  • 您将需要服务器 cron 作业而不是 wp cron 来实现这一点。 WP cron 仅在有人访问您的网站时才有效,因此我会避免使用它。然后获取所有具有所需状态的订单并更新。
  • 我的服务器上有一个服务器 cron 作业,但我仍然需要将 php 代码添加到我的 WP 以自动取消订单

标签: wordpress woocommerce hook


【解决方案1】:

有用的文档 - https://developer.wordpress.org/reference/classes/wp_query/#date-parameters https://woocommerce.wp-a2z.org/oik_api/wc_get_order_statuses/

//My server cronjob is targeting wp-cron.php 
function ss_cancel_failed_orders() {
    $held_duration = 15; //minutes how often my cron job to run per day/hour
    $data_store = WC_Data_Store::load('order');

    //Change post_status for desired order statuses
    //Change date_query before value to desired time compare with modified post date
    $unpaid_orders = get_posts(array('posts_per_page' => -1, 'post_type' => 'shop_order', 'post_status' => array('wc-failed','wc-on-hold'), 'date_query' => array(array('before' => '15 minutes ago', 'column'=>'post_modified' ))));
    if ( $unpaid_orders ) {
        foreach ( $unpaid_orders as $unpaid_order ) {
            $order = wc_get_order( $unpaid_order );
            $order->update_status( 'cancelled', __( 'Order has expired.', 'woocommerce' ) ); // Status here is without wc- prefix
        }
    }
    wp_clear_scheduled_hook( 'ssa_cancel_failed_orders' );
    wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'ssa_cancel_failed_orders' ); // you can remove wp cron and work with server cron only
  }
  add_action('ssa_cancel_failed_orders', 'ss_cancel_failed_orders');

【讨论】:

  • 谢谢。我测试过,但似乎不起作用。我的 cron 作业每 5 分钟运行一次。我测试了订单状态 wp-pending。还有一个问题,array('wc-failed','wc-on-hold') 是指 wc-failed 还是 wc-on-hold?
  • 使用 - wordpress.org/plugins/wp-crontrol 检查发生了什么。表示抓取状态为 wc-failed 和 wc-on-hold 的订单。
  • 即使我使用 wp-control 插件手动运行事件也没有发生任何事情。
  • 有订单吗?
  • 我有数百个订单。但是代码不会更新状态。
【解决方案2】:

我在这段代码的帮助下做到了:

function autocancel_wc_orders(){
    $query = ( array(
        'limit'   => 10,
        'orderby' => 'date',
        'order'   => 'DESC',
        'status'  => array( 'wc-pending', 'wc-ywraq-new', 'wc-ywraq- 
        pending')
    ) );
    $orders = wc_get_orders( $query );
    foreach( $orders as $order ){       

        $date     = new DateTime( $order->get_date_created() );
        $today    = new DateTime();
        $interval = $date->diff($today);

        $datediff = $interval->format('%a');

        if( $datediff > 2 ){
            $order->update_status('cancelled', 'Cancelled for missing 
            payment');
        }   
    }
} 
add_action( 'admin_init', 'autocancel_wc_orders' );

我在链接中在线找到了这个答案: https://samuelsilva.pt/cancel-woocommerce-not-paid-orders-automatically/

【讨论】:

    猜你喜欢
    • 2018-08-30
    • 2017-05-24
    • 1970-01-01
    • 2019-08-27
    • 2018-02-27
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    相关资源
    最近更新 更多