Change Specific Category Layout By ID in Magento 2

We can change Category page layout for specific category only through the Catalog_category_view_selectable_ID_Name.xml file.

When we create a website in Magento eCommerce shopping framework usually we used to face many challenges, one of them is to design the theme.

Here we are going to tell you about how you can customize the Magento 2 theme category page layout for specific category.

In order to customize the specific category page layout we need to select the id.

 

Catalog Category View Selectable: Change Specific Category Layout By ID

Here we have copied the default magento xml file from below location:

vendor/magento/module-catalog/view/frontend/layout/catalog_category_view.xml

And customize according to your needs or you can copy the below code.

 

catalog_category_view_selectable_128_CARTIER.xml

Here,

128 is a category ID

CARTIER is the name which you want to give foe custom layout update.

 

<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body> 
<referenceContainer name="content">
<block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="product/CARTIER.phtml">
<block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
<block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName">
<argument name="name" xsi:type="string">product_list_toolbar</argument>
</action>
</block>
<container name="category.product.list.additional" as="additional" />
</referenceContainer>
<!-- <move element="category.products.list" destination="main.content" after="columns.top" /> -->
</body>
</page>

 

When you have created the file, after that you will have to run below commands of Magento 2, after that you go to the admin side and select the store, then go to Design and select the file and then save and then clear the cache.

Change Specific Category Layout

 

Here you see that the file that has been created is visible here, you have to select it.

Custom Layout for Specific Category

 

Now you can copy the file from below location:

vendor/magento/module-catalog/view/frontend/templates/product/list.phtml

And you can change the functionality according to your needs.

 

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\Framework\App\Action\Action;

?>
<?php
/**
* Product list template
*
* @var $block \Magento\Catalog\Block\Product\ListProduct
* @var \Magento\Framework\Escaper $escaper
* @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
*/
?>
<?php
$_productCollection = $block->getLoadedProductCollection();
/** @var \Magento\Catalog\Helper\Output $_helper */
$_helper = $block->getData('outputHelper');
?>
<?php if (!$_productCollection->count()): ?>
<div class="message info empty">
<div><?= $escaper->escapeHtml(__('We can\'t find products matching the selection.')) ?></div>
</div>
<?php else: ?>
<?= $block->getToolbarHtml() ?>
<?= $block->getAdditionalHtml() ?>
<?php
if ($block->getMode() === 'grid') {
$viewMode = 'grid';
$imageDisplayArea = 'category_page_grid';
$showDescription = false;
$templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
} else {
$viewMode = 'list';
$imageDisplayArea = 'category_page_list';
$showDescription = true;
$templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::FULL_VIEW;
}
/**
* Position for actions regarding image size changing in vde if needed
*/
$pos = $block->getPositioned();
?>
<div class="products wrapper <?= /* @noEscape */ $viewMode ?> products-<?= /* @noEscape */ $viewMode ?>">
<ol class="products list items product-items">
<?php /** @var $_product \Magento\Catalog\Model\Product */ ?>
<?php foreach ($_productCollection as $_product): ?>
<li class="item product product-item">
<div class="product-item-info"
id="product-item-info_<?= /* @noEscape */ $_product->getId() ?>"
data-container="product-<?= /* @noEscape */ $viewMode ?>">
<?php
$productImage = $block->getImage($_product, $imageDisplayArea);
if ($pos != null) {
$position = 'left:' . $productImage->getWidth() . 'px;'
. 'top:' . $productImage->getHeight() . 'px;';
}
?>
<?php // Product Image ?>
<a href="<?= $escaper->escapeUrl($_product->getProductUrl()) ?>"
class="product photo product-item-photo"
tabindex="-1">
<?= $productImage->toHtml() ?>
</a>
<div class="product details product-item-details">
<?php $_productNameStripped = $block->stripTags($_product->getName(), null, true); ?>
<strong class="product name product-item-name">
<a class="product-item-link"
href="<?= $escaper->escapeUrl($_product->getProductUrl()) ?>">
<?=/* @noEscape */ $_helper->productAttribute($_product, $_product->getName(), 'name')?>
</a>
</strong>
<?= $block->getReviewsSummaryHtml($_product, $templateType) ?>
<?= /* @noEscape */ $block->getProductPrice($_product) ?>

