Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Fix - Error loading tier prices table when the customer group is deleted which was mapped to a tier price entry #363

support0110b3117f7f
diff --git a/src/Business/Grand.Business.Common/Events/GroupDeletedEventHandler.cs b/src/Business/Grand.Business.Common/Events/GroupDeletedEventHandler.cs
index a07232d10..92daf379e 100644
--- a/src/Business/Grand.Business.Common/Events/GroupDeletedEventHandler.cs
+++ b/src/Business/Grand.Business.Common/Events/GroupDeletedEventHandler.cs
@@ -1,5 +1,8 @@
-using Grand.Domain.Customers;
+using Grand.Domain.Catalog;
+using Grand.Domain.Customers;
using Grand.Domain.Data;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Events;
using MediatR;
@@ -8,16 +11,29 @@ namespace Grand.Business.Common.Events
public class GroupDeletedEventHandler : INotificationHandler<EntityDeleted<CustomerGroup>>
{
private readonly IRepository<Customer> _customerRepository;
-
- public GroupDeletedEventHandler(IRepository<Customer> customerRepository)
+ private readonly IRepository<Product> _productRepository;
+ private readonly ICacheBase _cacheBase;
+
+ public GroupDeletedEventHandler(
+ IRepository<Customer> customerRepository,
+ IRepository<Product> productRepository,
+ ICacheBase cacheBase)
{
_customerRepository = customerRepository;
+ _productRepository = productRepository;
+ _cacheBase = cacheBase;
}
public async Task Handle(EntityDeleted<CustomerGroup> notification, CancellationToken cancellationToken)
{
//delete from customers
await _customerRepository.Pull(string.Empty, x => x.Groups, notification.Entity.Id);
+
+ //delete tier prices on the product
+ await _productRepository.PullFilter(string.Empty, x => x.TierPrices, z => z.CustomerGroupId, notification.Entity.Id);
+
+ //clear cache
+ await _cacheBase.RemoveByPrefix(CacheKey.PRODUCTS_PATTERN_KEY);
}
}
}
diff --git a/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs b/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs
index 08ae5b14b..5b4d03741 100644
--- a/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs
+++ b/src/Web/Grand.Web.Admin/Services/ProductViewModelService.cs
@@ -1745,12 +1745,18 @@ namespace Grand.Web.Admin.Services
else
storeName = _translationService.GetResource("Admin.Catalog.Products.TierPrices.Fields.Store.All");
+ var groupName = string.Empty;
+ if(string.IsNullOrEmpty(x.CustomerGroupId))
+ groupName =_translationService.GetResource("Admin.Catalog.Products.TierPrices.Fields.CustomerGroup.All");
+ else
+ groupName = (await _groupService.GetCustomerGroupById(x.CustomerGroupId))?.Name ?? "NOT FOUND";
+
items.Add(new ProductModel.TierPriceModel {
Id = x.Id,
StoreId = x.StoreId,
Store = storeName,
CurrencyCode = x.CurrencyCode,
- CustomerGroup = !string.IsNullOrEmpty(x.CustomerGroupId) ? (await _groupService.GetCustomerGroupById(x.CustomerGroupId)).Name : _translationService.GetResource("Admin.Catalog.Products.TierPrices.Fields.CustomerGroup.All"),
+ CustomerGroup = groupName,
ProductId = product.Id,
CustomerGroupId = !string.IsNullOrEmpty(x.CustomerGroupId) ? x.CustomerGroupId : "",
Quantity = x.Quantity,