Drupal 8 theme suggestion for Commerce product page template per product type

By default you get the following page template suggestions for a Commerce product:

page--product--[ID].html.twig
page--product--%.html.twig
page--product.html.twig
page.html.twig

Here's a template suggestion for custom page templates per product type:

/**
  * Add page templates for Commerce product types
  * Example: page--product--event.html.twig
  */
/**
* Implements hook_theme_suggestions_page_alter().
*/
function THEMENAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if ($product = \Drupal::routeMatch()->getParameter('commerce_product')) {
    $suggestions[] = 'page__product__' . $product->bundle();
  }
}

Tags:

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.