[英]Get a calculated price of Product Variation
I created a promotion with offer type - Fixed amount off each matching product
and applies to - Specific product
.我创建了一个优惠类型的促销活动 -
Fixed amount off each matching product
并适用于 - 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. 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);
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.所以,
calculate
方法返回给我一个PriceCalculatorResult
object 有 2 个属性, calculatedPrice
和basePrice
但它们是相同的,好像折扣不适用,但我看到它应用在购物车中。
Solved this problem, by passing the 4th argument $adjustment_types
to the $commercePriceCalc->calculate
method.通过将第四个参数
$adjustment_types
传递给$commercePriceCalc->calculate
方法解决了这个问题。
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.