Skip to content

Commit 3c0adc4

Browse files
committed
Move resave to job
1 parent e26823e commit 3c0adc4

File tree

3 files changed

+87
-11
lines changed

3 files changed

+87
-11
lines changed

CHANGELOG-WIP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- Added `craft\commerce\models\Email::$renderSiteId`.
3838
- Added `craft\commerce\models\Email::getRenderSite()`.
3939
- Added `craft\commerce\models\Pdf::$linkExpiry`.
40+
- Added `craft\commerce\queue\jobs\ResaveProductVariants`.
4041
- Added `craft\commerce\records\Email::$renderSiteId`.
4142
- Added `craft\commerce\records\Order::$dateFirstPaid`.
4243
- Added `craft\commerce\services\CatalogPricingRules::hasCatalogPricingRules()`.

src/elements/Product.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,17 +1665,32 @@ public function afterSave(bool $isNew): void
16651665

16661666
$this->id = $record->id;
16671667

1668-
if ($this->getIsCanonical() && isset($this->typeId) && $productType->isStructure) {
1668+
if ($this->getIsCanonical() &&
1669+
isset($this->typeId) &&
1670+
$productType->isStructure
1671+
) {
16691672
// Has the parent changed?
16701673
if ($this->hasNewParent()) {
16711674
$this->_placeInStructure($isNew, $productType);
16721675
}
16731676

1674-
// Update the products descendants, who may be using this products URI in their own URIs
1677+
// Update the product's descendants, who may be using this product's URI in their own URIs
16751678
if (!$isNew) {
16761679
Craft::$app->getElements()->updateDescendantSlugsAndUris($this, true, true);
16771680
}
16781681
}
1682+
1683+
// Queue job to resave variants if the variant title format references the product
1684+
if ($this->getIsCanonical() &&
1685+
isset($this->typeId) &&
1686+
!$productType->hasVariantTitleField &&
1687+
$productType->variantTitleFormat &&
1688+
StringHelper::containsAny($productType->variantTitleFormat, ['product.', 'owner.', 'primaryOwner.'])
1689+
) {
1690+
Craft::$app->getQueue()->push(new \craft\commerce\queue\jobs\ResaveProductVariants([
1691+
'productId' => $this->id,
1692+
]));
1693+
}
16791694
}
16801695

16811696
parent::afterSave($isNew);
@@ -1868,15 +1883,6 @@ public function getFieldLayout(): ?FieldLayout
18681883
*/
18691884
public function beforeSave(bool $isNew): bool
18701885
{
1871-
$productType = $this->getType();
1872-
1873-
if (!$productType->hasVariantTitleField &&
1874-
$productType->variantTitleFormat &&
1875-
StringHelper::containsAny($productType->variantTitleFormat, ['product.', 'owner.', 'primaryOwner.'])
1876-
) {
1877-
$this->setDirtyAttributes(['allVariants'], true);
1878-
}
1879-
18801886
// Make sure the entry has at least one revision if the section has versioning enabled
18811887
if ($this->_shouldSaveRevision()) {
18821888
$hasRevisions = self::find()
@@ -1900,6 +1906,7 @@ public function beforeSave(bool $isNew): bool
19001906
}
19011907
}
19021908

1909+
$productType = $this->getType();
19031910
// Set the structure ID for Element::attributes() and afterSave()
19041911
if ($productType->isStructure) {
19051912
$this->structureId = $productType->structureId;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* @link https://craftcms.com/
4+
* @copyright Copyright (c) Pixel & Tonic, Inc.
5+
* @license https://craftcms.github.io/license/
6+
*/
7+
8+
namespace craft\commerce\queue\jobs;
9+
10+
use craft\commerce\elements\Product;
11+
use craft\commerce\elements\Variant;
12+
use craft\queue\BaseJob;
13+
14+
/**
15+
* Resave Product Variants queue job
16+
*
17+
* @author Pixel & Tonic, Inc. <[email protected]>
18+
* @since 5.5.0
19+
*/
20+
class ResaveProductVariants extends BaseJob
21+
{
22+
/**
23+
* @var int The product ID whose variants should be resaved
24+
*/
25+
public int $productId;
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function execute($queue): void
31+
{
32+
$product = Product::find()
33+
->id($this->productId)
34+
->one();
35+
36+
if (!$product) {
37+
return;
38+
}
39+
40+
$variants = Variant::find()
41+
->productId($this->productId)
42+
->status(null)
43+
->all();
44+
45+
$total = count($variants);
46+
47+
foreach ($variants as $i => $variant) {
48+
$this->setProgress($queue, $i / $total);
49+
\Craft::$app->getElements()->saveElement($variant);
50+
}
51+
}
52+
53+
/**
54+
* @inheritdoc
55+
*/
56+
protected function defaultDescription(): ?string
57+
{
58+
$product = Product::find()
59+
->id($this->productId)
60+
->one();
61+
62+
if ($product) {
63+
return 'Resaving variants for product: ' . $product->title;
64+
}
65+
66+
return 'Resaving product variants';
67+
}
68+
}

0 commit comments

Comments
 (0)