From 11aba02c067491b69ff161f0a43d956f6ca30c3c Mon Sep 17 00:00:00 2001 From: Konstantin Savosteev Date: Tue, 3 Dec 2024 18:20:01 +0200 Subject: [PATCH] VCST-2223: update to Configurable Products (#23) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: update to Configurable Products (#23) --------- Co-authored-by: Евгений Колосов --- .../VirtoCommerce.XCart.Core.csproj | 2 +- .../Commands/AddCartItemCommandHandler.cs | 17 ++++++++-- .../GetProductConfigurationQueryHandler.cs | 31 ++++++++++++++----- src/VirtoCommerce.XCart.Web/module.manifest | 2 +- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/VirtoCommerce.XCart.Core/VirtoCommerce.XCart.Core.csproj b/src/VirtoCommerce.XCart.Core/VirtoCommerce.XCart.Core.csproj index 05e4278..620e88c 100644 --- a/src/VirtoCommerce.XCart.Core/VirtoCommerce.XCart.Core.csproj +++ b/src/VirtoCommerce.XCart.Core/VirtoCommerce.XCart.Core.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/VirtoCommerce.XCart.Data/Commands/AddCartItemCommandHandler.cs b/src/VirtoCommerce.XCart.Data/Commands/AddCartItemCommandHandler.cs index 6674c08..21bd7ad 100644 --- a/src/VirtoCommerce.XCart.Data/Commands/AddCartItemCommandHandler.cs +++ b/src/VirtoCommerce.XCart.Data/Commands/AddCartItemCommandHandler.cs @@ -2,6 +2,9 @@ using System.Threading; using System.Threading.Tasks; using MediatR; +using VirtoCommerce.CatalogModule.Core.Model.Search; +using VirtoCommerce.CatalogModule.Core.Search; +using VirtoCommerce.Platform.Core.Common; using VirtoCommerce.XCart.Core; using VirtoCommerce.XCart.Core.Commands; using VirtoCommerce.XCart.Core.Commands.BaseCommands; @@ -14,15 +17,19 @@ public class AddCartItemCommandHandler : CartCommandHandler { private readonly ICartProductService _cartProductService; private readonly IMediator _mediator; + private readonly IProductConfigurationSearchService _productConfigurationSearchService; public AddCartItemCommandHandler( ICartAggregateRepository cartAggregateRepository, ICartProductService cartProductService, - IMediator mediator) + IMediator mediator, + IProductConfigurationSearchService productConfigurationSearchService + ) : base(cartAggregateRepository) { _cartProductService = cartProductService; _mediator = mediator; + _productConfigurationSearchService = productConfigurationSearchService; } public override async Task Handle(AddCartItemCommand request, CancellationToken cancellationToken) @@ -38,7 +45,13 @@ public override async Task Handle(AddCartItemCommand request, Can CartProduct = product, }; - if (product?.Product?.IsConfigurable == true) + var configurations = await _productConfigurationSearchService.SearchNoCloneAsync(new ProductConfigurationSearchCriteria + { + ProductId = request.ProductId + }); + var configuration = configurations.Results.FirstOrDefault(); + + if (configuration?.IsActive == true) { var createConfigurableProductCommand = new CreateConfiguredLineItemCommand { diff --git a/src/VirtoCommerce.XCart.Data/Queries/GetProductConfigurationQueryHandler.cs b/src/VirtoCommerce.XCart.Data/Queries/GetProductConfigurationQueryHandler.cs index 903ab17..0eb0370 100644 --- a/src/VirtoCommerce.XCart.Data/Queries/GetProductConfigurationQueryHandler.cs +++ b/src/VirtoCommerce.XCart.Data/Queries/GetProductConfigurationQueryHandler.cs @@ -1,7 +1,10 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using VirtoCommerce.CatalogModule.Core.Services; +using VirtoCommerce.CatalogModule.Core.Model.Configuration; +using VirtoCommerce.CatalogModule.Core.Model.Search; +using VirtoCommerce.CatalogModule.Core.Search; +using VirtoCommerce.Platform.Core.Common; using VirtoCommerce.Xapi.Core.Infrastructure; using VirtoCommerce.XCart.Core; using VirtoCommerce.XCart.Core.Models; @@ -12,23 +15,30 @@ namespace VirtoCommerce.XCatalog.Data.Queries; public class GetProductConfigurationQueryHandler : IQueryHandler { - private readonly IConfigurableProductService _configurableProductService; + private readonly IProductConfigurationSearchService _productConfigurationSearchService; private readonly IConfiguredLineItemContainerService _configuredLineItemContainerService; private readonly ICartProductsLoaderService _cartProductService; public GetProductConfigurationQueryHandler( - IConfigurableProductService configurableProductService, + IProductConfigurationSearchService productConfigurationSearchService, IConfiguredLineItemContainerService configuredLineItemContainerService, ICartProductsLoaderService cartProductService) { - _configurableProductService = configurableProductService; + _productConfigurationSearchService = productConfigurationSearchService; _configuredLineItemContainerService = configuredLineItemContainerService; _cartProductService = cartProductService; } public async Task Handle(GetProductConfigurationQuery request, CancellationToken cancellationToken) { - var configuration = await _configurableProductService.GetProductConfigurationAsync(request.ConfigurableProductId); + var result = new ProductConfigurationQueryResponse(); + + var configuration = await GetConfiguration(request); + + if (configuration == null) + { + return result; + } var container = await _configuredLineItemContainerService.CreateContainerAsync(request); @@ -40,7 +50,6 @@ public async Task Handle(GetProductConfigurat var productByIds = cartProducts.ToDictionary(x => x.Product.Id, x => x); - var result = new ProductConfigurationQueryResponse(); foreach (var section in configuration.Sections) { var configurationSection = new ExpProductConfigurationSection @@ -49,7 +58,6 @@ public async Task Handle(GetProductConfigurat Name = section.Name, IsRequired = section.IsRequired, Description = section.Description, - Type = section.Type, }; result.ConfigurationSections.Add(configurationSection); @@ -76,4 +84,13 @@ public async Task Handle(GetProductConfigurat return result; } + + private async Task GetConfiguration(GetProductConfigurationQuery request) + { + var criteria = AbstractTypeFactory.TryCreateInstance(); + criteria.ProductId = request.ConfigurableProductId; + + var configurationsResult = await _productConfigurationSearchService.SearchNoCloneAsync(criteria); + return configurationsResult.Results.FirstOrDefault(); + } } diff --git a/src/VirtoCommerce.XCart.Web/module.manifest b/src/VirtoCommerce.XCart.Web/module.manifest index 7bf11f5..1fe4bc7 100644 --- a/src/VirtoCommerce.XCart.Web/module.manifest +++ b/src/VirtoCommerce.XCart.Web/module.manifest @@ -13,7 +13,7 @@ - + Cart Experience API