[5.x] Fix variants not saving it the SKU auto format caused duplicates#4269
Open
nfourtythree wants to merge 4 commits into5.xfrom
Open
[5.x] Fix variants not saving it the SKU auto format caused duplicates#4269nfourtythree wants to merge 4 commits into5.xfrom
nfourtythree wants to merge 4 commits into5.xfrom
Conversation
There was a problem hiding this comment.
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’sskuFormat. - 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Product::beforeValidate()sets variant SKUs fromskuFormatwithout duplicate detection, so by the timeVariant::updateSku()runs the!$this->skuguard blocks the duplicate checkcraft\commerce\db\Tableandcraft\helpers\Sequenceimports toProduct.phpProduct::beforeValidate()where the SKU is initially generated