我需要将WooCommerce设置为自动清理旧的已完成订单.搬到垃圾桶是可以的,但我真的更愿意把它们删除.以下是我现在使用的方法,但似乎不起作用:

function expire_after_x_days(){
        global $wpdb;
    // Get current time
        $today = date("mdy");

    // set time to expire
        $time_to_expire = "-180 days";
        $expiration_date = date("mdy", strtotime( $today . $time_to_expire));

    // Get orders with processing status
        $result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-completed'");

        if( !empty($result)) foreach ($result as $order){
            // Get order's time
            $order_time = get_the_time('mdy', $order->ID );

    // Compare order's time with current time -10 days
        if ( $order_time < $expiration_date ){

    // Update order status    
               wp_delete_post($order_id,true);
            }
        }
} 

    // Use the best HOOK for your case 
    add_action( 'admin_footer', 'expire_after_x_days' );

    // OR simply call the function if you are pasting this in your functions.php 

我做错了什么?

function expire_after_x_days(){
        global $wpdb;
    // Get current time
        $today = date("mdy");

    // set time to expire
        $time_to_expire = "-180 days";
        $expiration_date = date("mdy", strtotime( $today . $time_to_expire));

    // Get orders with processing status
        $result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'shop_order' AND post_status = 'wc-completed'");

        if( !empty($result)) foreach ($result as $order){
            // Get order's time
            $order_time = get_the_time('mdy', $order->ID );

    // Compare order's time with current time -10 days
        if ( $order_time < $expiration_date ){

    // Update order status    
               wp_delete_post($order_id,true);
            }
        }
} 

    // Use the best HOOK for your case 
    add_action( 'admin_footer', 'expire_after_x_days' );

    // OR simply call the function if you are pasting this in your functions.php 

我做错了什么?

推荐答案

取而代之的是使用以下将垃圾和删除所有旧的完成的订单(older than 180 days),批次200,以避免崩溃您的网站.不需要使用任何WordPress计划的操作,这些操作确实不可靠.

以下是一个简化和改进的代码版本:

add_action( 'admin_footer', 'auto_trash_old_completed_orders' );
function auto_trash_old_completed_orders(){
    $completed_order_ids = wc_get_orders( array(
        'limit'         => 200, // By batch of 200 orders
        'status'        => 'completed', 
        'return'        => 'ids',
        'date_created'  => '<' . date("Y-m-d", strtotime("-6 months")),
    ) );

    if ( count($completed_order_ids) > 0 ){
        foreach($completed_order_ids as $completed_order_id ) {
            wp_trash_post($completed_order_id); //
            wp_delete_post($completed_order_id, true);
        }
    }
} 

应该能行得通.

Php相关问答推荐

Laravel迁移返回SQL错误,但SQL有效且工作正常

一列必须指定多个变量的Laravel查询

以编程方式更新现有Yith WooCommerce Booking的状态

在WooCommerce产品中按类型对产品属性术语进行分类以供显示

筛选器具有多个查询之一

Laravel自定义验证规则更正

WooCommerce在收据页面获取产品下载URL

将数据推入所有子数组条目

如何在codeigniter中将order_by RAND与下面的代码一起使用

在 PHP 中将字符串分解为数组(字符串类似于 WP 短代码)

有没有办法像引用枚举一样引用注册表中的对象?

使用帖子 ID 更新 wp 帖子

对 WooCommerce 购物车和 checkout 中显示的运输方式进行排序

如何在 Laravel 迁移中使用日期时间和天数?

使用用户元数据填写 woocommerce checkout 字段

在帖子内容中使用短代码触发 Woocommerce 挂钩

NetBeans 不显示中文内容

如何在 php/laravel 中获取元素在数组中的位置

将加密/解密函数从 PHP 转换为 NodeJS

如何简化 php API 中的格式化数组列表数据以提供 React Native 部分列表