简体   繁体   English

如何使用 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-2.x 中的表单更改上从 product_id 加载变体_id?

You need first to load Product variation entity , You need to get Product variation ids from loaded product and then extract SKU, Price, Currency Code and etc.您首先需要加载Product variation entity ,您需要Product variation ids from loaded product获取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'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 drupal 8 commerce 2 - 如何以编程方式取消发布所有已发布的产品及其产品变体 - drupal 8 commerce 2 - How to unpublish all published products and their products variations programmatically 在 Drupal 8 Commerce 中,如何以编程方式从当前购物车中获取订单 ID 和产品变体 ID? - In Drupal 8 Commerce, how to get order id and product variation id from current cart programmatically? 如何从产品实体(Drupal commerce)获取产品类型? - How to get product type from product entity (Drupal commerce)? 如何在Drupal 8的主题模板中覆盖商务产品详细信息tpl.twig(commerce-product.html.twig)? - How to overwrite commerce product details tpl.twig (commerce-product.html.twig) in theme template in drupal 8? 在 Drupal 8. Drupal 商务。 如何根据产品字段隐藏视图(实体参考 - 分类) - In Drupal 8. Drupal Commerce. How to hide a view based on Product field (Entity Reference - Taxonomy) 在 Drupal 8 的视图块中使用 Content ID 上下文过滤器 - Using a Content ID Contextual Filter in a Views Block in Drupal 8 如何通过特定于 Drupal 的 Twig 中的 ID 获取特定节点为 object? - How to get specific node as object by its ID in Drupal-specific Twig? 如何获取Drupal 8 Form中的触发按钮id - How to get triggered button id in Drupal 8 Form Drupal 8 Twig 用户 ID? - Drupal 8 Twig User ID? Drupal 8.2.0 Commerce 2.x安装失败 - Drupal 8.2.0 Commerce 2.x installation failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM