【问题标题】:How to get product type from product entity (Drupal commerce)?如何从产品实体(Drupal Commerce)获取产品类型?
【发布时间】:2020-04-03 17:08:12
【问题描述】:

我正在获取带有视图的产品对象。如何获取它的类型?

$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-> ????
  ...

【问题讨论】:

    标签: drupal-8 commerce


    【解决方案1】:

    我认为这是更好的解决方案:

    $product_type = $product->bundle();
    

    【讨论】:

      【解决方案2】:

      我发现的一个有点疯狂的方法是:

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

      谁有更好的解决方案?

      【讨论】:

        猜你喜欢
        • 2013-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多