Reading list Switch to dark mode

    Override product price calculation in PrestaShop 8.1

    Updated 21 August 2023

    In this blog, we are going to learn how to override a product price calculation in PrestaShop.

    Usually, we use the default price of the product for the customers. Sometimes we need to override the price of the product. This price can be used for every customer or some specific customers. It completely depends on our requirements.

    Product price overriding is mostly used for changing some extra amount or giving some extra discount to the customer.

    To override the price of the product, we need to follow the following steps.

    Let’s say, we want 10% less price than the actual price of the product for all logged-in customers.

    Searching for an experienced
    Prestashop Company ?
    Find out More

    First of all, we need to register a new hook named ‘actionProductPriceCalculation’

    Note: This hook is available since PrestaShop version 8.1

    We use this hook to perform the custom calculation as per our requirements.

    Now we define the definition of hook functionality as follows:

    public function hookActionProductPriceCalculation($params)
    {
        // Create your logic here
        // return the new price value after custom calculation
    
        $oldPrice = $params['price'];
        $newPrice = $oldPrice;
        if ($this->context->customer->id) {
            $newPrice = $oldPrice - ($oldPrice * (10 / 100));
        }
        $params['price'] = $newPrice;
    }

    By applying the above code, we will get the result as follows:

    For visitor product price is $29.00 i.e. actual price of the product.

    Before-calculations

    For logged-in customers, the product price is $26.10

    After-calculation

    That’s all about this blog.

    If any issues or doubts in the above step, please feel free to let us know in the comment section.

    We would be happy to help.

    You can also learn How to Override Symfony Grid Listing in PrestaShop.

    You can also explore our PrestaShop Development Services and a large range of quality PrestaShop Modules

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home