-
Notifications
You must be signed in to change notification settings - Fork 688
[Shopify] Add items as product variants #26712
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
onbuyuka
merged 12 commits into
microsoft:main
from
tinestaric:AddItemsAsProductVariants
Aug 1, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b20646d
Extract GetVariantSKU case
965b3a9
Extract Create Shpfy Variant from Item to an internal procedure
203553e
Bump API Version
cf122f5
Add Action for adding items as variants to existing shpfy products
e74253c
Revert API Version
7e63a33
Remove Option update, add gate keepers
9e016f7
Fix ID Collision
5203347
Add missing access property
cebc3e9
Merge branch 'main' into AddItemsAsProductVariants
JesperSchulz 356a6ce
Update Return value documentation
79c0d3b
Fix Object and Enum IDs
d09e954
Merge branch 'main' into AddItemsAsProductVariants
onbuyuka 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
27 changes: 27 additions & 0 deletions
27
Apps/W1/Shopify/app/src/GraphQL/Codeunits/ShpfyGQLGetProductOptions.Codeunit.al
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,27 @@ | ||
| namespace Microsoft.Integration.Shopify; | ||
|
|
||
| /// <summary> | ||
| /// Codeunit Shpfy GQL GetProductOptions (ID 30345) implements Interface Shpfy IGraphQL. | ||
| /// </summary> | ||
| codeunit 30345 "Shpfy GQL GetProductOptions" implements "Shpfy IGraphQL" | ||
| { | ||
| Access = Internal; | ||
|
|
||
| /// <summary> | ||
| /// GetGraphQL. | ||
| /// </summary> | ||
| /// <returns>Return value of type Text.</returns> | ||
| internal procedure GetGraphQL(): Text | ||
| begin | ||
| exit('{"query":"{product(id: \"gid://shopify/Product/{{ProductId}}\") {id title options {id name}}}"}'); | ||
| end; | ||
|
|
||
| /// <summary> | ||
| /// GetExpectedCost. | ||
| /// </summary> | ||
| /// <returns>Return value of type Integer.</returns> | ||
| internal procedure GetExpectedCost(): Integer | ||
| begin | ||
| exit(2); | ||
| end; | ||
| } |
27 changes: 27 additions & 0 deletions
27
Apps/W1/Shopify/app/src/GraphQL/Codeunits/ShpfyGQLProductVariantDelete.Codeunit.al
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,27 @@ | ||
| namespace Microsoft.Integration.Shopify; | ||
|
|
||
| /// <summary> | ||
| /// Codeunit Shpfy GQL ProductVariantDelete (ID 30344) implements Interface Shpfy IGraphQL. | ||
| /// </summary> | ||
| codeunit 30344 "Shpfy GQL ProductVariantDelete" implements "Shpfy IGraphQL" | ||
| { | ||
| Access = Internal; | ||
|
|
||
| /// <summary> | ||
| /// GetGraphQL. | ||
| /// </summary> | ||
| /// <returns>Return value of type Text.</returns> | ||
| internal procedure GetGraphQL(): Text | ||
| begin | ||
| exit('{"query":"mutation {productVariantDelete(id: \"gid://shopify/ProductVariant/{{VariantId}}\") {deletedProductVariantId userErrors{field message}}}"}'); | ||
| end; | ||
|
|
||
| /// <summary> | ||
| /// GetExpectedCost. | ||
| /// </summary> | ||
| /// <returns>Return value of type Integer.</returns> | ||
| internal procedure GetExpectedCost(): Integer | ||
| begin | ||
| exit(10); | ||
| end; | ||
| } |
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
110 changes: 110 additions & 0 deletions
110
Apps/W1/Shopify/app/src/Products/Codeunits/ShpfyCreateItemAsVariant.Codeunit.al
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,110 @@ | ||
| namespace Microsoft.Integration.Shopify; | ||
|
|
||
| using Microsoft.Inventory.Item; | ||
|
|
||
| codeunit 30343 "Shpfy Create Item As Variant" | ||
| { | ||
| TableNo = Item; | ||
| Access = Internal; | ||
|
|
||
tinestaric marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var | ||
| Shop: Record "Shpfy Shop"; | ||
| ShopifyProduct: Record "Shpfy Product"; | ||
| CreateProduct: Codeunit "Shpfy Create Product"; | ||
| VariantApi: Codeunit "Shpfy Variant API"; | ||
| ProductApi: Codeunit "Shpfy Product API"; | ||
| DefaultVariantId: BigInteger; | ||
|
|
||
| trigger OnRun() | ||
| begin | ||
| CreateVariantFromItem(Rec); | ||
| end; | ||
|
|
||
| /// <summary> | ||
| /// Creates a variant from a given item and adds it to the parent product. | ||
| /// </summary> | ||
| /// <param name="Item">The item to be added as a variant.</param> | ||
| internal procedure CreateVariantFromItem(var Item: Record "Item") | ||
| var | ||
| TempShopifyVariant: Record "Shpfy Variant" temporary; | ||
| begin | ||
| CreateProduct.CreateTempShopifyVariantFromItem(Item, TempShopifyVariant); | ||
| TempShopifyVariant."Product Id" := ShopifyProduct."Id"; | ||
| TempShopifyVariant.Title := Item."No."; | ||
| TempShopifyVariant."Option 1 Name" := 'Variant'; | ||
| TempShopifyVariant."Option 1 Value" := Item."No."; | ||
|
|
||
| if VariantApi.AddProductVariant(TempShopifyVariant) then begin | ||
| ShopifyProduct."Has Variants" := true; | ||
| ShopifyProduct.Modify(true); | ||
| end; | ||
| end; | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Checks if items can be added as variants to the product. The items cannot be added as variants if: | ||
| /// - The product has more than one option. | ||
| /// - The UoM as Variant setting is enabled. | ||
| /// </summary> | ||
| internal procedure CheckProductAndShopSettings() | ||
| var | ||
| MultipleOptionsErr: Label 'The product has more than one option. Items cannot be added as variants to a product with multiple options.'; | ||
| UOMAsVariantEnabledErr: Label 'Items cannot be added as variants to a product with the "%1" setting enabled for this store.', Comment = '%1 - UoM as Variant field caption'; | ||
| Options: Dictionary of [Text, Text]; | ||
| begin | ||
| if Shop."UoM as Variant" then | ||
| Error(UOMAsVariantEnabledErr, Shop.FieldCaption("UoM as Variant")); | ||
|
|
||
| Options := ProductApi.GetProductOptions(ShopifyProduct.Id); | ||
|
|
||
| if Options.Count > 1 then | ||
| Error(MultipleOptionsErr); | ||
| end; | ||
|
|
||
| /// <summary> | ||
| /// Finds the default variant ID for the product if the product has no variants. | ||
| /// If new variants will be added, the default variant will be removed. | ||
| /// </summary> | ||
| internal procedure FindDefaultVariantId() | ||
| var | ||
| ProductVariantIds: Dictionary of [BigInteger, DateTime]; | ||
| begin | ||
| if not ShopifyProduct."Has Variants" then begin | ||
| VariantApi.RetrieveShopifyProductVariantIds(ShopifyProduct, ProductVariantIds); | ||
| DefaultVariantId := ProductVariantIds.Keys.Get(1); | ||
| end; | ||
| end; | ||
|
|
||
| /// <summary> | ||
| /// Removes the default variant if new variants were added to the product. | ||
| /// </summary> | ||
| internal procedure RemoveDefaultVariant() | ||
| var | ||
| ShopifyVariant: Record "Shpfy Variant"; | ||
| begin | ||
| if (DefaultVariantId <> 0) and ShopifyProduct."Has Variants" then begin | ||
| VariantApi.DeleteProductVariant(DefaultVariantId); | ||
| if ShopifyVariant.Get(DefaultVariantId) then | ||
| ShopifyVariant.Delete(true); | ||
| end; | ||
| end; | ||
|
|
||
| /// <summary> | ||
| /// Sets the parent product to which the variant will be added. | ||
| /// </summary> | ||
| /// <param name="ShopifyProductId">The parent Shopify product ID.</param> | ||
| internal procedure SetParentProduct(ShopifyProductId: BigInteger) | ||
| begin | ||
| ShopifyProduct.Get(ShopifyProductId); | ||
| SetShop(ShopifyProduct."Shop Code"); | ||
| end; | ||
|
|
||
| local procedure SetShop(ShopCode: Code[20]) | ||
| begin | ||
| Shop.Get(ShopCode); | ||
| VariantApi.SetShop(Shop); | ||
| ProductApi.SetShop(Shop); | ||
| CreateProduct.SetShop(Shop); | ||
| end; | ||
|
|
||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.