获取 Product Variation 的计算价格

Get a calculated price of Product Variation

我创建了优惠类型为 - Fixed amount off each matching product 的促销并适用于 - Specific product。在我的自定义块中,我得到了产品实体,但在那里看不到任何促销活动。我怎样才能得到它?

UPD:试图通过 commerce_order.price_calculator 服务解决这个问题。

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);

所以,calculate 方法 returns me a PriceCalculatorResult object with 2 properties, calculatedPrice and basePrice 但它们是相同的,好像打折没有应用,但我看到它应用在购物车中。

通过将第 4 个参数 $adjustment_types 传递给 $commercePriceCalc->calculate 方法解决了这个问题。

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

所以,代码的最终版本

$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);