<?= $block->getProductDetailsHtml($_product) ?>

<div class="product-item-inner">
<div class="product actions product-item-actions">
<div class="actions-primary">
<?php if ($_product->isSaleable()):?>
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
<form data-role="tocart-form"
data-product-sku="<?= $escaper->escapeHtml($_product->getSku()) ?>"
action="<?= $escaper->escapeUrl($postParams['action']) ?>"
method="post">
<input type="hidden"
name="product"
value="<?= /* @noEscape */ $postParams['data']['product'] ?>">
<input type="hidden"
name="<?= /* @noEscape */ Action::PARAM_NAME_URL_ENCODED ?>"
value="<?=
/* @noEscape */ $postParams['data'][Action::PARAM_NAME_URL_ENCODED]
?>">
<?= $block->getBlockHtml('formkey') ?>
<button type="submit"
title="<?= $escaper->escapeHtmlAttr(__('Add to Cart')) ?>"
class="action tocart primary"
disabled>
<span><?= $escaper->escapeHtml(__('Add to Cart')) ?></span>
</button>
</form>
<?php else:?>
<?php if ($_product->isAvailable()):?>
<div class="stock available">
<span><?= $escaper->escapeHtml(__('In stock')) ?></span></div>
<?php else:?>
<div class="stock unavailable">
<span><?= $escaper->escapeHtml(__('Out of stock')) ?></span></div>
<?php endif; ?>
<?php endif; ?>
</div>
<?= strpos($pos, $viewMode . '-primary') ?
/* @noEscape */ $secureRenderer->renderStyleAsTag(
$position,
'product-item-info_' . $_product->getId() . ' div.actions-primary'
) : '' ?>
<div data-role="add-to-links" class="actions-secondary">
<?php if ($addToBlock = $block->getChildBlock('addto')): ?>
<?= $addToBlock->setProduct($_product)->getChildHtml() ?>
<?php endif; ?>
</div>
<?= strpos($pos, $viewMode . '-secondary') ?
/* @noEscape */ $secureRenderer->renderStyleAsTag(
$position,
'product-item-info_' . $_product->getId() . ' div.actions-secondary'
) : '' ?>
</div>
<?php if ($showDescription): ?>
<div class="product description product-item-description">
<?= /* @noEscape */ $_helper->productAttribute(
$_product,
$_product->getShortDescription(),
'short_description'
) ?>
<a href="<?= $escaper->escapeUrl($_product->getProductUrl()) ?>"
title="<?= /* @noEscape */ $_productNameStripped ?>"
class="action more"><?= $escaper->escapeHtml(__('Learn More')) ?></a>
</div>
<?php endif; ?>
</div>
</div>
</div>
<?= strpos($pos, $viewMode . '-actions') ?
/* @noEscape */ $secureRenderer->renderStyleAsTag(
$position,
'product-item-info_' . $_product->getId() . ' div.product-item-actions'
) : '' ?>
</li>
<?php endforeach; ?>
</ol>
</div>
<?= $block->getToolbarHtml() ?>
<?php if (!$block->isRedirectToCartEnabled()): ?>
<script type="text/x-magento-init">
{
"[data-role=tocart-form], .form.map.checkout": {
"catalogAddToCart": {
"product_sku": "<?= $escaper->escapeJs($_product->getSku()) ?>"
}
}
}
</script>
<?php endif; ?>
<?php endif; ?>

 

After that you have to run the below Magento 2 command:

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy -f

php bin/magento cache:clean

php bin/magento cache:flush

 

Now you can check you category layout

 

Change Specific Category Layout By ID

 

Recommended Post:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

Elasticsearch: How to Install Elasticsearch in Ubuntu 18.04

 

Also Read:

Show Custom Attributes in Magento 2 on The Frontend Side

Magento 2 Interview Questions: Most Important Question & Answer

Category Top Navigation: How to Change Maximal Depth in Magento 2

Category Top Navigation: How to Change Maximal Depth in Magento 2

Here i will show you how to grab all of the child categories and their subcategories in category top navigation menu.

