Allow buy button for certain products by customer role

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
I have been given a requirement that I have not seen posted before, forgive me if I missed it. The standard access control list for products limits being able to see the product in the catalog by role. While this functionality is great, and we use it regularly, we also require a way to allow customers to buy a product only if they are in a certain role, but other roles can still view the product. What would be the best way to go about this? Simply create a plugin that checks the customer and the product to see if the add to cart button should be visible?
6 years ago
I just found this https://github.com/nopSolutions/nopCommerce/issues/2111 which might be related.
6 years ago
We created a new class and inherited from IProductService and then override the standard methods (GetProductById, etc). We first called the base method and then depending on the business logic set the DisableBuyButton (on the product record) to true or false.

Hope that helps
6 years ago
So the
public virtual Product getProductById(int productId)
and
public virtual IList<Product> getProductsByIds(int[] productIds)
methods are the ones called to display the product details page and list of products respectively? Thus overriding them and changing the disable buy button flag based on the customer should remove the option to buy? If so that is amazing and I appreciate the direction!
6 years ago
[email protected] wrote:
We created a new class and inherited from IProductService and then override the standard methods (GetProductById, etc). We first called the base method and then depending on the business logic set the DisableBuyButton (on the product record) to true or false.

Hope that helps


So I completed this implementation and it works like a charm for quick view and the product details view. However, in the grid or list view it is hit or miss. Did you seem to have a problem with caching or something of that nature?
6 years ago
Something rings a bell that we did but i can't see anything in the code that relates to caching. It looks like we also override :
GetAllProductsDisplayedOnHomePage()
SearchProducts()

Also a gotcha for us was when amending records in the admin area the class would get called and certain products would randomly get bits changed (we use the same method to override prices).

Anyway we got round it by adding the following when overriding the methods.

//Get the product record
var productRecord = base.GetProductById(productId);

if (_workContext.IsAdmin)
{
   return productRecord;
}
6 years ago
[email protected] wrote:
Something rings a bell that we did but i can't see anything in the code that relates to caching. It looks like we also override :
GetAllProductsDisplayedOnHomePage()
SearchProducts()

Also a gotcha for us was when amending records in the admin area the class would get called and certain products would randomly get bits changed (we use the same method to override prices).

Anyway we got round it by adding the following when overriding the methods.

//Get the product record
var productRecord = base.GetProductById(productId);

if (_workContext.IsAdmin)
{
   return productRecord;
}


Something definitely seems off. After waiting all night, I did another reload and while in the product list/grid view, the buy button is not disabled, but while in the quick view and product details view all is well. I will see what I can figure out. I definitely noticed that gotcha though. I was wondering why my products had the buy button disabled when I was a member of the role allowed to purchase them. Well, the admin area had the disable buy button checkbox toggled and I clicked save. That was a fun fix. Thank you for your help!
6 years ago
Is this a vanilla install or are you using things like noptemplates?
6 years ago
[email protected] wrote:
Is this a vanilla install or are you using things like noptemplates?


Far from vanilla. Lots of plugins, both custom and purchased, nopTemplates as well.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.