0

Display category list and end child category only show product list

  • My custom theme

Magento_Catalog/category/products.phtml

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Category view template
 *
 * @var $block \Magento\Catalog\Block\Category\View
 */
?>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
$subcats = $category->getChildrenCategories();
//var_dump($category->getData());
//var_dump($category->getIsAnchor());
//var_dump($subcats->count());
if ($subcats && ($category->getIsAnchor() != '1' || $category->getIsAnchor() != 1)) { ?>
    <ul class="sub-category-list">
        <?php
        foreach ($subcats as $subcat) {
            if ($subcat->getIsActive()) {
                $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId());
                $subcaturl = $subcat->getUrl();
                $_imgHtml = '';
                if ($_imgUrl = $_category->getImageUrl()) {
                    $_imgHtml = '<img src="' . $_imgUrl . '" />';
                } else {
                    $mediaUrl = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
                    $_imgHtml = '<img src="' . $mediaUrl. 'catalog/category/default.jpg" />';
                }
                ?>
                <li class="col-md-3 col-sm-6 text-center sub-category">
                    <a href="<?php echo $subcaturl; ?>" class="block-promo" title="<?php echo $subcat->getName(); ?>">
                        <?php echo $_imgHtml; ?>
                        <span style="background-color: rgba(255,255,255,0.9)" class="content bg-white">
                        <strong><?php echo $subcat->getName(); ?></strong>
                    </span>
                    </a>
                </li>
            <?php }
        } ?>
    </ul>
<?php } else { ?>
<?php if (!$block->isContentMode() || $block->isMixedMode()): ?>
    <?php echo $block->getProductListHtml() ?>
<?php endif; ?>
<?php } ?>

Not display category list only show product list

Remove else part show category list only not product list

what mistake i am done i don't know

0

In magento,product listing is shown when both for Non-anchor and Anchor categories.

And as per ,you code you have remove product listing when it is not anchor category that means your conditions is wrong.It should like:

<?php if($subcats && ($category->getIsAnchor() != '1' || $category->getIsAnchor() != 1)) { ?>
<ul class="sub-category-list">
        <?php
        foreach ($subcats as $subcat) {
            if ($subcat->getIsActive()) {
                $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId());
                $subcaturl = $subcat->getUrl();
                $_imgHtml = '';
                if ($_imgUrl = $_category->getImageUrl()) {
                    $_imgHtml = '<img src="' . $_imgUrl . '" />';
                } else {
                    $mediaUrl = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
                    $_imgHtml = '<img src="' . $mediaUrl. 'catalog/category/default.jpg" />';
                }
                ?>
                <li class="col-md-3 col-sm-6 text-center sub-category">
                    <a href="<?php echo $subcaturl; ?>" class="block-promo" title="<?php echo $subcat->getName(); ?>">
                        <?php echo $_imgHtml; ?>
                        <span style="background-color: rgba(255,255,255,0.9)" class="content bg-white">
                        <strong><?php echo $subcat->getName(); ?></strong>
                    </span>
                    </a>
                </li>
            <?php }
        } ?>
    </ul>
<?php endif ?>

<?php if (!$block->isContentMode() || $block->isMixedMode()): ?>
    <?php echo $block->getProductListHtml() ?>
<?php endif; ?>
|improve this answer|||||
  • Hai Amit Bera, May i know correct way of condition – kamal Aug 30 '17 at 8:44
  • Already add the condition on answer – Amit Bera Aug 30 '17 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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