Disable inventory and price update for products

we want to disable modification of price and inventory quantity for product which are under a particular product template. how to achieve this ?

Litium version: 7

You could use a ValidationRule to check if the user is allowed to modify PriceListItem for a Variant. The _system user should always be allowed (I did not test this, only that it doesn’t work for non-system users). Removing price is still allowed.

So something like this for PriceListItem and then something similar for InventoryItem.

using Litium.FieldFramework;
using Litium.Products;
using Litium.Security;
using Litium.Validations;

namespace Litium.Accelerator.ValidationRules
{
	public class ValidatePricePermission : ValidationRuleBase<PriceListItem>
	{
		private readonly BaseProductService _baseProductService;
		private readonly FieldTemplateService _fieldTemplateService;
		private readonly SecurityContextService _securityContextService;
		private readonly VariantService _variantService;

		public ValidatePricePermission(BaseProductService baseProductService,
			FieldTemplateService fieldTemplateService,
			SecurityContextService securityContextService,
			VariantService variantService)
		{
			_baseProductService = baseProductService;
			_fieldTemplateService = fieldTemplateService;
			_securityContextService = securityContextService;
			_variantService = variantService;
		}

		public override ValidationResult Validate(PriceListItem entity, ValidationMode validationMode)
		{
			var result = new ValidationResult();

			if (validationMode == ValidationMode.Remove)
				return result;

			var variant = _variantService.Get(entity.VariantSystemId);
			var baseProduct = _baseProductService.Get(variant.BaseProductSystemId);
			var fieldTemplate = _fieldTemplateService.Get<ProductFieldTemplate>("ProductWithOneVariant");

			if (baseProduct.FieldTemplateSystemId == fieldTemplate.SystemId)
			{
				if (_securityContextService.GetIdentityUserSystemId() != SecurityContextService.System.SystemId)
				{
					result.AddError("*", "You are not allowed to update the price for this template!");
				}
			}

			return result;
		}
	}
}
2 Likes

It worked. Thanks a lot ! :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.