By the way, if seen, there is a default function in the admin side of Magento which provides the convenience of setting the maximal depth of navigation for your menu as needed, but this functionality does not work in most Magento 2 version.

So let us know how we add any nested sub-category to Magento 2 category top navigation according to our needs or how we show four more depth sub-categories in the navigation menu.

By default magento2 provide depth of menu level is 4, But as you can see here the nested subcategory has more than 4 category depth so it won't show on the front-end.

Maximal Depth in Magento 2

 

Magento 2 Category Top Navigation Maximal Depth

Maximal depth in the category navigation decide the level of subcategory which shows the level of menu on the store view.

On the Admin sidebar, go to Stores > Settings > Configuration.

Expand Catalog and click on the Catalog underneath.

Expand the Category Top Navigation section.

You will see something like below screen. This section of mximal depth decide the level of category navigation menu in the store view.

This depth of the top navigation of category has a global configuration scope, so this setting applies to all websites, stores, and store views in the Magento 2.

Category Navigation Top Menu

How actually its should be visible on the frontend but it's wont be visible like the below screen.

You have to create the block and template for the just like below class.

Magento 2 Category Top Navigation Menu

 

Effective Category Level in Magento 2 for SEO

Search engine optimisation is the most important factor of online business success so the category structure of your catalog can influence how well your site is indexed by search engines.

The more deeply nested a category, the less likely it is to be thoroughly indexed. Magento 2 nested category level or depth of the category level should be between one and three visible levels which would be most effective.

 

How to Customise Maximal Depth Level of Category Navigation Menu

You just have to create a block for this and then you can get the data on the store view through the custom.phtml template.

<?php
namespace Wishusucess\CustomMenu\Block;

class Topmenu extends \Magento\Framework\View\Element\Template
{

protected $_categoryHelper;
protected $_categoryFlatConfig;
protected $_topMenu;
protected $_categoryFactory;
protected $_helper;
protected $_filterProvider;
protected $_blockFactory;
protected $_custommenuConfig;
protected $_storeManager;

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Helper\Category $categoryHelper,
\Wishusucess\CustomMenu\Helper\Data $helper,
\Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Theme\Block\Html\Topmenu $topMenu,
\Magento\Cms\Model\Template\FilterProvider $filterProvider,
\Magento\Cms\Model\BlockFactory $blockFactory
) {

$this->_categoryHelper = $categoryHelper;
$this->_categoryFlatConfig = $categoryFlatState;
$this->_categoryFactory = $categoryFactory;
$this->_topMenu = $topMenu;
$this->_helper = $helper;
$this->_filterProvider = $filterProvider;
$this->_blockFactory = $blockFactory;
$this->_storeManager = $context->getStoreManager();

parent::__construct($context);
}

public function getCategoryHelper()
{
return $this->_categoryHelper;
}

public function getCategoryModel($id)
{
$_category = $this->_categoryFactory->create();
$_category->load($id);

return $_category;
}

public function getHtml($outermostClass = '', $childrenWrapClass = '', $limit = 0)
{
return $this->_topMenu->getHtml($outermostClass, $childrenWrapClass, $limit);
}

public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
{
return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
}

public function getChildCategories($category)
{
if ($this->_categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) {
$subcategories = (array)$category->getChildrenNodes();
} else {
$subcategories = $category->getChildren();
}

return $subcategories;
}

public function getActiveChildCategories($category)
{
$children = [];
if ($this->_categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) {
$subcategories = (array)$category->getChildrenNodes();
} else {
$subcategories = $category->getChildren();
}
foreach($subcategories as $category) {
if (!$category->getIsActive()) {
continue;
}
$children[] = $category;
}
return $children;
}

public function getBlockContent($content = '') {
$content = html_entity_decode($content);
if(!$this->_filterProvider)
return $content;
return $this->_filterProvider->getBlockFilter()->filter(trim($content));
}

