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

Refacto account migration, now use multiple as schedule action for ea… #48

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 57 additions & 11 deletions src/Admin/WoocommerceActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ function ( $order_id, $message ) {
*/
add_action( 'sf_migrate_single_action', array( $this, 'migrate_old_accounts' ) );

/**
* Divide orders query in batches of 10 for migration
*/
add_action( 'sf_migrate_orders_add_default_account', [ $this, 'migrate_orders_batch' ] );

/**
* Orders Statuses Mapping
* Each status with action
Expand Down Expand Up @@ -437,19 +442,52 @@ public function migrate_old_accounts() {
return false;
}

//Migrate old orders to default account
$args = array(
'limit' => - 1,
'meta_key' => Query::WC_META_SF_REFERENCE,
'meta_compare' => 'EXISTS',
// Add WC schedule action for orders account migration, batch of 10
as_schedule_single_action(
time() + 3,
'sf_migrate_orders_add_default_account',
[
$account_id
],
'sf_migrate_single'
);

$query = new \WC_Order_Query( $args );
return true;
}

/**
* Run orders meta data migration, by batches of 10
*
* @param int $account_id
*
* @throws Exception
*/
public function migrate_orders_batch( $account_id ) {
// Get SF orders that are not yet migrated, by batches of 10
$args = [
'limit' => 10,
'meta_query' => [
'relation' => 'AND',
[
'meta_key' => Query::WC_META_SF_REFERENCE,
'meta_compare' => 'EXISTS',
],
[
'meta_key' => Query::WC_META_SF_STORE_ID,
'meta_compare' => 'NOT EXISTS',
]
],
];

$query = new \WC_Order_Query( $args );

$wc_orders = $query->get_orders();

// If no order, log the info
if ( empty( $wc_orders ) ) {
ShoppingFeedHelper::get_logger()->error(
ShoppingFeedHelper::get_logger()->info(
sprintf(
__( 'No SF orders founds for migration', 'shopping-feed' )
__( 'No more SF orders founds for migration, ending migration script.', 'shopping-feed' )
),
array(
'source' => 'shopping-feed',
Expand All @@ -460,15 +498,23 @@ public function migrate_old_accounts() {
return false;
}

// Add the new meta to every found order
foreach ( $wc_orders as $wc_order ) {
//add store id
// Add store id
/** @var \WC_Order $wc_order */
$wc_order->add_meta_data( Query::WC_META_SF_STORE_ID, $account_id );
$wc_order->save();
}

ShoppingFeedHelper::end_upgrade();
// Add schedule action on self to process next 10 orders, until completion
as_schedule_single_action(
time() + 3,
'sf_migrate_orders_add_default_account',
[
$account_id
],
'sf_migrate_single'
);

return true;
}
}