繁体   English   中英

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

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

我有一个Drupal 8.7网站和Drupal Commerce 2.14模块

在我的商店模板中以显示字段field_professionnel_cgv我使用以下代码TWIG:

{{ store.field_professionnel_cgv }}

如何从我的产品模板显示此字段。

您可以在模块中添加mymodule_preprocess_commerce_product()挂钩函数,然后在该函数内部获取商店实体并使用其数据设置twig变量,以便在模板内可用。

检查原始功能的外观:/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];
  }
}

因此,您必须像在此那样获取存储对象并分配twig变量。

暂无
暂无

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

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