public function getCustomBlockHtml($type='after') {
$html = '';

$block_ids = $this->_custommenuConfig['custom_links']['staticblock_'.$type];

if (!$block_ids) return '';

$block_ids = preg_replace('/\s/', '', $block_ids);
$ids = explode(',', $block_ids);
$store_id = $this->_storeManager->getStore()->getId();

foreach($ids as $block_id) {
$block = $this->_blockFactory->create();
$block->setStoreId($store_id)->load($block_id);

if(!$block) continue;

$block_content = $block->getContent();

if(!$block_content) continue;

$content = $this->_filterProvider->getBlockFilter()->setStoreId($store_id)->filter($block_content);
if(substr($content, 0, 4) == '<ul>')
$content = substr($content, 4);
if(substr($content, strlen($content) - 5) == '</ul>')
$content = substr($content, 0, -5);

$html .= $content;
}

return $html;
}
public function getSubmenuItemsHtml($children, $level = 1, $max_level = 0, $column_width=12, $menu_type = 'fullwidth', $columns = null)
{
$html = '';

if(!$max_level || ($max_level && $max_level == 0) || ($max_level && $max_level > 0 && $max_level-1 >= $level)) {
$column_class = "";
if($level == 1 && $columns && ($menu_type == 'fullwidth' || $menu_type == 'staticwidth')) {
$column_class = "col-sm-".$column_width." ";
$column_class .= "mega-columns columns".$columns;
}
$html = '<ul class="subchildmenu '.$column_class.'">';
foreach($children as $child) {
$cat_model = $this->getCategoryModel($child->getId());

$rt_menu_hide_item = $cat_model->getData('rt_menu_hide_item');

if (!$rt_menu_hide_item) {
$sub_children = $this->getActiveChildCategories($child);

$rt_menu_cat_label = $cat_model->getData('rt_menu_cat_label');
$rt_menu_icon_img = $cat_model->getData('rt_menu_icon_img');
$rt_menu_font_icon = $cat_model->getData('rt_menu_font_icon');

$item_class = 'level'.$level.' ';
if(count($sub_children) > 0)
$item_class .= 'parent ';
$html .= '<li class="ui-menu-item '.$item_class.'">';
if(count($sub_children) > 0) {
$html .= '<div class="open-children-toggle"></div>';
}
if($level == 1 && $rt_menu_icon_img) {
$html .= '<div class="menu-thumb-img"><a class="menu-thumb-link" href="'.$this->_categoryHelper->getCategoryUrl($child).'"><img src="' . $this->_helper->getBaseUrl().'catalog/category/' . $rt_menu_icon_img . '" alt="'.$child->getName().'"/></a></div>';
}
$html .= '<a href="'.$this->_categoryHelper->getCategoryUrl($child).'">';
if ($level > 1 && $rt_menu_icon_img)
$html .= '<img class="menu-thumb-icon" src="' . $this->_helper->getBaseUrl().'catalog/category/' . $rt_menu_icon_img . '" alt="'.$child->getName().'"/>';
elseif($rt_menu_font_icon)
$html .= '<em class="menu-thumb-icon '.rt_menu_font_icon.'"></em>';
$html .= '<span>'.$child->getName();
if($rt_menu_cat_label)
$html .= '<span class="cat-label cat-label-'.$rt_menu_cat_label.'">'.$this->_custommenuConfig['cat_labels'][$rt_menu_cat_label].'</span>';
$html .= '</span></a>';
if(count($sub_children) > 0) {
$html .= $this->getSubmenuItemsHtml($sub_children, $level+1, $max_level, $column_width, $menu_type);
}
$html .= '</li>';
}
}
$html .= '</ul>';
}

return $html;
}

