Skip to content

Commit 27b5fd4

Browse files
committed
Fix variant save not updating product default data
1 parent 77ac878 commit 27b5fd4

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/elements/Variant.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use craft\elements\conditions\ElementConditionInterface;
3131
use craft\gql\types\DateTime;
3232
use craft\helpers\ArrayHelper;
33+
use craft\helpers\Db;
3334
use craft\helpers\Html;
3435
use craft\models\FieldLayout;
3536
use Throwable;
@@ -1006,6 +1007,19 @@ public function afterSave(bool $isNew): void
10061007
}
10071008

10081009
$record->save(false);
1010+
1011+
// Fallback updating of default variant data if the variant is being saved in isolation
1012+
if ($this->duplicateOf === null && $this->isDefault) {
1013+
Db::update(Table::PRODUCTS, [
1014+
'defaultVariantId' => $this->id,
1015+
'defaultSku' => $this->getSkuAsText(),
1016+
'defaultPrice' => $this->price ?? 0.0,
1017+
'defaultHeight' => $this->height ?? 0,
1018+
'defaultLength' => $this->length ?? 0,
1019+
'defaultWidth' => $this->width ?? 0,
1020+
'defaultWeight' => $this->weight ?? 0,
1021+
], ['id' => $this->productId]);
1022+
}
10091023
}
10101024

10111025
parent::afterSave($isNew);

0 commit comments

Comments
 (0)