Skip to content

Commit

Permalink
Merge pull request #201 from ConvertKit/fix-sync-past-orders-hpos-dis…
Browse files Browse the repository at this point in the history
…abled

Fix Sync Past Orders when HPOS Disabled
  • Loading branch information
n7studios authored Nov 16, 2024
2 parents 67a9fa6 + 078d7fe commit 5df8c6e
Show file tree
Hide file tree
Showing 6 changed files with 557 additions and 43 deletions.
2 changes: 1 addition & 1 deletion admin/class-ckwc-admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function sync_past_orders() {
/* translators: %1$s: WooCommerce Order ID, %2$s: ConvertKit API Purchase ID */
__( 'WooCommerce Order ID #%1$s added to Kit Purchase Data successfully. Kit Purchase ID: #%2$s', 'woocommerce-convertkit' ),
$id,
$result['purchase']['id']
get_post_meta( $id, 'ckwc_purchase_data_id', true )
)
);

Expand Down
85 changes: 62 additions & 23 deletions includes/class-ckwc-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,35 +659,74 @@ public function get_orders_not_sent_to_convertkit() {
break;
}

// Run query to fetch Order IDs whose Purchase Data has not been sent to ConvertKit.
$query = new WC_Order_Query(
array(
'limit' => -1,
// Get Orders based on whether HPOS is enabled.
switch ( get_option( 'woocommerce_custom_orders_table_enabled' ) ) {
case 'no':
// Query CPT.
// We can't use WC_Order_Quey with a meta_query when HPOS is disabled:
// https://github.com/woocommerce/woocommerce/pull/47457.
$query = new WP_Query(
array(
// Return posts of type `shop_order`.
'post_type' => 'shop_order',
'posts_per_page' => -1,

// Only include Orders that do not match the Purchase Data Event integration setting.
'post_status' => $post_statuses,

// Only include Orders that do not have a ConvertKit Purchase Data ID.
'meta_query' => array(
array(
'key' => 'ckwc_purchase_data_id',
'compare' => 'NOT EXISTS',
),
),

// Only return Order IDs.
'fields' => 'ids',
)
);

// If no Orders exist that have not had their Purchase Data sent to ConvertKit,
// return false.
if ( empty( $query->posts ) ) {
return false;
}

// Only include Orders that do not match the Purchase Data Event integration setting.
'status' => $post_statuses,
// Return the array of Order IDs.
return $query->posts;

// Only include Orders that do not have a ConvertKit Purchase Data ID.
'meta_query' => array(
default:
// Query HPOS.
$query = new WC_Order_Query(
array(
'key' => $this->purchase_data_id_meta_key,
'compare' => 'NOT EXISTS',
),
),
'limit' => -1,

// Only return Order IDs.
'return' => 'ids',
)
);
// Only include Orders that do not match the Purchase Data Event integration setting.
'status' => $post_statuses,

// If no Orders exist that have not had their Purchase Data sent to ConvertKit,
// return false.
if ( empty( $query->get_orders() ) ) {
return false;
}
// Only include Orders that do not have a ConvertKit Purchase Data ID.
'meta_query' => array(
array(
'key' => 'ckwc_purchase_data_id',
'compare' => 'NOT EXISTS',
),
),

// Only return Order IDs.
'return' => 'ids',
)
);

// If no Orders exist that have not had their Purchase Data sent to ConvertKit,
// return false.
if ( empty( $query->get_orders() ) ) {
return false;
}

// Return the array of Order IDs.
return $query->get_orders();
// Return the array of Order IDs.
return $query->get_orders();
}

}

