简体   繁体   English

如何从产品实体(Drupal commerce)获取产品类型?

[英]How to get product type from product entity (Drupal commerce)?

I'm getting product object with a view.我正在获取带有视图的产品对象。 How to get its type?如何获取它的类型?

$view = Views::getView('get_all_products');
$view->execute();
foreach ($view->result as $row) { // products
  $product = $row->_entity;
  $product = \Drupal::service('entity.repository')->getTranslationFromContext($product); // Translating it
  $productType = $product-> ????
  ...

我认为更好的解决方案:

$product_type = $product->bundle();

A bit insane way I've found is:我发现的一个有点疯狂的方法是:

$productType = $product->get("type")[0]->get("target_id")->getValue();

Anyone has a better solution?有人有更好的解决方案吗?

暂无
暂无

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

相关问题 在 Drupal 8 Commerce 中,如何以编程方式从当前购物车中获取订单 ID 和产品变体 ID? - In Drupal 8 Commerce, how to get order id and product variation id from current cart programmatically? 在 Drupal 8. Drupal 商务。 如何根据产品字段隐藏视图(实体参考 - 分类) - In Drupal 8. Drupal Commerce. How to hide a view based on Product field (Entity Reference - Taxonomy) 如何在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 商业产品索引 solr 时出错 - Getting error during indexing solr from Drupal commerce product in date field Drupal 8 Commerce 2:在自定义产品模板中放置“添加到购物车”按钮 - Drupal 8 Commerce 2: Placing an “Add to Cart” button in a custom product template 如何使用 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 8 商务模块中如何从购物车中获取折扣金额? - In Drupal 8 commerce module how to get disounted amount from cart? Drupal 8使用产品类型和分类术语查看上下文过滤器 - Drupal 8 views contextual filter using product type and a taxonomy term 想要将Drupal 8用于新产品 - Want to use Drupal 8 for new product 如何查看产品类型的机器名称? - How to check the machine name of the product type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM