0

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.

Sizuji
  • 868
  • 2
  • 9
  • 26
  • 1
    I haven't had to do this. But the official docs suggest using a different service: commerce_price.chain_price_resolver. Source: https://docs.drupalcommerce.org/commerce2/developer-guide/adapting-from-1x/price-calculation – Brian Wagner Oct 01 '19 at 14:51
  • Thanks for your answer, but I've already solved this issue, check my answer – Sizuji Oct 02 '19 at 16:36

1 Answers1

0

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);
Sizuji
  • 868
  • 2
  • 9
  • 26