1

I hope someone can help me. I have a website in Magento 2, and I want to hide the related products when they do not have articles.

I have added my list of products and some have related products and others do not, but the block of related products is always displayed.

How can I hide it when it's empty?

Thank you

Hello, this is the code that I have in the path /public_html/app/design/frontend/Sm/himarket/Magento_Catalog/templates/product/list/related.phtml

<?php
$_config = $this->helper('Sm\Himarket\Helper\Data');
$limit_product = $_config->getProductDetail('related_limit');

switch ($type = $block->getType()) {

    case 'related-rule':
        if ($exist = $block->hasItems()) {
            $type = 'related';
            $class = $type;

            $image = 'related_products_list';
            $title = __('Related Products');
            $items = $block->getAllItems();
            $limit = $block->getPositionLimit();
            $shuffle = (int) $block->isShuffled();

            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;

        }
    break;

    case 'related':
        /** @var \Magento\Catalog\Block\Product\ProductList\Related $block */
        if ($exist = $block->getItems()->getSize()) {
            $type = 'related';
            $class = $type;

            $image = 'related_products_list';
            $title = __('Related Products');
            $items = $block->getItems();
            $limit = 0;
            $shuffle = 0;

            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;

        }
    break;

    case 'other':
    break;
}
?>

<?php if ($exist):?>

        <div class="related-block products-grid">
            <ol class="products list items product-items">
                <?php $i=0; foreach ($items as $_item): $i++;?>
                <li class="item product product-item">
                    <div class="product-item-info">
                        <div class="item-inner">
                            <div class="box-image">
                                <a href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
                                    <?php echo $block->getImage($_item, $image)->toHtml(); ?>
                                </a>

                                <!--LABEL PRODUCT-->
                                <div class="inner-label">
                                <?php
                                    $specialprice = $_item->getSpecialPrice();
                                    $specialPriceFromDate = $_item->getSpecialFromDate();
                                    $specialPriceToDate = $_item->getSpecialToDate();

                                    $today = time();

                                    if ($specialprice) {
                                        if ($today >= strtotime($specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime($specialPriceFromDate) && is_null($specialPriceToDate)) { ?>
                                            <div class="label-product label-sale">
                                                <span class="sale-product-icon">
                                                    <?php echo __('Sale'); ?>
                                                </span>
                                            </div>
                                        <?php }
                                    }
                                ?>

                                <?php
                                $now = date("Y-m-d");
                                $newsFrom = substr($_item->getNewsFromDate(), 0, 10);
                                $newsTo = substr($_item->getNewsToDate(), 0, 10);

                                if ($newsTo != '' || $newsFrom != '') {
                                    if (($newsTo != '' && $newsFrom != '' && $now >= $newsFrom && $now <= $newsTo) || ($newsTo == '' && $now >= $newsFrom) || ($newsFrom == '' && $now <= $newsTo)) {?>
                                            <div class="label-product label-new">
                                                <span class="new-product-icon"><?php echo __('New'); ?></span>
                                            </div>
                                    <?php }
                                } ?>
                                </div>

                                <!--END LABEL PRODUCT-->

                                <div class="bottom-action-out-wrap">
                                    <div class="bottom-action-out">
                                        <div class="bottom-action">
                                            <a style="display: none;" href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>">&nbsp;</a>


                                            <?php if($_config->getAdvanced('show_wishlist_button')){?>
                                                <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow()): ?>
                                                    <a href="#" data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_item); ?>'  class="action towishlist btn-action link-wishlist" data-action="add-to-wishlist" title="<?php /* @escapeNotVerified */ echo __('Add to Wish List') ?>">
                                                        <span><?php /* @escapeNotVerified */ echo __('Add to Wish List') ?></span>
                                                    </a>
                                                <?php endif; ?>
                                            <?php } ?>

                                            <?php if($_config->getAdvanced('show_compare_button')){?>
                                                <?php if ($block->getAddToCompareUrl()): ?>
                                                    <?php  $compareHelper = $this->helper('Magento\Catalog\Helper\Product\Compare'); ?>
                                                    <a href="#" class="action tocompare btn-action link-compare" data-post='<?php /* @escapeNotVerified */ echo $compareHelper->getPostDataParams($_item);?>' data-role="add-to-links" title="<?php /* @escapeNotVerified */ echo __('Add to Compare'); ?>">
                                                        <span><?php /* @escapeNotVerified */ echo __('Add to Compare') ?></span>
                                                    </a>
                                                <?php endif; ?>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="product details product-item-details box-info">
                                <h2 class="product name product-item-name product-name">
                                    <a class="product-item-link" title="<?php echo $block->escapeHtml($_item->getName()) ?>" href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>">
                                        <?php echo $block->escapeHtml($_item->getName()) ?>
                                    </a>
                                </h2>

                                <?php echo $block->getReviewsSummaryHtml($_item, $templateType) ?>

                                <?php /* @escapeNotVerified */ echo $block->getProductPrice($_item); ?>
                                <?php if($_config->getAdvanced('show_addtocart_button')){?>
                                                <?php if ($_item->isSaleable()): ?>
                                                    <?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
                                                        <button class="action tocart btn-action btn-cart" data-mage-init='{"redirectUrl": {"url": "<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_item) ?>"}}' type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
                                                            <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                                        </button>
                                                     <?php else: ?>
                                                        <?php $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
                                                        $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
                                                        ?>
                                                        <button class="action tocart btn-action btn-cart" data-post='<?php /* @escapeNotVerified */ echo $postData; ?>' type="button" title="<?php /* @escapeNotVerified */ echo __('Add to Cart') ?>">
                                                            <span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
                                                        </button>
                                                    <?php endif; ?>
                                                <?php else: ?>
                                                    <?php if ($_item->getIsSalable()): ?>
                                                        <div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
                                                    <?php else: ?>
                                                        <div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
                                                    <?php endif; ?>
                                                <?php endif; ?>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                </li>
                <?php if($i == $limit_product) break;?>
                <?php endforeach ?>
            </ol>
        </div>


<script>
    require(['jquery','owlcarousel'], function ($, owlCarousel) {
        var post_slider = $(".related-block .product-items");
        post_slider.owlCarousel({               
        responsive:{
            0:{
                items:1
            },
            480:{
                items:2
            },
            768:{
                items:3
            },
            992:{
                items:4
            },
            1200:{
                items:5
            }
        },
        autoplay:true,
        loop:true,
        nav : true,
        dots: false,
        autoplaySpeed : 950,
        navSpeed : 500,
        dotsSpeed : 500,
        autoplayHoverPause: true,
        margin:5,
        });   
    }); 
    </script>
<?php endif;?>

Your Answer

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

Browse other questions tagged or ask your own question.