Expand Down
13 changes: 5 additions & 8 deletions tests/acceptance/sync-past-orders/SyncPastOrdersCLICest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/**
* Tests for Sync Past Orders functionality using WP-CLI
*
* @since 1.7.1
* @since 1.9.0
*/
class SyncPastOrdersCLICest
{
/**
* Run common actions before running the test functions in this class.
*
* @since 1.7.1
* @since 1.9.0
*
* @param AcceptanceTester $I Tester.
*/
Expand All @@ -18,9 +18,6 @@ public function _before(AcceptanceTester $I)
// Activate Plugin.
$I->activateWooCommerceAndConvertKitPlugins($I);

// Enable HPOS.
$I->setupWooCommerceHPOS($I);

// Setup WooCommerce Plugin.
$I->setupWooCommercePlugin($I);

Expand All @@ -42,7 +39,7 @@ public function _before(AcceptanceTester $I)
* attempting to sync past orders to ConvertKit Purchase Data, and no
* WooCommerce Orders exist.
*
* @since 1.7.1
* @since 1.9.0
*
* @param AcceptanceTester $I Tester.
*/
Expand All @@ -57,7 +54,7 @@ public function testSyncPastOrdersWhenNoOrdersExist(AcceptanceTester $I)
* attempting to sync past orders to ConvertKit Purchase Data, and
* WooCommerce Orders exist.
*
* @since 1.7.1
* @since 1.9.0
*
* @param AcceptanceTester $I Tester.
*/
Expand All @@ -84,7 +81,7 @@ public function testSyncPastOrders(AcceptanceTester $I)
* attempting to sync past orders to ConvertKit Purchase Data using
* the --limit argument, and WooCommerce Orders exist.
*
* @since 1.7.1
* @since 1.9.0
*
* @param AcceptanceTester $I Tester.
*/
Expand Down
19 changes: 8 additions & 11 deletions tests/acceptance/sync-past-orders/SyncPastOrdersCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/**
* Tests for Sync Past Orders functionality.
*
* @since 1.4.3
* @since 1.9.0
*/
class SyncPastOrdersCest
{
/**
* Run common actions before running the test functions in this class.
*
* @since 1.4.3
* @since 1.9.0
*
* @param AcceptanceTester $I Tester.
*/
Expand All @@ -18,9 +18,6 @@ public function _before(AcceptanceTester $I)
// Activate Plugin.
$I->activateWooCommerceAndConvertKitPlugins($I);

// Enable HPOS.
$I->setupWooCommerceHPOS($I);

// Setup WooCommerce Plugin.
$I->setupWooCommercePlugin($I);

Expand All @@ -35,7 +32,7 @@ public function _before(AcceptanceTester $I)
* Test that no button is displayed on the Integration Settings screen
* when the Integration is disabled.
*
* @since 1.4.3
* @since 1.9.0
*
* @param AcceptanceTester $I Tester.
*/
Expand Down Expand Up @@ -87,7 +84,7 @@ public function testNoButtonDisplayedWhenIntegrationDisabled(AcceptanceTester $I
* when the Integration is enabled, valid API credentials are specified
* but there are no WooCommerce Orders.
*
* @since 1.4.3
* @since 1.9.0
*
* @param AcceptanceTester $I Tester.
*/
Expand All @@ -113,7 +110,7 @@ public function testNoButtonDisplayedWhenNoOrders(AcceptanceTester $I)
* - valid API credentials are specified,
* - a WooCommerce Order exists, that has had its Purchase Data sent to ConvertKit.
*
* @since 1.4.3
* @since 1.9.0
*
* @param AcceptanceTester $I Tester.
*/
Expand Down Expand Up @@ -146,7 +143,7 @@ public function testNoButtonDisplayedWhenNoPastOrders(AcceptanceTester $I)
* - a WooCommerce Order exists, that has not had its Purchase Data sent to ConvertKit,
* - clicking the button sends the Order to ConvertKit.
*
* @since 1.4.3
* @since 1.9.0
*
* @param AcceptanceTester $I Tester.
*/
Expand Down Expand Up @@ -217,7 +214,7 @@ public function testSyncPastOrder(AcceptanceTester $I)
* meta key to the Order, with a value of 'yes' - however did not store the ConvertKit Purchase Data API response's
* Transaction ID in the Order, meaning there is no way to potentially map WooCommerce Orders to ConvertKit API data.
*
* 1.4.3 and later performs the same as 1.4.2, but also storing the ConvertKit Transaction ID in the 'ckwc_purchase_data_id',
* 1.9.0 and later performs the same as 1.4.2, but also storing the ConvertKit Transaction ID in the 'ckwc_purchase_data_id',
* allowing for the possibility of future mapping between WooCommerce and ConvertKit.
*
* This test ensures that a 1.4.2 or older Order, which was already sent to ConvertKit, will be sent again so that
Expand Down Expand Up @@ -287,7 +284,7 @@ public function testSyncPastOrderCreatedInPreviousPluginVersion(AcceptanceTester
* is not sent to ConvertKit when attempting to access the Sync Past Orders screen
* and the API credentials are invalid.
*
* @since 1.4.3
* @since 1.9.0
*
* @param AcceptanceTester $I Tester.
*/
Expand Down
131 changes: 131 additions & 0 deletions tests/acceptance/sync-past-orders/SyncPastOrdersHPOSCLICest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* Tests for Sync Past Orders functionality using WP-CLI with WooCommerce's
* High Performance Order Storage (HPOS) system.
*
* @since 1.7.1
*/
class SyncPastOrdersHPOSCLICest
{
/**
* Run common actions before running the test functions in this class.
*
* @since 1.7.1
*
* @param AcceptanceTester $I Tester.
*/
public function _before(AcceptanceTester $I)
{
// Activate Plugin.
$I->activateWooCommerceAndConvertKitPlugins($I);

// Enable HPOS.
$I->setupWooCommerceHPOS($I);

// Setup WooCommerce Plugin.
$I->setupWooCommercePlugin($I);

// Setup ConvertKit for WooCommerce Plugin.
$I->setupConvertKitPlugin($I);

// Activate Custom Order Numbers Plugin.
$I->activateThirdPartyPlugin($I, 'custom-order-numbers-for-woocommerce');

// Setup Custom Order Numbers Plugin.
$I->setupCustomOrderNumbersPlugin($I);

// Delete all existing WooCommerce Orders from the database.
$I->wooCommerceDeleteAllOrders($I);
}

/**
* Test that the CLI command returns the expected console output when
* attempting to sync past orders to ConvertKit Purchase Data, and no
* WooCommerce Orders exist.
*
* @since 1.7.1
*
* @param AcceptanceTester $I Tester.
*/
public function testSyncPastOrdersWhenNoOrdersExist(AcceptanceTester $I)
{
$I->cli([ 'ckwc-sync-past-orders' ]);
$I->seeInShellOutput('No outstanding Orders to send to Kit');
}

/**
* Test that the CLI command returns the expected console output when
* attempting to sync past orders to ConvertKit Purchase Data, and
* WooCommerce Orders exist.
*
* @since 1.7.1
*
* @param AcceptanceTester $I Tester.
*/
public function testSyncPastOrders(AcceptanceTester $I)
{
// Create Product and Checkout for this test, not sending the Order
// to ConvertKit.
$result = $I->wooCommerceCreateProductAndCheckoutWithConfig($I);

// Remove prefix from Order ID, as CLI will not show the Custom Order Number Prefix.
$orderIDParts = explode( '-', $result['order_id'] );
$orderID = $orderIDParts[ count($orderIDParts) - 1 ];

// Run CLI command.
$I->cli([ 'ckwc-sync-past-orders' ]);
$I->seeInShellOutput('WooCommerce Order ID #' . $orderID . ' added to Kit Purchase Data successfully. Kit Purchase ID: #');

// Confirm that the Order was added to ConvertKit.
$I->apiCheckPurchaseExists($I, $result['order_id'], $result['email_address'], $result['product_id']);
}

/**
* Test that the CLI command returns the expected console output when
* attempting to sync past orders to ConvertKit Purchase Data using
* the --limit argument, and WooCommerce Orders exist.
*
* @since 1.7.1
*
* @param AcceptanceTester $I Tester.
*/
public function testSyncPastOrdersWithLimitArgument(AcceptanceTester $I)
{
// Create Product and Checkout for this test, not sending the Order
// to ConvertKit.
$results = [
$I->wooCommerceCreateProductAndCheckoutWithConfig($I),
$I->wooCommerceCreateProductAndCheckoutWithConfig($I),
];

// Run CLI command with --limit=1 to send each Order individually.
foreach (array_reverse($results) as $result) {
// Remove prefix from Order ID, as CLI will not show the Custom Order Number Prefix.
$orderIDParts = explode( '-', $result['order_id'] );
$orderID = $orderIDParts[ count($orderIDParts) - 1 ];

// Run CLI command.
$I->cli([ 'ckwc-sync-past-orders', '--limit=1' ]);
$I->seeInShellOutput('WooCommerce Order ID #' . $orderID . ' added to Kit Purchase Data successfully. Kit Purchase ID: #');

// Confirm that the Order was added to ConvertKit.
$I->apiCheckPurchaseExists($I, $result['order_id'], $result['email_address'], $result['product_id']);
}
}

/**
* Deactivate and reset Plugin(s) after each test, if the test passes.
* We don't use _after, as this would provide a screenshot of the Plugin
* deactivation and not the true test error.
*
* @since 1.4.4
*
* @param AcceptanceTester $I Tester.
*/
public function _passed(AcceptanceTester $I)
{
$I->deactivateThirdPartyPlugin($I, 'custom-order-numbers-for-woocommerce');
$I->deactivateWooCommerceAndConvertKitPlugins($I);
$I->resetConvertKitPlugin($I);
}
}
Loading

0 comments on commit 5df8c6e

Please sign in to comment.