-
Notifications
You must be signed in to change notification settings - Fork 121
Variations: Generates all variations locally #8537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
WooCommerce/Classes/ViewRelated/Products/Variations/ProductVariationGenerator.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import Foundation | ||
| import Yosemite | ||
|
|
||
| /// Generates all possible variations from a product attributes | ||
| /// | ||
| struct ProductVariationGenerator { | ||
|
|
||
| /// Group a colection of attribute options. | ||
| /// EG: [Size: Large, Color: Black, Fabric: Cotton] | ||
| /// | ||
| private struct Combination: Hashable { | ||
| let options: [Option] | ||
| } | ||
|
|
||
| /// Represents an attribute option. | ||
| /// EG: Size: Large | ||
| /// | ||
| private struct Option: Hashable { | ||
| let attributeID: Int64 | ||
| let attributeName: String | ||
| let value: String | ||
| } | ||
|
|
||
| /// Generates all possible variations from a product attributes. | ||
| /// Additionally it excludes variations that already exists in the `variations` parameter. | ||
| /// | ||
| static func generateVariations(for product: Product, excluding variations: [ProductVariation]) -> [CreateProductVariation] { | ||
| let allCombinations = getCombinations(from: product) | ||
| let uniqueCombinations = filterExistingCombinations(allCombinations, existing: variations) | ||
| return buildVariations(from: uniqueCombinations, for: product) | ||
| } | ||
|
|
||
| /// Generates all posible combination for a product attributes. | ||
| /// | ||
| private static func getCombinations(from product: Product) -> [Combination] { | ||
| // Iterates through attributes while eceiving the previous combinations list. | ||
| product.attributes.reduce([Combination(options: [])]) { combinations, attribute in | ||
| combinations.flatMap { combination in | ||
| // When receiving a previous combination list, we add each attribute to each previous combination util we finish with them. | ||
| attribute.options.map { option in | ||
| Combination(options: combination.options + [Option(attributeID: attribute.attributeID, attributeName: attribute.name, value: option)]) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Removes the provided variations from the given combinations array. | ||
| /// | ||
| private static func filterExistingCombinations(_ combinations: [Combination], existing variations: [ProductVariation]) -> [Combination] { | ||
| // Convert variations into combinations | ||
| let existingCombinations = variations.map { existingVariation in | ||
| let options = existingVariation.attributes.map { attibute in | ||
| Option(attributeID: attibute.id, attributeName: attibute.name, value: attibute.option) | ||
| } | ||
| return Combination(options: options) | ||
| } | ||
|
|
||
| // Filter existing combinations. | ||
| let existingSet = Set(existingCombinations) | ||
| return combinations.filter { combination in | ||
| !existingSet.contains(combination) | ||
| } | ||
| } | ||
|
|
||
| /// Convert the provided combinations into `[CreateProductVariation]` types that are consumed by our Yosemite stores. | ||
| /// | ||
| private static func buildVariations(from combinations: [Combination], for product: Product) -> [CreateProductVariation] { | ||
| combinations.map { combination in | ||
| let attributes = combination.options.map { option in | ||
| ProductVariationAttribute(id: option.attributeID, name: option.attributeName, option: option.value) | ||
| } | ||
| // Setting a regular price is not required when creating a variation. | ||
| return CreateProductVariation(regularPrice: "", attributes: attributes) | ||
| } | ||
| } | ||
| } | ||
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
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
121 changes: 121 additions & 0 deletions
121
...rce/WooCommerceTests/ViewRelated/Products/Variations/ProductVariationGeneratorTests.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import XCTest | ||
| @testable import WooCommerce | ||
| @testable import Yosemite | ||
|
|
||
| final class ProductVariationGeneratorTests: XCTestCase { | ||
|
|
||
| func test_all_variations_are_generated_correctly() { | ||
| // Given | ||
| let product = Product.fake().copy(attributes: [ | ||
| ProductAttribute.fake().copy(attributeID: 1, name: "Size", options: ["S", "M"]), | ||
| ProductAttribute.fake().copy(attributeID: 2, name: "Color", options: ["Red", "Green"]), | ||
| ProductAttribute.fake().copy(attributeID: 3, name: "Fabric", options: ["Cotton", "Nylon"]), | ||
| ]) | ||
|
|
||
| // When | ||
| let variations = ProductVariationGenerator.generateVariations(for: product, excluding: []) | ||
|
|
||
| // Then | ||
| XCTAssertEqual(variations, [ | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "S"), | ||
| .init(id: 2, name: "Color", option: "Red"), | ||
| .init(id: 3, name: "Fabric", option: "Cotton") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "S"), | ||
| .init(id: 2, name: "Color", option: "Red"), | ||
| .init(id: 3, name: "Fabric", option: "Nylon") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "S"), | ||
| .init(id: 2, name: "Color", option: "Green"), | ||
| .init(id: 3, name: "Fabric", option: "Cotton") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "S"), | ||
| .init(id: 2, name: "Color", option: "Green"), | ||
| .init(id: 3, name: "Fabric", option: "Nylon") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "M"), | ||
| .init(id: 2, name: "Color", option: "Red"), | ||
| .init(id: 3, name: "Fabric", option: "Cotton") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "M"), | ||
| .init(id: 2, name: "Color", option: "Red"), | ||
| .init(id: 3, name: "Fabric", option: "Nylon") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "M"), | ||
| .init(id: 2, name: "Color", option: "Green"), | ||
| .init(id: 3, name: "Fabric", option: "Cotton") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "M"), | ||
| .init(id: 2, name: "Color", option: "Green"), | ||
| .init(id: 3, name: "Fabric", option: "Nylon") | ||
| ]), | ||
| ]) | ||
| } | ||
|
|
||
| func test_existing_variations_are_excluded_correctly() { | ||
| // Given | ||
| let product = Product.fake().copy(attributes: [ | ||
| ProductAttribute.fake().copy(attributeID: 1, name: "Size", options: ["S", "M"]), | ||
| ProductAttribute.fake().copy(attributeID: 2, name: "Color", options: ["Red", "Green"]), | ||
| ProductAttribute.fake().copy(attributeID: 3, name: "Fabric", options: ["Cotton", "Nylon"]), | ||
| ]) | ||
|
|
||
| let existingVariations = [ | ||
| ProductVariation.fake().copy(attributes: [ | ||
| .init(id: 1, name: "Size", option: "M"), | ||
| .init(id: 2, name: "Color", option: "Green"), | ||
| .init(id: 3, name: "Fabric", option: "Cotton"), | ||
| ]), | ||
| ProductVariation.fake().copy(attributes: [ | ||
| .init(id: 1, name: "Size", option: "S"), | ||
| .init(id: 2, name: "Color", option: "Red"), | ||
| .init(id: 3, name: "Fabric", option: "Nylon"), | ||
| ]) | ||
| ] | ||
|
|
||
| // When | ||
| let variations = ProductVariationGenerator.generateVariations(for: product, excluding: existingVariations) | ||
|
|
||
| // Then | ||
| XCTAssertEqual(variations, [ | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "S"), | ||
| .init(id: 2, name: "Color", option: "Red"), | ||
| .init(id: 3, name: "Fabric", option: "Cotton") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "S"), | ||
| .init(id: 2, name: "Color", option: "Green"), | ||
| .init(id: 3, name: "Fabric", option: "Cotton") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "S"), | ||
| .init(id: 2, name: "Color", option: "Green"), | ||
| .init(id: 3, name: "Fabric", option: "Nylon") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "M"), | ||
| .init(id: 2, name: "Color", option: "Red"), | ||
| .init(id: 3, name: "Fabric", option: "Cotton") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "M"), | ||
| .init(id: 2, name: "Color", option: "Red"), | ||
| .init(id: 3, name: "Fabric", option: "Nylon") | ||
| ]), | ||
| CreateProductVariation(regularPrice: "", attributes: [ | ||
| .init(id: 1, name: "Size", option: "M"), | ||
| .init(id: 2, name: "Color", option: "Green"), | ||
| .init(id: 3, name: "Fabric", option: "Nylon") | ||
| ]), | ||
| ]) | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: updating initializer to have empty string as a default value for
regularPricecan improve readability.