I'm not completely sure, if it was designed or not. The default price calculator doesn't include the given quantity once the price is queried without any adjustments in \Drupal\commerce_order\PriceCalculator::calculate()
Current code snippet:
if (empty($adjustment_types) || empty($processors)) {
return new PriceCalculatorResult($resolved_price, $resolved_price);
}
Proposed solution:
Simply multiply the resolved price before creating a the PriceCalculatorResult
if (empty($adjustment_types) || empty($processors)) {
return new PriceCalculatorResult($resolved_price->multiply($quantity), $resolved_price);
}
This behaviour can cause unexpected prices once there are no adjustments but quantity is provided.
Comments
Comment #2
bpstr commentedComment #3
vishalghyv commentedPrice is being resolved over chainPriceResolver. Even then could you suggest a way to reproduce I am not able to. And what kind of unexpected behavior.
Comment #4
bpstr commentedI created a simple field formatter for order items to display prices without tax, but including promotion.
For this task, I used the
commerce_order.price_calculatorservice, which passes the givenPurchasableEntityInterfacethrough thechainPriceResolverand then returnsthe calculated price by creating an order item and applying the given parameters on that instance.
If you take a look at the code of
\Drupal\commerce_order\PriceCalculator::calculate, thequantity is only regarded when there are
$adjustment_typesor$processorsprovided.In my case, there were no processors found when iterating through
$adjustment_types, but Ibelieve this is the same case, when an empty array (default value) is passed as a fourth argument.
Another great example is a helper service to provide consistent price calculation through the site.
I had to write an extra check because the 'promotion' adjustment type did not have a processor, and the
quantity was not regarded when the service returned the 'calculated price'.
(The second method,
calculateTotalPrice()always returned the correct amount.)Comment #5
loze commentedI also am experiencing this same behavior. Quantity is ignored if the adjustments array is empty.
Comment #6
loze commentedhere is a patch that only returns early if either the processors or adjustments are empty AND the quantity is only 1
This allows me to calculate a total price of an item with a quantity > 1 and no processor/adjustments.
thanks.
Comment #7
loze commentedI'm sorry, disregard my last comment. The solution in the OP is better.
Here is a patch for that.
Comment #8
luksakThe patch in #7 works for me! Thanks!
Comment #9
jsacksick commentedI'm fine committing the fix, considering the quantity is taken into account when there are adjustments.
My only problem is
PriceCalculatorResult::getCalculatedPrice()which currently says the following:Comment #12
jsacksick commentedJust changed the comment to:
And committed, thank you!