public function getCustomMenuHtml()
{
$html = '';

$categories = $this->getStoreCategories(true,false,true);

$this->_custommenuConfig = $this->_helper->getConfig('custommenu');

$max_level = $this->_custommenuConfig['general']['max_level'];
$html .= $this->getCustomBlockHtml('before');
foreach($categories as $category) {
if (!$category->getIsActive()) {
continue;
}

$cat_model = $this->getCategoryModel($category->getId());

$rt_menu_hide_item = $cat_model->getData('rt_menu_hide_item');

if(!$rt_menu_hide_item) {
$children = $this->getActiveChildCategories($category);
$rt_menu_cat_label = $cat_model->getData('rt_menu_cat_label');
$rt_menu_icon_img = $cat_model->getData('rt_menu_icon_img');
$rt_menu_font_icon = $cat_model->getData('rt_menu_font_icon');
$rt_menu_cat_columns = $cat_model->getData('rt_menu_cat_columns');
$rt_menu_float_type = $cat_model->getData('rt_menu_float_type');

if(!$rt_menu_cat_columns){
$rt_menu_cat_columns = 4;
}

$menu_type = $cat_model->getData('rt_menu_type');
if(!$menu_type)
$menu_type = $this->_custommenuConfig['general']['menu_type'];

$custom_style = '';
if($menu_type=="staticwidth")
$custom_style = ' style="width: 500px;"';

$rt_menu_static_width = $cat_model->getData('rt_menu_static_width');
if($menu_type=="staticwidth" && $rt_menu_static_width)
$custom_style = ' style="width: '.$rt_menu_static_width.';"';

$item_class = 'level0 ';
$item_class .= $menu_type.' ';

$menu_top_content = trim($cat_model->getData('rt_menu_block_top_content'));
$menu_left_content = trim($cat_model->getData('rt_menu_block_left_content'));
$menu_left_width = trim($cat_model->getData('rt_menu_block_left_width'));
if(!$menu_left_content || !$menu_left_width)
$menu_left_width = 0;
$menu_right_content = trim($cat_model->getData('rt_menu_block_right_content'));
$menu_right_width = trim($cat_model->getData('rt_menu_block_right_width'));
if(!$menu_right_content || !$menu_right_width)
$menu_right_width = 0;
$menu_bottom_content = trim($cat_model->getData('rt_menu_block_bottom_content'));
if($rt_menu_float_type)
$rt_menu_float_type = 'fl-'.$rt_menu_float_type.' ';
if(count($children) > 0 || (($menu_type=="fullwidth" || $menu_type=="staticwidth") && ($menu_top_content || $menu_left_content || $menu_right_content || $menu_bottom_content)))
$item_class .= 'parent ';
$html .= '<li class="ui-menu-item '.$item_class.$rt_menu_float_type.'">';
if(count($children) > 0) {
$html .= '<div class="open-children-toggle"></div>';
}
$html .= '<a href="'.$this->_categoryHelper->getCategoryUrl($category).'" class="level-top">';
if ($rt_menu_icon_img)
$html .= '<img class="menu-thumb-icon" src="' . $this->_helper->getBaseUrl().'catalog/category/' . $rt_menu_icon_img . '" alt="'.$category->getName().'"/>';
elseif($rt_menu_font_icon)
$html .= '<em class="menu-thumb-icon '.$rt_menu_font_icon.'"></em>';
$html .= '<span>'.$category->getName().'</span>';
if($rt_menu_cat_label)
$html .= '<span class="cat-label cat-label-'.$rt_menu_cat_label.'">'.$this->_custommenuConfig['cat_labels'][$rt_menu_cat_label].'</span>';
$html .= '</a>';
if(count($children) > 0 || (($menu_type=="fullwidth" || $menu_type=="staticwidth") && ($menu_top_content || $menu_left_content || $menu_right_content || $menu_bottom_content))) {
$html .= '<div class="level0 submenu"'.$custom_style.'>';
if(($menu_type=="fullwidth" || $menu_type=="staticwidth") && $menu_top_content) {
$html .= '<div class="menu-top-block">'.$this->getBlockContent($menu_top_content).'</div>';
}
if(count($children) > 0 || (($menu_type=="fullwidth" || $menu_type=="staticwidth") && ($menu_left_content || $menu_right_content))) {
$html .= '<div class="row">';
if(($menu_type=="fullwidth" || $menu_type=="staticwidth") && $menu_left_content && $menu_left_width > 0) {
$html .= '<div class="menu-left-block col-sm-'.$menu_left_width.'">'.$this->getBlockContent($menu_left_content).'</div>';
}
$html .= $this->getSubmenuItemsHtml($children, 1, $max_level, 12-$menu_left_width-$menu_right_width, $menu_type, $rt_menu_cat_columns);
if(($menu_type=="fullwidth" || $menu_type=="staticwidth") && $menu_right_content && $menu_right_width > 0) {
$html .= '<div class="menu-right-block col-sm-'.$menu_right_width.'">'.$this->getBlockContent($menu_right_content).'</div>';
}
$html .= '</div>';
}
if(($menu_type=="fullwidth" || $menu_type=="staticwidth") && $menu_bottom_content) {
$html .= '<div class="menu-bottom-block">'.$this->getBlockContent($menu_bottom_content).'</div>';
}
$html .= '</div>';
}
$html .= '</li>';
}
}
$html .= $this->getCustomBlockHtml('after');

return $html;
}
}

 

