CollectionSyncService.php
1 year ago
CustomerSyncService.php
1 year ago
PostSyncService.php
1 year ago
ProductSyncService.php
1 year ago
ProductsSyncProcess.php
1 year ago
ProductsSyncService.php
1 year ago
StoreSyncService.php
1 year ago
SyncService.php
1 year ago
SyncServiceProvider.php
1 year ago
ProductsSyncProcess.php
86 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Sync; |
| 4 | |
| 5 | use SureCart\Background\BackgroundProcess; |
| 6 | use SureCart\Models\Product; |
| 7 | |
| 8 | /** |
| 9 | * This process fetches and queues all products for syncing. |
| 10 | */ |
| 11 | class ProductsSyncProcess extends BackgroundProcess { |
| 12 | /** |
| 13 | * The prefix for the action. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $prefix = 'surecart'; |
| 18 | |
| 19 | /** |
| 20 | * The action. |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $action = 'queue_products'; |
| 25 | |
| 26 | /** |
| 27 | * Perform task with queued item. |
| 28 | * |
| 29 | * Override this method to perform any actions required on each |
| 30 | * queue item. Return the modified item for further processing |
| 31 | * in the next pass through. Or, return false to remove the |
| 32 | * item from the queue. |
| 33 | * |
| 34 | * @param mixed $args Queue item to iterate over. |
| 35 | * |
| 36 | * @return mixed |
| 37 | */ |
| 38 | protected function task( $args ) { |
| 39 | // the current page. |
| 40 | $page = $args['page'] ?? 1; |
| 41 | |
| 42 | // get the items (uncached). |
| 43 | $products = Product::where( [ 'cached' => false ] )::paginate( |
| 44 | [ |
| 45 | 'page' => $page, |
| 46 | 'per_page' => $args['batch_size'] ?? 25, |
| 47 | ] |
| 48 | ); |
| 49 | |
| 50 | if ( is_wp_error( $products ) ) { |
| 51 | error_log( $products->get_error_message() ); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | // add each item to the queue. |
| 56 | foreach ( $products->data as $product ) { |
| 57 | $product->queueSync( true ); // sync with notice. |
| 58 | } |
| 59 | |
| 60 | // we have more to process. |
| 61 | if ( $products->hasNextPage() ) { |
| 62 | return [ |
| 63 | 'page' => $products->pagination->page + 1, |
| 64 | 'batch_size' => $args['batch_size'] ?? 25, |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | // nothing more to process. |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Complete processing. |
| 74 | * |
| 75 | * Override if applicable, but ensure that the below actions are |
| 76 | * performed, or, call parent::complete(). |
| 77 | */ |
| 78 | protected function complete() { |
| 79 | // kick off the queue process immediately (instead of waiting for the next scheduled run). |
| 80 | \SureCart::queue()->run(); |
| 81 | |
| 82 | // call the parent complete method. |
| 83 | parent::complete(); |
| 84 | } |
| 85 | } |
| 86 |