Cleanup draft orders (https://github.com/woocommerce/woocommerce-blocks/pull/1855)
* On init, create cron events * Cleanup query + unit test * Replace cron with AS
This commit is contained in:
parent
a602a69d4d
commit
25a2367338
|
@ -21,8 +21,10 @@ class Library {
|
|||
add_action( 'init', array( __CLASS__, 'register_blocks' ) );
|
||||
add_action( 'init', array( __CLASS__, 'define_tables' ) );
|
||||
add_action( 'init', array( __CLASS__, 'maybe_create_tables' ) );
|
||||
add_action( 'init', array( __CLASS__, 'maybe_create_cronjobs' ) );
|
||||
add_filter( 'wc_order_statuses', array( __CLASS__, 'register_draft_order_status' ) );
|
||||
add_filter( 'woocommerce_register_shop_order_post_statuses', array( __CLASS__, 'register_draft_order_post_status' ) );
|
||||
add_action( 'woocommerce_cleanup_draft_orders', array( __CLASS__, 'delete_expired_draft_orders' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,6 +80,15 @@ class Library {
|
|||
update_option( 'wc_blocks_db_version', \Automattic\WooCommerce\Blocks\Package::get_version() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe create cron events.
|
||||
*/
|
||||
public static function maybe_create_cronjobs() {
|
||||
if ( function_exists( 'as_next_scheduled_action' ) && false === as_next_scheduled_action( 'woocommerce_cleanup_draft_orders' ) ) {
|
||||
as_schedule_recurring_action( strtotime( 'midnight tonight' ), DAY_IN_SECONDS, 'woocommerce_cleanup_draft_orders' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register blocks, hooking up assets and render functions as needed.
|
||||
*/
|
||||
|
@ -151,4 +162,24 @@ class Library {
|
|||
];
|
||||
return $statuses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete draft orders older than a day.
|
||||
*
|
||||
* Ran on a daily cron schedule.
|
||||
*/
|
||||
public static function delete_expired_draft_orders() {
|
||||
global $wpdb;
|
||||
|
||||
$wpdb->query(
|
||||
"
|
||||
DELETE posts, term_relationships, postmeta
|
||||
FROM $wpdb->posts posts
|
||||
LEFT JOIN $wpdb->term_relationships term_relationships ON ( posts.ID = term_relationships.object_id )
|
||||
LEFT JOIN $wpdb->postmeta postmeta ON ( posts.ID = postmeta.post_id )
|
||||
WHERE posts.post_status = 'wc-checkout-draft'
|
||||
AND posts.post_modified <= ( NOW() - INTERVAL 1 DAY )
|
||||
"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
namespace Automattic\WooCommerce\Blocks\Tests\Library;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use \WC_Order;
|
||||
use Automattic\WooCommerce\Blocks\Library;
|
||||
|
||||
/**
|
||||
* Tests Delete Draft Orders functionality
|
||||
*
|
||||
* @since $VID:$
|
||||
* @group testing
|
||||
*/
|
||||
class DeleteDraftOrders extends TestCase {
|
||||
/**
|
||||
* During setup create some draft orders.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setUp() {
|
||||
global $wpdb;
|
||||
|
||||
$order = new WC_Order();
|
||||
$order->set_status( 'checkout-draft' );
|
||||
$order->save();
|
||||
|
||||
$order = new WC_Order();
|
||||
$order->set_status( 'checkout-draft' );
|
||||
$order->save();
|
||||
$wpdb->update(
|
||||
$wpdb->posts,
|
||||
array(
|
||||
'post_modified' => date( 'Y-m-d H:i:s', strtotime( '-1 DAY', current_time( 'timestamp' ) ) ),
|
||||
'post_modified_gmt' => gmdate( 'Y-m-d H:i:s', strtotime( '-1 DAY' ) )
|
||||
),
|
||||
array(
|
||||
'ID' => $order->get_id()
|
||||
)
|
||||
);
|
||||
|
||||
$order = new WC_Order();
|
||||
$order->set_status( 'checkout-draft' );
|
||||
$order->save();
|
||||
$wpdb->update(
|
||||
$wpdb->posts,
|
||||
array(
|
||||
'post_modified' => date( 'Y-m-d H:i:s', strtotime( '-2 DAY', current_time( 'timestamp' ) ) ),
|
||||
'post_modified_gmt' => gmdate( 'Y-m-d H:i:s', strtotime( '-2 DAY' ) )
|
||||
),
|
||||
array(
|
||||
'ID' => $order->get_id()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete draft orders older than a day.
|
||||
*
|
||||
* Ran on a daily cron schedule.
|
||||
*/
|
||||
public function test_delete_expired_draft_orders() {
|
||||
global $wpdb;
|
||||
|
||||
// Check there are 3 draft orders from our setup before running tests.
|
||||
$this->assertEquals( 3, (int) $wpdb->get_var( "SELECT COUNT(ID) from $wpdb->posts posts WHERE posts.post_status = 'wc-checkout-draft'" ) );
|
||||
|
||||
// Run delete query.
|
||||
Library::delete_expired_draft_orders();
|
||||
|
||||
// Only 1 should remain.
|
||||
$this->assertEquals( 1, (int) $wpdb->get_var( "SELECT COUNT(ID) from $wpdb->posts posts WHERE posts.post_status = 'wc-checkout-draft'" ) );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue