2Hats . 3 minutes
December 5, 2023

How to Set Up Customer-Based Price Calculation in Shopware

Introduction

Currently, in Shopware it is not possible to provide customer-based prices or discounts for products using tags/categories. In this article, we will discuss how to provide a price based on the customer.

To do customer-based price calculations that can’t be met with Shopware’s default options, we have to decorate the service ProductPriceCalculator. It comes with a “calculate” method, which you can decorate and customise. So you should have a basic understanding of service decoration in Shopware.

We will be creating an admin section to map products, and customers with price or discount. Based on this mapping price calculation will be done in the frontend.

But before we proceed, our guide involves using Shopware 6.5, So we assume you have intermediate knowledge of Shopware. If not, Jumping right into a Framework as a 1st step is not such a good idea, so if you are a newcomer we suggest you get some basic knowledge before proceeding here.

Step 1: Create an Admin section for mapping price

Create a custom entity and admin CRUD section to insert the following details.

Quantity range.

For infinite quantities, we can make the “To Quantity” field Null. 

Text box to enter price or discount(%).

Either price or discount has to be selected.

Product/tag

Either select a product or product tag. You can also extend functionality to add a category select to apply the rule to all products in that category.

Customer

Select a customer for the rules to be applied, you can also add a customer group, so that all customers within that group applied. These fields can be changed or updated depending on your requirements.

Step 2: Decorating the price calculator

In this section, we have been using the “adjust a service” method to decorate the price calculator function. If you are unaware of this, we suggest you get some basic knowledge before proceeding here.

We must add the ProductPriceCalculator decorator in the services.xml file with the attribute decorates pointing to the service we want to decorate. We can also add our custom entity repository as an argument.

In the ProductPriceCalculator class, we could accept arguments in the construct.

Recreate calculate function inside the decorator class and trigger core calculate function, then fetch customer information from saleschannel context. 

Then loop through the products and fetch the calculated price and list price. 

Also, fetch customer-specific prices from the custom entity.

From our custom entity, we could fetch the price or discount based on product/tag and customer. Here you can customize it based on your requirements.

If a Custom entity has only one price for all quantities of a product. Set the calculated price and cheapest price and skip the rest of the part.

You can set custom prices according to conditions in the custom entity, you can modify it according to the functionality in the backend.

If more than different prices for different quantities, then we need to loop through each quantity price and set the price accordingly.

 

Since here we create custom prices based on the customer, the Customer has to always log in to the store to reflect this price update.

Conclusion

With Shopware 6.5, you can now implement customer-based price calculations. It’s super easy! Just follow these steps and make sure to tweak the code to fit your business needs. Happy coding!

blog
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
Aneesh ceo
Aneesh Sreedharan
Founder & CEO, 2Hats Logic Solutions
Subscribe to our Newsletter
Arsha Contact

    Stay In The Loop!

    Subscribe to our newsletter and learn about the latest digital trends.