-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Shopify] Add items as product variants (#26712)
This pull request does not have a related issue as it's part of delivery for development agreed directly with @AndreiPanko Added an action to Variants subpage to add BC Items as additional Variants to an existing Shopify Product. If the parent product only had a default variant before the addition, it is deleted from both Shopify and BC. The variants are added under the existing the Product Option (ex. Color, Material, or Title, if product only had the default variant) Items cannot be added if the shop has "UOM as Variant" enabled, or if the parent product has more than one option defined. Fixes #26819 Fixes [AB#468218](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/468218) --------- Co-authored-by: Tine Staric <[email protected]> Co-authored-by: Jesper Schulz-Wedde <[email protected]> Co-authored-by: Onat Buyukakkus <[email protected]>
- Loading branch information
1 parent
eb8dd44
commit fd23b55
Showing
9 changed files
with
403 additions
and
86 deletions.
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 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 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 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 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; | ||
|
||
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 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 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.