如何使用 Drupal Commerce-2.x 以编程方式使用 product_id 加载所有变体及其 ID?
How to load all variations and its id by using product_id programmatically using Drupal Commerce-2.x?
如何在 Drupal commerce 的表单更改中从 product_id 加载 variations_id-2.x?
您需要先加载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'];
如何在 Drupal commerce 的表单更改中从 product_id 加载 variations_id-2.x?
您需要先加载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'];