0

How to show product category on product page in magento2.2.6? i selected 2columns-left.xml layout template in category. http://magento-213836-648260.cloudwaysapps.com/shop.html i want to add sidebar on this page please check above content http://prntscr.com/lczbxh. Please explain step by step.

5
  • First url is not working. Commented Nov 1, 2018 at 7:52
  • ohh sorry, please check this magento-213836-648260.cloudwaysapps.com/shop.html
    – neha rani
    Commented Nov 1, 2018 at 8:04
  • Ok its working. Now you want to show only those categories which are associated with this products in the left sidebar. Right? Commented Nov 1, 2018 at 8:09
  • yes i want to show categories in left side bar.
    – neha rani
    Commented Nov 1, 2018 at 8:11
  • Check my answer. Its working fine. Commented Nov 1, 2018 at 8:59

1 Answer 1

0

Create a module with name STech_Procategories by following steps:

Step 1: Create registration.php under

app/code/STech/Procategories/registration.php

with below content:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'STech_Procategories',
    __DIR__
);

Step 2: Create module.xml under

app/code/STech/Procategories/etc/module.xml

with below content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="STech_Procategories" setup_version="0.0.1">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Step 3: Create catalog_product_view.xml under

app/code/STech/Procategories/view/frontend/layout/catalog_product_view.xml

with below content:

<?xml version="1.0"?>
<page layout="2columns-left" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="sidebar.additional">
            <block class="Magento\Catalog\Block\Product\View" name="pro.categories" template="STech_Procategories::sidebar/categories.phtml"/>
        </referenceContainer>
    </body>
</page>

Step 4: Create categories.phtml under

app/code/STech/Procategories/view/frontend/templates/sidebar/categories.phtml

with below content:

<?php
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();

$_product = $block->getProduct();
$categoryIds = $_product->getCategoryIds();

$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$_categories = $categoryCollection->create()->addAttributeToSelect('*')->addAttributeToFilter('entity_id', $categoryIds);
?>

<?php if(count($_categories) > 0): ?>
    <div class="block filter">
        <div class="content">
            <dl class="options">
                <dt><?=  __('Product Categories') ?></dt>
                <dd>
                    <ol class="items">
                        <?php foreach ($_categories as $_category): ?>
                            <?php if ($_category->getIsActive()): ?>
                                <li class="item">
                                    <a href="<?= $_category->getUrl() ?>"><?= $_category->getName() ?></a>
                                </li>
                            <?php endif; ?>
                        <?php endforeach ?>
                    </ol>
                </dd>
            </dl>
        </div>
    </div>
<?php endif; ?>

Run setup upgrade, di compile and static content deploy.

7
  • After complete this steps then?
    – neha rani
    Commented Nov 1, 2018 at 9:39
  • Thats it now test the product page. Don't forget to run setup upgrade, di compile, and static content deploy. That's it. Commented Nov 1, 2018 at 9:42
  • when i put this all steps then my magento admin stop working.
    – neha rani
    Commented Nov 1, 2018 at 9:45
  • i want to call default sidebar of magento 2.2
    – neha rani
    Commented Nov 1, 2018 at 9:46
  • These code never affect your admin. Commented Nov 1, 2018 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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