如何在Drupal commerce-2.x中的表单alter上从product_id加载variant_id?
答案 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'];