Skip to content

[5.x] Fix variants not saving it the SKU auto format caused duplicates#4269

Open
nfourtythree wants to merge 4 commits into5.xfrom
nathaniel/com-540-5x-the-sku-is-not-editable-if-an-automatic-format-is-used
Open

[5.x] Fix variants not saving it the SKU auto format caused duplicates#4269
nfourtythree wants to merge 4 commits into5.xfrom
nathaniel/com-540-5x-the-sku-is-not-editable-if-an-automatic-format-is-used

Conversation

@nfourtythree
Copy link
Copy Markdown
Contributor

@nfourtythree nfourtythree commented Apr 7, 2026

  • Understand the issue: Product::beforeValidate() sets variant SKUs from skuFormat without duplicate detection, so by the time Variant::updateSku() runs the !$this->sku guard blocks the duplicate check
  • Add craft\commerce\db\Table and craft\helpers\Sequence imports to Product.php
  • Add collision detection logic to Product::beforeValidate() where the SKU is initially generated
  • PHP syntax validation passed
  • Code review and security scan passed

@nfourtythree nfourtythree self-assigned this Apr 7, 2026
@linear
Copy link
Copy Markdown

linear bot commented Apr 7, 2026

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent variant saves from failing when an automatic Variant SKU Format generates duplicate SKUs, by attempting to detect existing SKUs and adjusting the generated SKU to avoid collisions.

Changes:

  • Added SKU collision detection during Variant::updateSku() when SKUs are generated from a product type’s skuFormat.
  • Introduced sequence-based numeric suffixing for auto-generated SKUs that would otherwise clash.
  • Documented the fix in the changelog under “Unreleased”.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/elements/Variant.php Adds logic to detect existing SKUs and append a sequence suffix when auto-generated SKUs would duplicate.
CHANGELOG.md Adds an Unreleased entry describing the variant SKU duplicate-save fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…erated SKUs

Agent-Logs-Url: https://github.com/craftcms/commerce/sessions/537ef641-fdea-488b-b434-0eb4c430782b

Co-authored-by: nfourtythree <266453+nfourtythree@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants