简体   繁体   中英

How to display store fields from the product template?

I have a Drupal 8.7 site and the Drupal Commerce 2.14 module

In the template of my store to display the field field_professionnel_cgv I use this code 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.

Check how original function looks like at: /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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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