Sync subscribers sequentially from orders
[MAILPOET-3954]
This commit is contained in:
@@ -3,9 +3,9 @@
|
||||
namespace MailPoet\Cron\Workers;
|
||||
|
||||
use MailPoet\Entities\ScheduledTaskEntity;
|
||||
use MailPoet\Segments\WooCommerce;
|
||||
use MailPoet\Segments\WooCommerce as WooCommerceSegment;
|
||||
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
|
||||
use MailPoetVendor\Doctrine\DBAL\Connection;
|
||||
|
||||
class WooCommerceSync extends SimpleWorker {
|
||||
const TASK_TYPE = 'woocommerce_sync';
|
||||
@@ -18,12 +18,17 @@ class WooCommerceSync extends SimpleWorker {
|
||||
/** @var WooCommerceHelper */
|
||||
private $woocommerceHelper;
|
||||
|
||||
/** @var Connection */
|
||||
private $connection;
|
||||
|
||||
public function __construct(
|
||||
WooCommerceSegment $woocommerceSegment,
|
||||
WooCommerceHelper $woocommerceHelper
|
||||
WooCommerceHelper $woocommerceHelper,
|
||||
Connection $connection
|
||||
) {
|
||||
$this->woocommerceSegment = $woocommerceSegment;
|
||||
$this->woocommerceHelper = $woocommerceHelper;
|
||||
$this->connection = $connection;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
@@ -32,15 +37,29 @@ class WooCommerceSync extends SimpleWorker {
|
||||
}
|
||||
|
||||
public function processTaskStrategy(ScheduledTaskEntity $task, $timer) {
|
||||
$countOfSynchronized = $task->getMeta()['count_of_synchronized'] ?? 0;
|
||||
$count = $this->woocommerceSegment->synchronizeCustomers($countOfSynchronized);
|
||||
$lastProcessedOrderId = $task->getMeta()['last_processed_order_id'] ?? 0;
|
||||
$highestOrderId = $this->getHighestOrderId();
|
||||
|
||||
$countOfSynchronized += $count;
|
||||
$task->setMeta(['count_of_synchronized' => $countOfSynchronized]);
|
||||
$lastProcessedOrderId = $this->woocommerceSegment->synchronizeCustomers($lastProcessedOrderId, $highestOrderId);
|
||||
|
||||
$meta = $task->getMeta() ?? [];
|
||||
$meta['last_processed_order_id'] = $lastProcessedOrderId;
|
||||
$task->setMeta($meta);
|
||||
$this->scheduledTasksRepository->persist($task);
|
||||
$this->scheduledTasksRepository->flush();
|
||||
if ($count === WooCommerce::BATCH_SIZE) {
|
||||
|
||||
if ($lastProcessedOrderId !== $highestOrderId) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private function getHighestOrderId(): int {
|
||||
global $wpdb;
|
||||
return (int)$this->connection->fetchOne("
|
||||
SELECT MAX(wpp.ID)
|
||||
FROM {$wpdb->posts} wpp
|
||||
WHERE wpp.post_type = 'shop_order'
|
||||
");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user