Bypass maximum car quantity based on user Role

1 week ago

On our store, we have set a maximum cart quantity for a specific item.

We don't want a regular customer to be able to purchase more than 5 units.

But we want this restriction to be ignored if this purchase is being done by a specific Role member.

Could someone please share a solution on how to accomplish this?

Thank you.
1 week ago
You need to customise the display of the error and check for the customers role
See   //quantity validation
public virtual IList<string> GetStandardWarnings(..)
in src\Libraries\Nop.Services\Orders\ShoppingCartService.cs
1 week ago
Hi Yidna,

Thank you for your reply.

Just a quick background: I've been developing ASP.NET Web Applications for a couple of years, but I have no experience with nopCommerce.

By looking at the code, I was able to get to that part.

Just two quick questions:
- How do I get the current user's Role (I know how to to that in .NET) but I don't know if nopCommerce does it in a different way;
I've found this thread ( and it looks like it's the same way:

- We have our store in Production. If I perform this additional validation and publish the store into a local folder on my machine, what files do I have to upload to server?

I mean: right now, our production environment is already running and I've noticed that it contains more/different files from those in my local computer (due to plugins, pdf receipts, etc).

After compiling and publishing to my local folder, what are the minimum files that I 'll have to upload to reflect this custom validation? I know that I cannot remove all files in production and replace them with all my local files...

Thank you!
1 week ago
Yes you can use that code to get the role and see if the customer is in the role
When the solution is built that code is in Nop.Services.dll
Nop.Services.dll is the only file you need to replace.

Using visual studio you can build and test
After its ready then build and Publish Nop.Web to a local directory

Take a backup of existing production version
Copy in the new version
Restart the webserver to load the new version

Should all work
1 week ago
Hi Yidna,

Just to let you know that I was able to accomplish what I wanted.

I don't know if it is the correct way, but it works perfectly. I'll just leave the code here because it may help somebody in the future.

// Get current logged in customer
Customer currentLoggedInCustomer = await _workContext.GetCurrentCustomerAsync();

// Variable that will tell us if the user belongs to our custom Role
bool isCurrentUserAWarehouse = await _customerService.IsInCustomerRoleAsync(currentLoggedInCustomer, "PlaceYourRoleNameHere", true);

// This was the previous condition
//if (quantity > product.OrderMaximumQuantity)

// This is the new condition
if (quantity > product.OrderMaximumQuantity && !isCurrentUserAWarehouse)
                warnings.Add(string.Format(await _localizationService.GetResourceAsync("ShoppingCart.MaximumQuantity"), product.OrderMaximumQuantity));
                hasQtyWarnings = true;

PS: I have done this in the ShoppingCartService.cs.

Thanks for your help!