Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GMT date when fetching cancellation orders. #43641

Merged
merged 4 commits into from Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/fix-43593
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Use GMT date when fetching orders to auto-cancel.
Expand Up @@ -987,10 +987,24 @@ public function get_orders( $args = array() ) {
/**
* Get unpaid orders last updated before the specified date.
*
* @param int $date Timestamp.
* @return array
* @param int $date This timestamp is expected in the timezone in WordPress settings for legacy reason, even though it's not a good practice.
*
* @return array Array of order IDs.
*/
public function get_unpaid_orders( $date ) {
$timezone_offset = wc_timezone_offset();
$gmt_timestamp = $date - $timezone_offset;
return $this->get_unpaid_orders_gmt( absint( $gmt_timestamp ) );
}

/**
* Get unpaid orders last updated before the specified GMT date.
*
* @param int $gmt_timestamp GMT timestamp.
*
* @return array Array of order IDs.
*/
public function get_unpaid_orders_gmt( $gmt_timestamp ) {
global $wpdb;

$orders_table = self::get_orders_table_name();
Expand All @@ -1004,7 +1018,7 @@ public function get_unpaid_orders( $date ) {
AND {$orders_table}.status = %s
AND {$orders_table}.date_updated_gmt < %s",
'wc-pending',
gmdate( 'Y-m-d H:i:s', absint( $date ) )
gmdate( 'Y-m-d H:i:s', absint( $gmt_timestamp ) )
)
);
// phpcs:enable
Expand Down
Expand Up @@ -1207,7 +1207,9 @@ public function test_get_order_count(): void {
*/
public function test_get_unpaid_orders(): void {
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested -- Intentional usage since timezone is changed for this file.
$now = current_time( 'timestamp' );
$now_gmt = time();
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested -- Testing a legacy code that does expect the offset timestamp.
$now_ist = current_time( 'timestamp', 0 ); // IST (Indian standard time) is 5.5 hours ahead of GMT and is set as timezone for this class.

// Create a few orders.
$orders_by_status = array(
Expand All @@ -1220,7 +1222,7 @@ public function test_get_unpaid_orders(): void {
$order = new \WC_Order();
$this->switch_data_store( $order, $this->sut );
$order->set_status( $order_status );
$order->set_date_modified( $now - DAY_IN_SECONDS );
$order->set_date_modified( $now_gmt - DAY_IN_SECONDS );
$order->save();

if ( ! $order->is_paid() ) {
Expand All @@ -1233,12 +1235,11 @@ public function test_get_unpaid_orders(): void {
$this->assertEquals( $orders_by_status['wc-completed'], $this->sut->get_order_count( 'wc-completed' ) );

// Find unpaid orders.
$this->assertEqualsCanonicalizing( $unpaid_ids, $this->sut->get_unpaid_orders( $now ) );
$this->assertEqualsCanonicalizing( $unpaid_ids, $this->sut->get_unpaid_orders( $now - HOUR_IN_SECONDS ) );
$this->assertEqualsCanonicalizing( $unpaid_ids, $this->sut->get_unpaid_orders( $now_ist ) );
$this->assertEqualsCanonicalizing( $unpaid_ids, $this->sut->get_unpaid_orders( $now_ist - HOUR_IN_SECONDS ) );

// No unpaid orders from before yesterday.
$this->assertCount( 0, $this->sut->get_unpaid_orders( $now - WEEK_IN_SECONDS ) );

$this->assertCount( 0, $this->sut->get_unpaid_orders( $now_ist - DAY_IN_SECONDS ) );
}

/**
Expand Down Expand Up @@ -3028,7 +3029,6 @@ public function test_timezone_date_query_support() {
$order->set_date_created( '2023-09-01T00:30:00' ); // This would be 2023-08-31T18:00:00 UTC given the current timezone.
$this->sut->create( $order );


$query = new OrdersTableQuery( array( 'date_created_gmt' => '2023-09-01' ) );
$this->assertEquals( 0, count( $query->orders ) ); // Should not return anything as the order was created on 2023-08-31 UTC.

Expand Down Expand Up @@ -3058,9 +3058,12 @@ public function test_data_retained_when_hooked_in_cache_filter() {
$this->toggle_cot_authoritative( true );
$this->enable_cot_sync();

add_action( 'woocommerce_delete_shop_order_transients', function ( $order_id ) {
wc_get_order( $order_id );
} );
add_action(
'woocommerce_delete_shop_order_transients',
function ( $order_id ) {
wc_get_order( $order_id );
}
);
$order = OrderHelper::create_order();

$this->assertEquals( 1, $order->get_customer_id() );
Expand All @@ -3087,7 +3090,7 @@ public function test_order_cache_is_cleared_on_meta_save() {

$order = OrderHelper::create_order();

// set the cache
// set the cache.
wc_get_order( $order->get_id() );

$order->add_meta_data( 'test_key', 'test_value' );
Expand Down