简体   繁体   English

如何显示产品模板中的商店字段?

[英]How to display store fields from the product template?

I have a Drupal 8.7 site and the Drupal Commerce 2.14 module 我有一个Drupal 8.7网站和Drupal Commerce 2.14模块

In the template of my store to display the field field_professionnel_cgv I use this code TWIG : 在我的商店模板中以显示字段field_professionnel_cgv我使用以下代码TWIG:

{{ store.field_professionnel_cgv }}

How to display this field from the template of my products. 如何从我的产品模板显示此字段。

You could add to your module mymodule_preprocess_commerce_product() hook function and inside that function get store entity and set twig variable with data from it so it will be available inside your template. 您可以在模块中添加mymodule_preprocess_commerce_product()挂钩函数,然后在该函数内部获取商店实体并使用其数据设置twig变量,以便在模板内可用。

Check how original function looks like at: /modules/contrib/commerce/modules/product/commerce_product.module 检查原始功能的外观:/modules/contrib/commerce/modules/product/commerce_product.module

function template_preprocess_commerce_product(array &$variables) {
  /** @var Drupal\commerce_product\Entity\ProductInterface $product */
  $product = $variables['elements']['#commerce_product'];

  $variables['product_entity'] = $product;
  $variables['product_url'] = $product->isNew() ? '' : $product->toUrl();
  $variables['product'] = [];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['product'][$key] = $variables['elements'][$key];
  }
}

So you'll have to fetch store object and assign twig variable like it's done here. 因此,您必须像在此那样获取存储对象并分配twig变量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM