Skip to content

Commit

Permalink
Updating bulk loaders to support Products instead of Variants
Browse files Browse the repository at this point in the history
  • Loading branch information
markmiddleton committed Jan 29, 2024
1 parent 3c38eb7 commit 0937e94
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/jobs/AlgoliaBulkLoadTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function execute($queue)

break;

CASE 'variant':
CASE 'product':

// loading too many causes a timeout and memory issue...
// breaking these updates into 100 record chunks
Expand All @@ -142,13 +142,13 @@ public function execute($queue)
if ($commercePlugin) {

// this is the total number of variants in the system
$variantCount = \craft\commerce\elements\Variant::find()->typeId($sectionId)->count();
$productCount = \craft\commerce\elements\Product::find()->typeId($sectionId)->count();

AlgoliaSync::$plugin->algoliaSyncService->logger("there are ".$variantCount." products to load who match sectionId: ".$sectionId, basename(__FILE__) , __LINE__);
AlgoliaSync::$plugin->algoliaSyncService->logger("there are ".$productCount." products to load who match sectionId: ".$sectionId, basename(__FILE__) , __LINE__);

$queue = Craft::$app->getQueue();

for ($x=0; $x<$variantCount; $x=$x+$this->standardLimit) {
for ($x=0; $x<$productCount; $x=$x+$this->standardLimit) {
$queue->push(new AlgoliaChunkLoadTask([
'description' => Craft::t('algolia-sync', 'Queueing a chunk of '.$elementType.' records to process start ('.$x.') limit ('.$this->standardLimit.')'),
'loadRecordType' => $this->loadRecordType,
Expand Down
14 changes: 6 additions & 8 deletions src/jobs/AlgoliaChunkLoadTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,26 @@ public function execute($queue)

SWITCH ($elementType) {

CASE 'variant':
CASE 'product':

AlgoliaSync::$plugin->algoliaSyncService->logger("loading a chunk of product variants (with type ID = ".$sectionId.") into the Algolia sync queue. OffsetCount=".$offsetCount.", limitCount=".$limitCount, basename(__FILE__) , __LINE__);
AlgoliaSync::$plugin->algoliaSyncService->logger("loading a chunk of products (with type ID = ".$sectionId.") into the Algolia sync queue. OffsetCount=".$offsetCount.", limitCount=".$limitCount, basename(__FILE__) , __LINE__);

$commercePlugin = Craft::$app->plugins->getPlugin('commerce');

if ($commercePlugin) {

$recordCount = \craft\commerce\elements\Variant::find()->typeId($sectionId)->offset($offsetCount)->limit($limitCount)->status('enabled')->count();
$variants = \craft\commerce\elements\Variant::find()->typeId($sectionId)->offset($offsetCount)->limit($limitCount)->status('enabled')->all();
$recordCount = \craft\commerce\elements\Product::find()->typeId($sectionId)->offset($offsetCount)->limit($limitCount)->status('enabled')->count();
$products = \craft\commerce\elements\Product::find()->typeId($sectionId)->offset($offsetCount)->limit($limitCount)->status('enabled')->all();

if ($recordCount > 0) {
AlgoliaSync::$plugin->algoliaSyncService->logger("We found ".$recordCount." products that need to be synced in this batch to be synced", basename(__FILE__) , __LINE__);

$currentLoopCount = 0;
foreach ($variants as $variant) {
foreach ($products as $product) {
$progress = $currentLoopCount / $recordCount;
$this->setProgress($queue, $progress);

// AlgoliaSync::$plugin->algoliaSyncService->logger(print_r($variant, true), basename(__FILE__) , __LINE__);

AlgoliaSync::$plugin->algoliaSyncService->prepareAlgoliaSyncElement($variant);
AlgoliaSync::$plugin->algoliaSyncService->prepareAlgoliaSyncElement($product);
$currentLoopCount++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/AlgoliaSyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ public function prepareAlgoliaSyncElement($element, $action = 'save', $algoliaMe
$variantInfo['weight'] = (float)$variantDetails->weight;
$variantInfo['hasUnlimitedStock'] = (bool)$variantDetails->hasUnlimitedStock;
$variantInfo['minQty'] = (int)$variantDetails->minQty;
$variantInfo['minQty'] = (int)$variantDetails->minQty;
$variantInfo['maxQty'] = (int)$variantDetails->maxQty;

// nest each variant under the product info
$recordUpdate['attributes']['variants'][] = $variantInfo;
Expand Down

0 comments on commit 0937e94

Please sign in to comment.