NopCommerce 4.2 - Tier Price gets deletec after Editing

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
Changing the foreign key relationship on the database does the same this as changing the TierPriceMap .OnDelete(DeleteBehavior.Cascade) so I did not try it.
3 years ago
OK, after one night's sleep I got a working work-around...

I think the root cause is actually the fact that the ViewModel (Nop.Web.Areas.Admin.Models.Catalog.TierPrice) contains a string property called CustomerRole which conflicts with the entity (Nop.Core.Domain.Catalog.TierPrice) virtual property CustomerRole.
When the extension method model.ToEntity(TierPrice) is called, the CustomerRole is lost on the tierPrice entity (previously pulled from the database).

My workaround is to skip the extension method and instead to set the changed properties from the model into the entity (at least the only ones I care about).

To Summarize, I made the following changes in the Nop.Web.Areas.Admin.Controllers.ProductController:


[HttpPost]
public virtual IActionResult TierPriceEditPopup(TierPriceModel model)
        {
             [...]
             if (ModelState.IsValid)
            {
                //fill entity from model
                // tierPrice = model.ToEntity(tierPrice);

                //At this point we already have the TierPrice pulled from the DB
                //that contains a CustomerRole entity
                //The code above removes the CustomerRole at model.ToEntity()
                //Instead we are going to add the properties from the model to the entity
                tierPrice.Price = model.Price;
                tierPrice.Quantity = model.Quantity;
                tierPrice.StartDateTimeUtc = model.StartDateTimeUtc;
                tierPrice.EndDateTimeUtc = model.EndDateTimeUtc;

                tierPrice.CustomerRoleId = model.CustomerRoleId > 0 ? model.CustomerRoleId : (int?)null;
                _productService.UpdateTierPrice(tierPrice);

                ViewBag.RefreshPage = true;

                return View(model);
            }
         }


This seems to be working for my system.  I hope this helps someone.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.