-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VCST-2092: add configurable products support (#14)
- Loading branch information
1 parent
8cfa797
commit a43f67b
Showing
35 changed files
with
1,084 additions
and
16 deletions.
There are no files selected for viewing
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
22 changes: 22 additions & 0 deletions
22
src/VirtoCommerce.XCart.Core/Commands/CreateConfiguredLineItemCommand.cs
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,22 @@ | ||
using System.Collections.Generic; | ||
using VirtoCommerce.Xapi.Core.Infrastructure; | ||
using VirtoCommerce.XCart.Core.Models; | ||
|
||
namespace VirtoCommerce.XCart.Core.Commands; | ||
|
||
public class CreateConfiguredLineItemCommand : ICommand<ExpConfigurationLineItem>, ICartProductContainerRequest | ||
{ | ||
public string StoreId { get; set; } | ||
|
||
public string UserId { get; set; } | ||
|
||
public string OrganizationId { get; set; } | ||
|
||
public string CurrencyCode { get; set; } | ||
|
||
public string CultureName { get; set; } | ||
|
||
public string ConfigurableProductId { get; set; } | ||
|
||
public IList<ProductConfigurationSection> ConfigurationSections { get; set; } = []; | ||
} |
148 changes: 148 additions & 0 deletions
148
src/VirtoCommerce.XCart.Core/ConfiguredLineItemContainer.cs
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,148 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using VirtoCommerce.CartModule.Core.Model; | ||
using VirtoCommerce.CoreModule.Core.Currency; | ||
using VirtoCommerce.CustomerModule.Core.Model; | ||
using VirtoCommerce.Platform.Core.Common; | ||
using VirtoCommerce.StoreModule.Core.Model; | ||
using VirtoCommerce.XCart.Core.Models; | ||
|
||
namespace VirtoCommerce.XCart.Core | ||
{ | ||
public class ConfiguredLineItemContainer : ICloneable | ||
{ | ||
public Currency Currency { get; set; } | ||
public Store Store { get; set; } | ||
public Member Member { get; set; } | ||
public string CultureName { get; set; } | ||
public string UserId { get; set; } | ||
public IList<string> ProductsIncludeFields { get; set; } | ||
|
||
public CartProduct ConfigurableProduct { get; set; } | ||
public IList<LineItem> Items { get; set; } = new List<LineItem>(); | ||
|
||
public LineItem AddItem(CartProduct cartProduct, int quantity) | ||
{ | ||
var lineItem = AbstractTypeFactory<LineItem>.TryCreateInstance(); | ||
lineItem.ProductId = cartProduct.Id; | ||
lineItem.Name = cartProduct.Product.Name; | ||
lineItem.Sku = cartProduct.Product.Code; | ||
lineItem.ImageUrl = cartProduct.Product.ImgSrc; | ||
lineItem.CatalogId = cartProduct.Product.CatalogId; | ||
lineItem.CategoryId = cartProduct.Product.CategoryId; | ||
|
||
lineItem.Quantity = quantity; | ||
|
||
// calculate prices and only static rewards | ||
if (cartProduct.Price != null) | ||
{ | ||
lineItem.Currency = cartProduct.Price.Currency.Code; | ||
|
||
var tierPrice = cartProduct.Price.GetTierPrice(quantity); | ||
if (tierPrice.Price.Amount > 0) | ||
{ | ||
lineItem.SalePrice = tierPrice.ActualPrice.Amount; | ||
lineItem.ListPrice = tierPrice.Price.Amount; | ||
} | ||
|
||
lineItem.DiscountAmount = Math.Max(0, lineItem.ListPrice - lineItem.SalePrice); | ||
lineItem.PlacedPrice = lineItem.ListPrice - lineItem.DiscountAmount; | ||
lineItem.ExtendedPrice = lineItem.PlacedPrice * lineItem.Quantity; | ||
} | ||
|
||
Items.Add(lineItem); | ||
|
||
return lineItem; | ||
} | ||
|
||
public ExpConfigurationLineItem CreateConfiguredLineItem() | ||
{ | ||
var lineItem = AbstractTypeFactory<LineItem>.TryCreateInstance(); | ||
|
||
lineItem.IsConfigured = true; | ||
lineItem.Quantity = 1; | ||
|
||
lineItem.Discounts = []; | ||
lineItem.TaxDetails = []; | ||
|
||
lineItem.ProductId = ConfigurableProduct.Product.Id; | ||
lineItem.Sku = $"Configuration-{ConfigurableProduct.Product.Code}"; | ||
|
||
lineItem.CatalogId = ConfigurableProduct.Product.CatalogId; | ||
lineItem.CategoryId = ConfigurableProduct.Product.CategoryId; | ||
|
||
lineItem.Name = ConfigurableProduct.Product.Name; | ||
lineItem.ImageUrl = ConfigurableProduct.Product.ImgSrc; | ||
lineItem.ProductOuterId = ConfigurableProduct.Product.OuterId; | ||
lineItem.ProductType = ConfigurableProduct.Product.ProductType; | ||
lineItem.TaxType = ConfigurableProduct.Product.TaxType; | ||
|
||
lineItem.FulfillmentCenterId = ConfigurableProduct.Inventory?.FulfillmentCenterId; | ||
lineItem.FulfillmentCenterName = ConfigurableProduct.Inventory?.FulfillmentCenterName; | ||
lineItem.VendorId = ConfigurableProduct.Product.Vendor; | ||
|
||
// create sub items | ||
lineItem.ConfigurationItems = Items | ||
.Select(x => | ||
{ | ||
var subItem = AbstractTypeFactory<ConfigurationItem>.TryCreateInstance(); | ||
|
||
subItem.ProductId = x.ProductId; | ||
subItem.Name = x.Name; | ||
subItem.Sku = x.Sku; | ||
subItem.ImageUrl = x.ImageUrl; | ||
subItem.Quantity = x.Quantity; | ||
subItem.CatalogId = x.CatalogId; | ||
subItem.CategoryId = x.CategoryId; | ||
|
||
return subItem; | ||
}) | ||
.ToList(); | ||
|
||
// prices | ||
lineItem.Currency = Currency.Code; | ||
|
||
UpdatePrice(lineItem); | ||
|
||
return new ExpConfigurationLineItem | ||
{ | ||
Item = lineItem, | ||
Currency = Currency, | ||
CultureName = CultureName, | ||
UserId = UserId, | ||
StoreId = Store.Id, | ||
}; | ||
} | ||
|
||
public void UpdatePrice(LineItem lineItem) | ||
{ | ||
var configurableProductPrice = ConfigurableProduct.Price ?? new Xapi.Core.Models.ProductPrice(Currency); | ||
|
||
lineItem.ListPrice = Items.Sum(x => x.ListPrice * x.Quantity) + configurableProductPrice.ListPrice.Amount; | ||
lineItem.SalePrice = Items.Sum(x => x.SalePrice * x.Quantity) + configurableProductPrice.SalePrice.Amount; | ||
|
||
lineItem.DiscountAmount = Math.Max(0, lineItem.ListPrice - lineItem.SalePrice); | ||
lineItem.PlacedPrice = lineItem.ListPrice - lineItem.DiscountAmount; | ||
lineItem.ExtendedPrice = lineItem.PlacedPrice * lineItem.Quantity; | ||
} | ||
|
||
public CartProductsRequest GetCartProductsRequest() | ||
{ | ||
var request = AbstractTypeFactory<CartProductsRequest>.TryCreateInstance(); | ||
|
||
request.Store = Store; | ||
request.Currency = Currency; | ||
request.CultureName = CultureName; | ||
request.Member = Member; | ||
request.UserId = UserId; | ||
|
||
return request; | ||
} | ||
|
||
public object Clone() | ||
{ | ||
return MemberwiseClone(); | ||
} | ||
} | ||
} |
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,15 @@ | ||
using VirtoCommerce.CartModule.Core.Model; | ||
using VirtoCommerce.CoreModule.Core.Currency; | ||
|
||
namespace VirtoCommerce.XCart.Core | ||
{ | ||
public class ExpConfigurationLineItem | ||
{ | ||
public string StoreId { get; set; } | ||
public string UserId { get; set; } | ||
public string CultureName { get; set; } | ||
|
||
public LineItem Item { get; set; } | ||
public Currency Currency { get; set; } | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/VirtoCommerce.XCart.Core/Models/CartProductsRequest.cs
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,21 @@ | ||
using System.Collections.Generic; | ||
using VirtoCommerce.CoreModule.Core.Currency; | ||
using VirtoCommerce.CustomerModule.Core.Model; | ||
using VirtoCommerce.StoreModule.Core.Model; | ||
|
||
namespace VirtoCommerce.XCart.Core.Models; | ||
|
||
public class CartProductsRequest | ||
{ | ||
public Store Store { get; set; } | ||
public string CultureName { get; set; } | ||
public Currency Currency { get; set; } | ||
public Member Member { get; set; } | ||
public string UserId { get; set; } | ||
|
||
public IList<string> ProductIds { get; set; } | ||
public IList<string> ProductsIncludeFields { get; set; } | ||
|
||
public bool LoadPrice { get; set; } = true; | ||
public bool LoadInventory { get; set; } = true; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/VirtoCommerce.XCart.Core/Models/ConfigurableProductOption.cs
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,8 @@ | ||
namespace VirtoCommerce.XCart.Core.Models; | ||
|
||
public class ConfigurableProductOption | ||
{ | ||
public string ProductId { get; set; } | ||
|
||
public int Quantity { get; set; } = 1; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/VirtoCommerce.XCart.Core/Models/ExpConfigurationSection.cs
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,19 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace VirtoCommerce.XCart.Core.Models; | ||
|
||
public class ExpProductConfigurationSection | ||
{ | ||
public string Id { get; set; } | ||
public string Name { get; set; } | ||
public string Type { get; set; } | ||
public string Description { get; set; } | ||
public bool IsRequired { get; set; } | ||
|
||
public IList<ExpConfigurationLineItem> Options { get; set; } = new List<ExpConfigurationLineItem>(); | ||
} | ||
|
||
public class ProductConfigurationQueryResponse | ||
{ | ||
public IList<ExpProductConfigurationSection> ConfigurationSections { get; set; } = new List<ExpProductConfigurationSection>(); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/VirtoCommerce.XCart.Core/Models/ICartProductContainerRequest.cs
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,10 @@ | ||
namespace VirtoCommerce.XCart.Core.Models; | ||
|
||
public interface ICartProductContainerRequest | ||
{ | ||
string StoreId { get; set; } | ||
string UserId { get; set; } | ||
string OrganizationId { get; set; } | ||
string CurrencyCode { get; set; } | ||
string CultureName { get; set; } | ||
} |
8 changes: 8 additions & 0 deletions
8
src/VirtoCommerce.XCart.Core/Models/ProductConfigurationSection.cs
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,8 @@ | ||
namespace VirtoCommerce.XCart.Core.Models; | ||
|
||
public class ProductConfigurationSection | ||
{ | ||
public string SectionId { get; set; } | ||
|
||
public ConfigurableProductOption Value { get; set; } | ||
} |
50 changes: 50 additions & 0 deletions
50
src/VirtoCommerce.XCart.Core/Queries/GetProductConfigurationQuery.cs
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,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using GraphQL; | ||
using GraphQL.Types; | ||
using VirtoCommerce.StoreModule.Core.Model; | ||
using VirtoCommerce.Xapi.Core.BaseQueries; | ||
using VirtoCommerce.Xapi.Core.Extensions; | ||
using VirtoCommerce.XCart.Core.Models; | ||
|
||
namespace VirtoCommerce.XCart.Core.Queries; | ||
|
||
public class GetProductConfigurationQuery : Query<ProductConfigurationQueryResponse>, ICartProductContainerRequest | ||
{ | ||
public string ConfigurableProductId { get; set; } | ||
|
||
public string StoreId { get; set; } | ||
|
||
public string UserId { get; set; } | ||
|
||
public string OrganizationId { get; set; } | ||
|
||
public string CurrencyCode { get; set; } | ||
|
||
public string CultureName { get; set; } | ||
|
||
public Store Store { get; set; } | ||
|
||
public IList<string> IncludeFields { get; set; } = Array.Empty<string>(); | ||
|
||
public override IEnumerable<QueryArgument> GetArguments() | ||
{ | ||
yield return Argument<NonNullGraphType<StringGraphType>>(nameof(ConfigurableProductId)); | ||
|
||
yield return Argument<NonNullGraphType<StringGraphType>>(nameof(StoreId), description: "Store Id"); | ||
yield return Argument<StringGraphType>(nameof(UserId), description: "User Id"); | ||
yield return Argument<StringGraphType>(nameof(CultureName), description: "Currency code (\"USD\")"); | ||
yield return Argument<StringGraphType>(nameof(CurrencyCode), description: "Culture name (\"en-US\")"); | ||
} | ||
|
||
public override void Map(IResolveFieldContext context) | ||
{ | ||
ConfigurableProductId = context.GetArgument<string>(nameof(ConfigurableProductId)); | ||
|
||
StoreId = context.GetArgument<string>(nameof(StoreId)); | ||
UserId = context.GetArgument<string>(nameof(UserId)) ?? context.GetCurrentUserId(); | ||
OrganizationId = context.GetCurrentOrganizationId(); | ||
CultureName = context.GetArgument<string>(nameof(CultureName)); | ||
CurrencyCode = context.GetArgument<string>(nameof(CurrencyCode)); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/VirtoCommerce.XCart.Core/Schemas/CartConfigurationItemType.cs
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,14 @@ | ||
using VirtoCommerce.CartModule.Core.Model; | ||
using VirtoCommerce.Xapi.Core.Schemas; | ||
|
||
namespace VirtoCommerce.XCart.Core.Schemas | ||
{ | ||
public class CartConfigurationItemType : ExtendableGraphType<ConfigurationItem> | ||
{ | ||
public CartConfigurationItemType() | ||
{ | ||
Field(x => x.Id, nullable: false).Description("Configuration item ID"); | ||
Field(x => x.Name, nullable: true).Description("Configuration item name"); | ||
} | ||
} | ||
} |
Oops, something went wrong.