Now you have to make a template file and then you can customize the below code according to your needs.

<?php
/**
* Copyright © 2016 TuanHatay. All rights reserved.
*/

?>
<?php
/**
* Top menu for store
*
* @see \Wishusucess\CustomMenu\Block\Topmenu
*/
?>
<?php
$_helper = $this->helper('Wishusucess\CustomMenu\Helper\Data');
$_config = $_helper->getConfig('custommenu');
$columnsLimit = 0;
if($_config['general']['enable']) {
?>
<?php $_menu = $block->getCustomMenuHtml(); ?>

<nav class="navigation custommenu" role="navigation">
<ul>
<?php echo $_menu; ?>
</ul>
</nav>

<script type="text/javascript">
require([
'jquery',
'wishusucess/custommenu'
], function ($) {
$(".custommenu").CustomMenu();
});
</script>
<?php
} else {
?>
<?php $_menu = $block->getHtml('level-top', 'submenu', $columnsLimit); ?>

<nav class="navigation custommenu" role="navigation">
<ul data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'>
<?php /* @escapeNotVerified */ echo $_menu; ?>
</ul>
</nav>

<?php
}
?>

Recommended Post:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

Elasticsearch: How to Install Elasticsearch in Ubuntu 18.04

 

Also Read:

Show Custom Attributes in Magento 2 on The Frontend Side

Magento 2 Interview Questions: Most Important Question & Answer

Show Custom Attributes in Magento 2 on The Frontend Side

Magento 2 custom attributes some times does not shows the data on the storefront page, like product details page, category page or listing page. So here we are giving you some different ways to show custom attributes data on the product details page.

You can print the Magento 2 custom attribute value on the product details page in the following way.

To get the product value

$product->getData('attribute_code');

or

$_product->getData('attribute_code');

or

$product->getAttributeCode()

or

$_product->getAttributeText('attribute_code');

 

How to Show Custom Attributes Identifer in phtml File

You can directly copy the below code and past in to your desired file location to print the value of custom attribute. Just you need to change the attribute code and then clean the magento caches.

<?php $_product->getAttributeText('attribute_code'); ?>

 

Magento 2 Interview Question: Most Important Question & Answer

 

How to Create Custom Attribute in Magento 2

In order to show custom attributes on the front-end we have to create attribute from the backend.

Just open the Admin=>Stores=>Attribute=>Product and click on the product tab you will see the screen like below image.

Create Attribute in Magento 2

Now you have to open the add new attribute tab and fill the required details.

Default level will look like the title in your catalog product  attributes.

Show Custom Attributes

If you want to show custom attributes value of at the front store, then for this you will have to keep the visible as catalog page on storefront as shown in the screen.

Show Custom Attributes on Detail Page

Now you have to decide in which group you want to put this attribute and accordingly you can create a new group or assign it to an old group.

Create Group of Custom Attributes

 

Create Custom Attribute Group in Magento 2

Here you see in the screen that I have created a custom group called short specification, now this short specification name will look like a group in the catalog product section.

Create Custom Attributes Group

Here you are seeing in the screen that the name of the group which has been created, looks like this.

Custom Attributes Group

Now this short specification group looks like a group in the catalog product section

However, if you save the value, then it will not be visible on the front store because you have not yet called its identifier in your desired page of Magento.

Custom Attribute in Catalog Product

Now we need to add the identifier in the below file location for the product details page but you can call this according to your requirements.

design/frontend/Theme_Vender/Theme_Name/Magento_Catalog/templates/product/view/attribute.phtml

 

<div class="product-config">
<strong class="custom_attributes">

<div><?php echo $_product->getData('brand_movement'); ?></div>
<div><?php echo $_product->getData('style'); ?></div>
<div><?php echo $_product->getData('chrono'); ?></div>
<div><?php echo $_product->getAttributeText('case_materiel'); ?></div>
<div><?php echo $_product->getAttributeText('strap_material'); ?></div>
<div><?php echo $_product->getData('stiching'); ?></div>

