1

I am using Commerce 2.0 on Drupal 8.4.

I have sub-themed Bartik and placed a commerce-product.html.twig file in the sub-theme's /templates folder to experiment with theming a Commerce2 product page.

My product-type has a custom field called 'specifications'. The Manage-display for the product-type places both 'body' and 'specifications' in the Content portion (not disabled).

An example product of this product type was given the following values.

title = "Product 1 title"  
body = "Product 1 body"      
specifications = "Product 1 specifications"

The product-display page for Product 1 displays the following for the corresponding twig commands:

{{ product.title }} displays "Product 1 title"  
{{ product.body }} displays nothing    
{{ product.field_specifications }} displays nothing  

{{ product_entity.title.value }} displays "Product 1 title"  
{{ product_entity.body.value }} displays "<p>Product 1 body</p>"   
{{ product_entity.field_specifications.value }} displays "<p>Product 1 specifications</p>"   

Seeing that

{{ product.title }} and {{ product_entity.title.value }} agree.

why does

{{ product.body }} display nothing while {{ product_entity.body.value }} displays something.

and

{{ product.field_specifications }} display nothing while {{ product_entity.field_specifications.value }} displays something.

(An informative discussion of the field variables available in commerce-product.html.twig is here)

1

why does

{{ product.body }} display nothing while {{ product_entity.body.value }} displays something.

This is because {{ product.body }} returns an array not a single value. Body has other properties such as summary so you need to specify what you want from that field.

why does

{{ product.field_specifications }} display nothing while {{ product_entity.field_specifications.value }} displays something.

I'm not sure what field type you set field_specifications to, but I'm guessing you set it to a formatted text field. If that's the case it would return an array just like Body would. If field_specifications was simply a textfield, {{product.field_specifications}} would have worked.

  • Also side note. Just in case you didn't know. {{ product_entity.body.value }} would turn the html tags into visible text. So instead of the tags working like they should, visitors to the site would actually see the html tags on the page. Use {{ product_entity.body.value|raw }} to get around that. – Matt Oct 19 '17 at 21:39
  • Hi Matt, the {{ product_entity.body.value|raw }} displayed "Product 1 body" like you mentioned. Thank you. – sedroca Oct 19 '17 at 22:27
  • I changed the field type of field_specifications to text. However {{ product.field_specifications }} still displays nothing. {{ product_entity.field_specifications }} displays "Product 1 specifications" (no <p> and </p>) – sedroca Oct 19 '17 at 22:32
  • Regarding {{ product.body }}, I've only seen it used as just that. However the Devel-tab load variable seems to indicate that the Product.body array is product.body.x-default.0.value. However {{ product.body.x-default.0.value }} displays "0" – sedroca Oct 19 '17 at 22:39
  • Did you actually delete field_specifications and create a new one of the same name? Because there isn't a way to actually change the field type after it's already created. – Matt Oct 20 '17 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.