Skip to main content

Change a Product Price in Cart

Get the original Product

First, get the original product. If you're working within the Cart, this should be no problem. Alternatively, use the CachedProductDetailRoute to load the product by it's ID. We do this because we need the original CalculatedPrice and TaxRules.

Create a new Calculated Price

In my example, i am adding a surcharge to the original Price. I first save the original CalculatedPrice in the variable $price and then we're good to go:

$newPrice = new CalculatedPrice(
($price->getUnitPrice() + $surcharge),
(($price->getUnitPrice() + $surcharge)*$price->getQuantity()),
$this->calculator->calculateGrossTaxes((($price->getUnitPrice() + $surcharge)*$price->getQuantity()), $price->getTaxRules()),
$price->getTaxRules(),
$price->getQuantity()
);
  • We add the surcharge to the original Unit Price.
  • We multiply the new Unit Price with the product quantity.
  • We calculate the new Taxes with the TaxCalculator class that you need to import. This is the most important step and the reason this article exists. You don't want to mess up here otherwise the Tax calculation will break.
  • Add the original TaxRules ...
  • ... and Quantity

Replace the original Price

To do this, simply write $originalProduct->setPrice($newPrice)