Automations
1 year ago
KeyCheck
1 year ago
SendingQueue
4 days ago
StatsNotifications
2 months ago
AuthorizedSendingEmailsCheck.php
3 years ago
BackfillEngagementData.php
1 year ago
Bounce.php
4 days ago
BounceTaskSubscribersCleanup.php
1 month ago
BulkConfirmationEmailResend.php
2 months ago
ExportFilesCleanup.php
2 months ago
InactiveSubscribersMaintenance.php
2 weeks ago
LogCleanup.php
10 months ago
Mixpanel.php
9 months ago
NewsletterTemplateThumbnails.php
1 year ago
ReEngagementEmailsScheduler.php
1 year ago
Scheduler.php
2 months ago
SendingQueueBodyCleanup.php
2 months ago
SendingTaskSubscribersCleanup.php
2 months ago
SimpleWorker.php
1 year ago
StatisticsExport.php
2 months ago
SubscriberLimitNotificationWorker.php
2 months ago
SubscriberLinkTokens.php
1 year ago
SubscribersCountCacheRecalculation.php
2 weeks ago
SubscribersEngagementScore.php
3 days ago
SubscribersLastEngagement.php
2 months ago
SubscribersSegmentsCountSync.php
2 weeks ago
SubscribersStatsReport.php
1 year ago
Tracks.php
9 months ago
UnconfirmedSubscribersCleanup.php
2 months ago
UnsubscribeTokens.php
1 month ago
WooCommercePastOrders.php
1 year ago
WooCommerceSync.php
3 years ago
WorkersFactory.php
2 weeks ago
index.php
3 years ago
WooCommerceSync.php
75 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\ScheduledTaskEntity; |
| 9 | use MailPoet\Segments\WooCommerce as WooCommerceSegment; |
| 10 | use MailPoet\WooCommerce\Helper as WooCommerceHelper; |
| 11 | |
| 12 | class WooCommerceSync extends SimpleWorker { |
| 13 | const TASK_TYPE = 'woocommerce_sync'; |
| 14 | const SUPPORT_MULTIPLE_INSTANCES = false; |
| 15 | const AUTOMATIC_SCHEDULING = false; |
| 16 | const BATCH_SIZE = 1000; |
| 17 | |
| 18 | /** @var WooCommerceSegment */ |
| 19 | private $woocommerceSegment; |
| 20 | |
| 21 | /** @var WooCommerceHelper */ |
| 22 | private $woocommerceHelper; |
| 23 | |
| 24 | public function __construct( |
| 25 | WooCommerceSegment $woocommerceSegment, |
| 26 | WooCommerceHelper $woocommerceHelper |
| 27 | ) { |
| 28 | $this->woocommerceSegment = $woocommerceSegment; |
| 29 | $this->woocommerceHelper = $woocommerceHelper; |
| 30 | parent::__construct(); |
| 31 | } |
| 32 | |
| 33 | public function checkProcessingRequirements() { |
| 34 | return $this->woocommerceHelper->isWooCommerceActive(); |
| 35 | } |
| 36 | |
| 37 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer) { |
| 38 | $meta = $task->getMeta(); |
| 39 | $highestOrderId = $this->getHighestOrderId(); |
| 40 | |
| 41 | if (!isset($meta['last_checked_order_id'])) { |
| 42 | $meta['last_checked_order_id'] = 0; |
| 43 | } |
| 44 | |
| 45 | do { |
| 46 | $this->cronHelper->enforceExecutionLimit($timer); |
| 47 | $meta['last_checked_order_id'] = $this->woocommerceSegment->synchronizeCustomers( |
| 48 | $meta['last_checked_order_id'], |
| 49 | $highestOrderId, |
| 50 | self::BATCH_SIZE |
| 51 | ); |
| 52 | $task->setMeta($meta); |
| 53 | $this->scheduledTasksRepository->persist($task); |
| 54 | $this->scheduledTasksRepository->flush(); |
| 55 | } while ($meta['last_checked_order_id'] < $highestOrderId); |
| 56 | |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | private function getHighestOrderId(): int { |
| 61 | $orders = $this->woocommerceHelper->wcGetOrders( |
| 62 | [ |
| 63 | 'status' => 'all', |
| 64 | 'type' => 'shop_order', |
| 65 | 'limit' => 1, |
| 66 | 'orderby' => 'ID', |
| 67 | 'order' => 'DESC', |
| 68 | 'return' => 'ids', |
| 69 | ] |
| 70 | ); |
| 71 | |
| 72 | return (!empty($orders)) ? $orders[0] : 0; |
| 73 | } |
| 74 | } |
| 75 |