Skip to content

Commit bdb3d78

Browse files
authored
Merge pull request #205 from Kit/fix-sync-past-orders-refunds-hpos
Fix Sync Past Orders when Refund Orders exist
2 parents 8428465 + 18658df commit bdb3d78

File tree

6 files changed

+124
-5
lines changed

6 files changed

+124
-5
lines changed

includes/class-ckwc-order.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,19 @@ public function send_purchase_data( $order_id, $status_old = 'new', $status_new
413413
'convertkit_for_woocommerce_error_order_missing',
414414
sprintf(
415415
/* translators: WooCommerce Order ID */
416-
__( 'Order ID %s could not be found in WooCommerce.', 'woocommerce-convertkit' ),
416+
__( 'Order ID #%s could not be found in WooCommerce.', 'woocommerce-convertkit' ),
417+
$order_id
418+
)
419+
);
420+
}
421+
422+
// If the Order is a refund, skip it.
423+
if ( $order instanceof \Automattic\WooCommerce\Admin\Overrides\OrderRefund ) {
424+
return new WP_Error(
425+
'convertkit_for_woocommerce_error_order_refund',
426+
sprintf(
427+
/* translators: WooCommerce Order ID */
428+
__( 'Order ID #%s is a refund. Skipping...', 'woocommerce-convertkit' ),
417429
$order_id
418430
)
419431
);
@@ -427,7 +439,7 @@ public function send_purchase_data( $order_id, $status_old = 'new', $status_new
427439
'convertkit_for_woocommerce_error_order_exists',
428440
sprintf(
429441
/* translators: WooCommerce Order ID */
430-
__( 'Order ID %s has already been sent to Kit.', 'woocommerce-convertkit' ),
442+
__( 'Order ID #%s has already been sent to Kit.', 'woocommerce-convertkit' ),
431443
$order_id
432444
)
433445
);
@@ -700,6 +712,8 @@ public function get_orders_not_sent_to_convertkit() {
700712
// Query HPOS.
701713
$query = new WC_Order_Query(
702714
array(
715+
// Return posts of type `shop_order`.
716+
'type' => 'shop_order',
703717
'limit' => -1,
704718

705719
// Only include Orders that do not match the Purchase Data Event integration setting.

tests/_support/Helper/Acceptance/WPBulkEdit.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,16 @@ public function bulkEdit($I, $postType, $postIDs, $configuration, $noticePostTyp
4141
}
4242
}
4343

44-
// Scroll to Bulk Edit label.
45-
$I->scrollTo('#bulk-edit-legend');
44+
switch ( $noticePostType ) {
45+
case 'coupon':
46+
// Scroll to Bulk Edit label.
47+
$I->scrollTo('#bulk-edit-legend');
48+
break;
49+
default:
50+
// Scroll to Stock Quantity.
51+
$I->scrollTo('select.change_stock');
52+
break;
53+
}
4654

4755
// Click Update.
4856
$I->click('Update');

tests/_support/Helper/Acceptance/WooCommerce.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,41 @@ public function wooCommerceChangeOrderStatus($I, $orderID, $orderStatus)
393393
);
394394
}
395395

396+
/**
397+
* Refunds the given Order ID.
398+
*
399+
* @since 1.9.2
400+
*
401+
* @param AcceptanceTester $I Acceptance Tester.
402+
* @param int $orderID WooCommerce Order ID.
403+
* @param int $amount Amount to refund.
404+
*/
405+
public function wooCommerceRefundOrder($I, $orderID, $amount)
406+
{
407+
// We perform the order status change by editing the Order as a WordPress Administrator would,
408+
// so that WooCommerce triggers its actions and filters that our integration hooks into.
409+
$I->loginAsAdmin();
410+
411+
// If the Order ID contains dashes, it's prefixed by the Custom Order Numbers Plugin.
412+
if (strpos($orderID, '-') !== false) {
413+
$orderIDParts = explode('-', $orderID);
414+
$orderID = $orderIDParts[ count($orderIDParts) - 1 ];
415+
}
416+
417+
// Load order edit screen.
418+
$I->amOnAdminPage('post.php?post=' . $orderID . '&action=edit');
419+
420+
// Refund the entire order.
421+
$I->click('button.refund-items');
422+
$I->waitForElementVisible('input#refund_amount');
423+
$I->fillField('input#refund_amount', $amount);
424+
$I->click('button.do-manual-refund');
425+
$I->acceptPopup();
426+
427+
// Wait for confirmation.
428+
$I->waitForElementVisible('abbr.refund_by');
429+
}
430+
396431
/**
397432
* Creates a 'Simple product' in WooCommerce that can be used for tests.
398433
*

tests/acceptance/sync-past-orders/SyncPastOrdersCest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,37 @@ public function testNoButtonDisplayedWhenNoPastOrders(AcceptanceTester $I)
135135
$I->dontSeeElementInDOM('a#ckwc_sync_past_orders');
136136
}
137137

138+
/**
139+
* Test that no button is displayed on the Integration Settings screen
140+
* when:
141+
* - the Integration is enabled,
142+
* - valid API credentials are specified,
143+
* - a WooCommerce Order exists, that has been refunded.
144+
*
145+
* @since 1.9.2
146+
*
147+
* @param AcceptanceTester $I Tester.
148+
*/
149+
public function testSyncPastOrderExcludesRefunds(AcceptanceTester $I)
150+
{
151+
// Delete all existing WooCommerce Orders from the database.
152+
$I->wooCommerceDeleteAllOrders($I);
153+
154+
// Create Product and Checkout for this test, sending the Order
155+
// to Kit.
156+
$result = $I->wooCommerceCreateProductAndCheckoutWithConfig($I);
157+
158+
// Refund the Order.
159+
$I->wooCommerceRefundOrder($I, $result['order_id'], 10);
160+
161+
// Load Settings screen.
162+
$I->loadConvertKitSettingsScreen($I);
163+
164+
// Confirm that no Sync Past Order button is displayed, as the Order
165+
// is fully refunded.
166+
$I->dontSeeElementInDOM('a#ckwc_sync_past_orders');
167+
}
168+
138169
/**
139170
* Test that a button is displayed on the Integration Settings screen
140171
* when:

tests/acceptance/sync-past-orders/SyncPastOrdersHPOSCest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,37 @@ public function testNoButtonDisplayedWhenNoPastOrders(AcceptanceTester $I)
139139
$I->dontSeeElementInDOM('a#ckwc_sync_past_orders');
140140
}
141141

142+
/**
143+
* Test that no button is displayed on the Integration Settings screen
144+
* when:
145+
* - the Integration is enabled,
146+
* - valid API credentials are specified,
147+
* - a WooCommerce Order exists, that has been refunded.
148+
*
149+
* @since 1.9.2
150+
*
151+
* @param AcceptanceTester $I Tester.
152+
*/
153+
public function testSyncPastOrderExcludesRefunds(AcceptanceTester $I)
154+
{
155+
// Delete all existing WooCommerce Orders from the database.
156+
$I->wooCommerceDeleteAllOrders($I);
157+
158+
// Create Product and Checkout for this test, sending the Order
159+
// to Kit.
160+
$result = $I->wooCommerceCreateProductAndCheckoutWithConfig($I);
161+
162+
// Refund the Order.
163+
$I->wooCommerceRefundOrder($I, $result['order_id'], 10);
164+
165+
// Load Settings screen.
166+
$I->loadConvertKitSettingsScreen($I);
167+
168+
// Confirm that no Sync Past Order button is displayed, as the Order
169+
// is fully refunded.
170+
$I->dontSeeElementInDOM('a#ckwc_sync_past_orders');
171+
}
172+
142173
/**
143174
* Test that a button is displayed on the Integration Settings screen
144175
* when:

views/backend/settings/sync-past-orders.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<h2><?php echo esc_html( $this->get_method_title() ); ?></h2>
1414

1515
<p>
16-
<?php esc_html_e( 'Do not navigate away from this page until the process is completed, otherwise complete purchase data will not be sent to ConvertKit. You will be notified via this page when the process is completed.', 'woocommerce-convertkit' ); ?>
16+
<?php esc_html_e( 'Do not navigate away from this page until the process is completed, otherwise complete purchase data will not be sent to Kit. You will be notified via this page when the process is completed.', 'woocommerce-convertkit' ); ?>
1717
</p>
1818

1919
<!-- Progress Bar -->

0 commit comments

Comments
 (0)