Jump to content

Price override in hook ActionProductPriceCalculation removes all taxes


neoweiter

Recommended Posts

Hi everyone

I made a module with this hook

    public function hookActionProductPriceCalculation($params)
    {
        if (empty($params['cart']) || !Validate::isLoadedObject($params['cart'])) {
            return;
        }

        $id_product = (int)$params['id_product'];

        $prices = PriceController::getStaticPrices($id_product); // Custom Class with new price calculation

        if ($prices && isset($prices['final_price'])) {
            $params['price'] = (float)$prices['final_price'];
        }
    }

this simply overrides all my products Tax Excl. prices in the Cart and in final Order.

This works well, but strangely the Cart Total does not count the Taxes anymore. My total Tax Incl. is the same as the total Tax Exc. amount like if the total tax was 0.00

Why is that ? How can I fix the total tax incl ?

Thanks

Edited by neoweiter (see edit history)
Link to comment
Share on other sites

test with:

public function hookActionProductPriceCalculation($params)
{
    // Validate cart object
    if (empty($params['cart']) || !Validate::isLoadedObject($params['cart'])) {
        return;
    }

    $id_product = (int)$params['id_product'];
    
    // Get custom price from your PriceController
    $prices = PriceController::getStaticPrices($id_product);

    if ($prices && isset($prices['final_price'])) {
        $price_excl = (float)$prices['final_price']; // Assume this is tax-excluded price

        // Load product to get tax info
        $product = new Product($id_product);
        $taxCalculator = $product->getTaxCalculator($params['cart']->id_address_delivery);

        // Set tax-exclusive and tax-inclusive prices
        $params['price'] = $price_excl;
        $params['price_tax_exc'] = $price_excl;
        $params['price_tax_inc'] = $taxCalculator->getInclusivePrice($price_excl);
    }
}

 

  • Like 1
Link to comment
Share on other sites

Thanks for your reply. 

I tested it but with no success.

After some research, it seems that no hook can achieve this, as it's coming too late in the calculation process. (It comes after prestashop has already calculated taxes)

I'm trying now to do it by overriding Product::getPriceStatic method.

Link to comment
Share on other sites

If it can help, I solved it by overriding the Product::priceCalculation method. I replaced $price with my new price calculation, just before the line

$price = $product_tax_calculator->addTaxes($price);

Maybe not the mot elegant, but it works, and I don't think there is another way of doing it with hooks as I tried them all.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...