Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Unreleased

- Fixed a bug where the deprecated `craft\commerce\services\ProductTypes::getEditableProductTypes()` method could still be called. ([#4349](https://github.com/craftcms/commerce/issues/4349))
- Fixed a bug where the deprecated `craft\commerce\services\ProductTypes::getEditableProductTypes()` method could still be called. ([#4349](https://github.com/craftcms/commerce/issues/4349))
- Fixed a bug where a variant's `skuFormat` containing `{id}` would generate a SKU missing that value for new variants.
- Fixed a bug where adding a site during a project config apply could rewrite `project.yaml`. ([#4348](https://github.com/craftcms/commerce/issues/4348))

## 5.7.2 - 2026-08-12
Expand Down
18 changes: 18 additions & 0 deletions src/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ class Variant extends Purchasable implements NestedElementInterface
*/
private ?string $_productTypeHandle = null;

/**
* @var bool Whether the SKU was auto-generated from a `skuFormat` containing `{id}` before this element had an ID,
* meaning it needs to be regenerated in [[afterAssignedId()]] once the ID is available.
*/
private bool $_regenerateSkuAfterIdAssigned = false;

/**
* @throws InvalidConfigException
*/
Expand Down Expand Up @@ -727,6 +733,12 @@ public function updateSku(Product $product): void
Craft::$app->language = $this->getSite()->language;
$this->sku = Craft::$app->getView()->renderSandboxedObjectTemplate($type->skuFormat, $this);

// If the format references the element's own ID but it doesn't have one yet, the rendered SKU will be
// missing that value — flag it for regeneration once afterAssignedId() runs.
if (!$this->id && str_contains($type->skuFormat, '{id}')) {
$this->_regenerateSkuAfterIdAssigned = true;
}

$skuExistsQuery = function(string $sku, ?int $id) {
$query = (new Query())
->select(['sku'])
Expand Down Expand Up @@ -1257,6 +1269,12 @@ public function afterAssignedId(): void

$product = $this->getOwner();
$this->updateTitle($product);

if ($this->_regenerateSkuAfterIdAssigned) {
$this->_regenerateSkuAfterIdAssigned = false;
$this->sku = '';
$this->updateSku($product);
}
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/elements/product/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,32 @@ public function testSkuFormatDeduplicatesMultipleVariantsWithCollision(): void
$this->setProductTypeSkuFormat(2001, null);
}

/**
* @group Product
*/
public function testSkuFormatWithIdIsRegeneratedAfterIdAssigned(): void
{
$this->setProductTypeSkuFormat(2001, 'SKU-{id}');

$product = new Product();
$product->title = 'SKU Format Id Test Product';
$product->typeId = 2001;
$product->enabled = false;

$variant = new Variant();
$variant->title = 'Test Variant';
// No SKU — format references {id}, which isn't available until after the element is saved

$product->setVariants([$variant]);
\Craft::$app->getElements()->saveElement($product, false);

$savedVariant = $product->getDefaultVariant();
self::assertEquals('SKU-' . $savedVariant->id, $savedVariant->sku);

\Craft::$app->getElements()->deleteElementById($product->id, Product::class, null, true);
$this->setProductTypeSkuFormat(2001, null);
}

private function resetSkuSequence(string $baseSku): void
{
\Craft::$app->getDb()->createCommand()
Expand Down
Loading