Get a calculated price of Product Variation

1.4k Views Asked by At

I created a promotion with offer type - Fixed amount off each matching product and applies to - Specific product. In my custom block I get the product entity, but don't see any promotions there. How can I get it?

UPD: Tried to solve this problem with via commerce_order.price_calculator service.

commercePriceCalc = \Drupal::service('commerce_order.price_calculator');
$context = new Context(\Drupal::entityTypeManager()->getStorage('user')->load(1), 
                     \Drupal::entityTypeManager()->getStorage('commerce_store')->load(1));
$prices = $commercePriceCalc->calculate($slide->field_product->entity, 1, $context);

So, calculate method returns me a PriceCalculatorResult object with 2 properties, calculatedPrice and basePrice but they are identical, as if the discount didn't apply, but I see it applied in a cart.

1

There are 1 best solutions below

0
Sizuji On BEST ANSWER

Solved this problem, by passing the 4th argument $adjustment_types to the $commercePriceCalc->calculate method.

$adjustment_types = array("promotion" => "promotion");

So, the final version of the code

$commercePriceCalc = \Drupal::service('commerce_order.price_calculator');
$context = new Context(\Drupal::currentUser(), 
                     \Drupal::entityTypeManager()->getStorage('commerce_store')->load($store_id));
$prices = $commercePriceCalc->calculate($slide->field_product->entity, 1, $context, $adjustment_types);