Displaying Product Combinations With Discounts on a Product Page in Prestashop

August 25, 2016 Dima Doroshchenko Prestashop
Displaying Product Combinations With Discounts on a Product Page in Prestashop

Our goal is to display available product combinations and their prices on a product page. For example, our store proposes some loyalty discount program, and we should show all product prices in a proper way. The following solution will fit for you in case you have a variety of product combinations and a set of user group discount rules.

Among them are: Distributors, Regular customers, Special customers, etc.

There are several methods how to apply a discount in PrestaShop:

Group discount — you can set up a discount in % for a certain User Group:
How to Add Product Combinations Involving the Use of Discount System on a Product Page in PrestaShop

Catalog Price Rules — allow setting up a discount depending on a product quantity, user group, combination, etc. Moreover it’s possible to set up a discount for each product separately (Specific Price):
How to Add Product Combinations Involving the Use of Discount System on a Product Page in PrestaShop

Combination Impact on price — set price for every combination:
How to Add Product Combinations Involving the Use of Discount System on a Product Page in PrestaShop

Moreover there are some types of discount rules based on the cart items (Cart Rules), but we won’t consider this case in this post.

By default a price is displayed only for the selected combination. After a product attribute is changed, a new price is calculated according to the applied discount rules. So we just need to understand the process of these calculations and apply it to all possible combinations.

Let’s take a look at what happens when you choose a certain combination, and how PrestaShop calculates a new product price.

All described code is taken from the default-bootstrap/js/product.js file.

Selected combination is passed in the URI anchor, so a JavaScript global variable is in our disposal.
How to Add Product Combinations Involving the Use of Discount System on a Product Page in PrestaShop

Let’s take a look at the Combinations array. The following JavaScript object is filled in the product.tpl template by the ProductController class.
How to Add Product Combinations Involving the Use of Discount System on a Product Page in PrestaShop

This object contains price (Combination Impact on price) and specific_price (Catalog Price Rules) elements.

Also we have access to global price variables (ProductPrice, productPriceTaxIncluded, etc.) that affect the final product price with applied group discounts and country based taxes (also calculated in the ProductController).

So now we are able to display prices with Catalog Price Rules and Impact on Price discounts applied.

Here is a custom template with combinations and applied discounts.
How to Add Product Combinations Involving the Use of Discount System on a Product Page in PrestaShop

To achieve the same result you need to develop a custom module with template for calculations according to the available Combinations based on the ProductController. But what if we want to create Price Rule for a purchase of 10+ or even 100+ products? You expect Combinations to contain additional information about all available discounts. Yes, this could be rather helpful. But it is not the way PrestaShop is built.

If you add a PriceRule with the From quantity parameter and reload a page, you will see that a new Volume Discounts table. And in case you increase the Quantity JavaScript will parse this table and update the current price. Such option does not suit us at all. The data that we need is not passed into JavaScript global variables, we don’t need the Volume Price table as well.

JavaScript adds anchor to the page URL after you select a combination. Thereafter the script calculates the final price with the applied discount and updates value on the page.

How to Add Product Combinations Involving the Use of Discount System on a Product Page in PrestaShop

So the following solution can suit us:
Let’s create a hook in our module

We are going to use 2 native methods from the ProductController: getQuantityDiscounts() and formatQuantityDiscounts():

Now we can create a JavaScript variable in the template:

How to Add Product Combinations Involving the Use of Discount System on a Product Page in PrestaShop
Finally we have all necessary data in order to display “from quantity” discounts properly.



2 comments

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *