Recently a client who runs a B2c and B2B Magento shop, asked if It was possible to limit the right to buy a product to certain customer groups.
With Magento it certainly is! And this is how we did it.
The Requirement - flexible right to buy permissions
This Magento client sells a range of products that they do not want members of the general public to be able to buy. In fact, only certain B2B customer groups should be able to buy these products. So, a “retail dealer” might be able to buy one kind for product, but a “distributor" might only be able to buy certain other products. The complication is that some customers are in reality both “retail dealers” and “distributors”. The desired outcome was a flexible system that allowed permissions to buy to be set at the product level - so at runtime we could determine if a particular customer had the right to buy this product, while also having the flexibility to change the permissions should the occasion arise.
The Solution - an observer database lookup
One issue with Magento open source is that its customer groups operate on a one-to-one basis - you can only be a member of one customer group. So, putting B2B customers into “dealer” or “distributor” customer groups would not provide the flexibility to allow some customers to span both groups for some products but be restricted from buying other products.So, the issue for this customer, was how to allow only certain customers to buy a product based on the product permissions themselves? The solution was to write an extension that used Magento' event-observer architecture to lookup who could buy this product.
A previous Magento agency had attempted to tackle this requirement by creating a 'yes/no' product attribute called “Saleable”. If a product was not “Saleable” the add to cart button was not rendered. The problem was that this solution took no account of different customer permissions - it was either on or off, on for all customers regardless of their customer group, or off for all customers, again regardless of their customer group.
Product Attributes and Customer Groups.
The solution we took was to add another product attribute and then map this attribute to the customer group a user belonged to. This required a lookup table that mapped product attribute values to customer groups. This meant we were able to say that customer groups A, B & D were able to buy this product if its attribute value = “ABD”, etc.
The issue was then how to perform the lookup at runtime without putting loads of business logic into Magento’s product view files.
Observer to the rescue.
Magento has the concept of event observers. When an event occurs, an observer can be created to observe and inject logic to determine outcomes. Joomla has a similar event model, and Drupal uses hooks in a similar fashion. So, in this instance I had to find the correct event to observe & then inject my logic to determine if this product was saleable to this user.
Here’s how We did it.
Customer and Product Attributes and the Look up Table
As mentioned above, each product has two attributes that we could use - Saleable & Sage Category (which is a value from their CRM system).
Customers also have an attribute called Customer Department - this provides the lookup between the product attribute Sage Category and the current customer.
Thus our extension creates a lookup table that maps the Sage Category to the customer department. As a Sage category has a one-to-many relationship to Customer Department, this allows us to say that this product can be bought by customers in any given customer group.
Our lookup table is very simple:
Once this table has been populated, at runtime we can search this table to see if this customer with a cust_dept can be found in the saleable table.
The observer is pretty straightforward too. It checks if the current customer is logged in, and if so, what is their Customer Department. It passes that value to a sql query that checks the lookup table and returns a boolean that is placed in a session variable. The event it observes is "catalog_controller_product_view" - this event is tiggered everytime a product is called.
The last piece of the jigsaw is the product view page. Here we render the add to cart button if the user is allowed to buy the product. We get the customer’s permission from our session variable:
Then we render the add to cart button based on this outcome.
What the above code does is check if the product is not Saleable. If that’s the case, can this user still buy it? If not render a CMS static block with some further information.
With this extension, which uses Magento’s event and observer architecture, our client is able to deploy flexible business rules to govern which user groups can buy a particular product. For this client, that has transformed their Magento shop into a true B2B platform, allowing them to sell directly to dealers and distributors without the risk of retail customers being able to bypass the distributors and dealer networks the client has spent two decades nurturing.
Do you have a Magento requirement? Please call us on 0116 279 3822 to have a chat.