</strong>

</div>

Now you can see here that all the values are visible because I have called the identity of the custom attribute in the product detail page.

Show Custom Attributes in Magento 2

 

 

Also Check:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

Magento Store: Best 36 Magento Websites Example in The World

Magento 2 Interview Question: Most Important Question & Answer

Here we have prepared top 10 Magento 2 Interview Question and answer for those who are looking for the job as a Magento Developer.

In the Magento 2 Interview Question interviewer always used to ask about what is Magento.

 

List of Top 10 Magento 2 Interview Question and Answer

Magento is an open-source e-commerce platform written in PHP. It uses multiple other PHP frameworks such as Laminas and Symfony. Magento source code is distributed under Open Software License (OSL) v3. 0. Magento was acquired by Adobe Inc in May 2018 for $1.68 Billion USD.

Magento 2 Interview Question

 

What is object manager in Magento 2?

The ObjectManager is class which handles `automatic` Dependency Injection in Magento 2. When any class is constructed, ObjectManager automatically injects class's dependencies which is provided directly (Constructor Injection) and defined in all di. xml files of modules.

 

Magento 2 Interview Question: What is Magento 2 Proxy Class?

Proxy is another new feature in Magento. It is a set of classes, automatically generated by Magento on compile-time. Proxies are used to lazy load a class they extend. The resulting code contains a Proxy class that extends a class we need this Proxy for.

 

Most Common Magento 2 Interview Question is:

What is Factories Classes in Magento?

Factories are service classes that instantiate non-injectable classes, that is, models that represent a database entity. They create a layer of abstraction between the ObjectManager and business code.

 

What is Factory Method in Magento 2?

The Magento 2 Factory Object will be used to instantiate an object. The Factory class name is the name of Model class and append with the Factory word. It also dependent with ObjectManager Class. This design pattern used to create objects for all the classes instead of using new keyword.

 

What is Service Contract in Magento 2 Interview Question

A service contract is a set of PHP interfaces that are defined for a module. If developers define data and service interfaces according to a set of design patterns, the result is a well-defined, durable API that other modules and third-party extensions can implement through Magento models and resource models.

 

What is Design Pattern in Magento 2?

The Magento 2 Dependency Injection(DI) is a system of object management based on Object Manager or Factory to create Objects with available configuration. DI is a design pattern allowing class A to declare dependencies so that class B can use those dependencies.

 

What is Dependency Injection in Magento 2 Interview Question

Magento 2 uses Dependency Injection to replace functionality provided by the Mage class in Magento 1 Dependency Injection is a design pattern that allows an object A to declare its dependencies to an external object B that supplies those dependencies.

 

What is EAV model in Magento 2?

In Magento, EAV stands for Entity, Attribute and Value.

Entity: The entity represents Magento data items such as products, categories, customers and orders. Each entity (product, category, etc.) will have its own entity record in the database.

Attribute: The attributes represent data items that belong to an entity. For example, the product entity has attributes such as name, price, status and many more.

Value: The value is the simplest to understand as it is simply a value linked to an attribute. For better understanding, let’s consider the product entity. Each product entity will have a series of attributes, one of them is the name attribute. Each product will then have a value for the name attribute (and all other attributes).

 

What are Magento 2 plugins?

A plugin, or interceptor, is a class that modifies the behaviour of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behaviour of original, public methods for any class or interface.

 

What is event and observer in Magento 2?

Events Events are dispatched by modules when certain actions are triggered. In addition to its own events, Magento allows you to create your own events that can be dispatched in your code. When an event is dispatched, it can pass data to any observers configured to watch that event.

Observers: Observers are a certain type of Magento class that can influence general behaviour, performance, or change business logic. Observers are executed whenever the event they are configured to watch is dispatched by the event manager.

 

What is repository in magento2?

Repositories give service requestors the ability to perform create, read, update, and delete (CRUD) operations on entities or a list of entities. A repository is an example of a service contract, and its implementation is part of the domain layer.

 

How to Start eCommerce Business

More About Magento 2 

List of Top Magento 2 Websites