1

I have the code (details below) that basically generates links and images for a categories sub-categories. It is working in Magento 1.7 - we are moving to Magento 2 and I would like to get it working in Magento 2.0. Can anyone please suggest an update to get it working?

PHTML FILE LOCATION

The phtml file is located in (Magento 1.9) /template/catalog/navigation/categoryblocks.phtml

PHTML FILE CONTENTS

<?php 
        //If there are sub categories
        $categories = $this->getCurrentChildCategories();
        $categoriescount = $this->getCurrentChildCategories()->count();
        if ($categoriescount > 0):  ?>
        <div class="container_12">  
        <?php 
            //Loop through categories
            foreach ($categories as $category): ?>
            <div class="grid_3 alpha special-spacing" style="margin-bottom:10px;">
                <div>
                    <!--<p class="cat-link"><?php echo $category->getName()?></p>-->
                    <a href="<?php echo $this->getCategoryUrl($category) ?>" class="cat-link"><?php echo $category->getName()?></a>
                </div>
                <?php //get category products
                $categoryProducts = $category->getProductCollection();
                   if(count($categoryProducts) <> 1):?>
                <a href="<?php echo $this->getCategoryUrl($category)?>" class="cat-image">
                <?php else:?>
                <?php $product = $categoryProducts->getFirstItem();?>
                <a href="<?php echo $product->getProductUrl()?>" class="cat-image">
                <?php endif;?>
                <?php 
                // If there is a thumbnail set for the category - Display it
                if($imgUrl = Mage::getModel('catalog/category')->load($category->getId())->getImage()):?>
                <img src="<?php echo $this->getBaseUrl()."media/catalog/category/".$imgUrl ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
                <?php endif; ?>
                </a>
            </div>

            <?php endforeach; ?>
        <div class="clear"></div>
        </div>
        <?php else:?>
        <p>No Sub Categories</p>

CURRENT CALL IN STATIC BLOCK

{{block type="catalog/navigation" template="catalog/navigation/categoryblocks.phtml"}}
6
  • in magento 2, your phtml file with image code is not working for magento 2 you have used in mage::getmodel is not supported in magento2. also block type is not support in magento 2 Aug 1, 2016 at 8:40
  • Thanks Rakesh - any suggested update for 2.0? Aug 1, 2016 at 10:12
  • have you created theme for magento 2? Aug 1, 2016 at 10:14
  • Yes we have theme set up. But are trying to add this functionality - which does not work currently. Aug 1, 2016 at 10:19
  • @YorkieMagento Did you get any solution to your issue. Please post solution on stack. I am also facing same issue
    – Pravin
    Aug 19, 2018 at 3:02

3 Answers 3

2

you can keep below code,

Keep inside Static block below code,

{{block class="Magento\Catalog\Block\Navigation" templates="catalog/navigation/categoryblocks.phtml"}}

Below file keep inside your app/design/frontend/Vendorname/themename/Magento_Catalog/templates/catalog/navigation/categoryblocks.phtml

    <?php 
            //If there are sub categories
            $categories = $block->getCurrentChildCategories();
            $categoriescount = $block->getCurrentChildCategories()->count();
            if ($categoriescount > 0):  ?>
            <div class="container_12">  
            <?php 
                //Loop through categories
                foreach ($categories as $category): ?>
                <div class="grid_3 alpha special-spacing" style="margin-bottom:10px;">
                    <div>
                        <!--<p class="cat-link"><?php echo $category->getName()?></p>-->
                        <a href="<?php echo $block->getCategoryUrl($category) ?>" class="cat-link"><?php echo $category->getName()?></a>
                    </div>
                    <?php //get category products
                    $categoryProducts = $category->getProductCollection();
                       if(count($categoryProducts) <> 1):?>
                    <a href="<?php echo $block->getCategoryUrl($category)?>" class="cat-image">
                    <?php else:?>
                    <?php $product = $categoryProducts->getFirstItem();?>
                    <a href="<?php echo $product->getProductUrl()?>" class="cat-image">
                    <?php endif;?>
                    </a>
                </div>

                <?php endforeach; ?>
            <div class="clear"></div>
            </div>
            <?php else:?>
    <h3>product not 

found...</h3>
<?php endif;?>
0
3

You need to use the following format to display templates inside CMS blocks.

{{block class="Block/Class/Here" template="PackageName_ ModuleName::path/here/template-name.phtml"}}
-2

You need to define the type the way it works in magento 2

{{block class="Magento\Catalog\Block\Navigation" templates="catalog/navigation/categoryblocks.phtml"}}
1
  • This is the code for Magento 1 and the question is related to Magento 2.
    – John
    May 15, 2017 at 9:58

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.