Add custom region in product template on Drupal 9 Commerce

In YOUTTHEME.theme file add code

/**
* Implements hook_preprocess_HOOK() for commerce-product templates.
*/
function MYTHEME_preprocess_commerce_product(array &$variables) {
   $regions = [
       'custom_product_region_one',
       'custom_product_region_two',
       'custom_product_region_three',
   ];
   foreach ($regions as $region) {
       $block_ids = \Drupal::entityQuery('block')
           ->condition('theme', 'MYTHEME')
           ->condition('region', $region)
           ->execute();
       foreach ($block_ids as $block_id) {
           $block = \Drupal\block\Entity\Block::load($block_id);
           if ($block && $block->access('view', \Drupal::currentUser())) {
               $variables[$region][] = \Drupal::entityTypeManager()->getViewBuilder('block')->view($block);
           }
       }
   }
}

After that add new region in YOUTTHEME.info.yml file

regions:
	custom_product_region_one: 'Region 1'
	custom_product_region_two: 'Region 2'
	custom_product_region_three: 'Region 3'

And finaly in commerce-product--full.html.twig put this code

{% if custom_product_region_one is not empty %}
 <div class="custom-product-region-one">
   {% for block in custom_product_region_one %}
     {{ block }}
   {% endfor %}
 </div>
{% endif %}
{% if custom_product_region_two is not empty %}
 <div class="custom-product-region-two">
   {% for block in custom_product_region_two %}
     {{ block }}
   {% endfor %}
 </div>
{% endif %}
{% if custom_product_region_three is not empty %}
 <div class="custom-product-region-three">
   {% for block in custom_product_region_three %}
     {{ block }}
   {% endfor %}
 </div>
{% endif %}

Clear caches and use it.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.