如何使用Drupal Commerce-2.x以编程方式使用product_id加载所有变体及其ID?

时间:2018-08-04 12:10:49

标签: drupal-8 drupal-commerce

如何在Drupal commerce-2.x中的表单alter上从product_id加载variant_id?

1 个答案:

答案 0 :(得分:1)

您需要首先加载Product variation entity,需要获取Product variation ids from loaded product,然后提取SKU,价格,货币代码等。

$parameter = \Drupal::routeMatch()->getParameter('commerce_product');
$product = \Drupal\commerce_product\Entity\Product::load((int)$parameter->id());

/*Load Product Variations*/ $entity_manager = \Drupal::entityManager(); $product_variation = $entity_manager->getStorage('commerce_product_variation')->load((int)$product->getVariationIds()[0]); $price_number = $product_variation->get('price')->getValue()[0]['number']; $price_currency = $product_variation->get('price')->getValue()[0]['currency_code'];