1

I have a code snippet to display cross-sell products on the product page via catalog/product/view.phtml.

It displays the product's cross-sell items, however it also displays disabled products.

Can anyone advise how I can filter only enabled products with the code below?

<?php if($_crossSellProducts = $_product->getCrossSellProducts()): ?>

    <div class="topproducts_containter">
    <div class="topsellerTitle">TOP <?php echo strtoupper(Mage::getModel('catalog/layer')->getCurrentCategory()->getName()); ?> PRODUCTS</div>

    <?php foreach ($_crossSellProducts as $_item): ?>
        <?php $_item = Mage::getModel('catalog/product')->load($_item->getId()); ?>

        <div class="col-sx-12 col-sm-2 items">
            <div class="img">
                <a class="product-image" href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>">
                    <img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(100, 150); ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />
                </a>
            </div>
            <div class="name">
                <a href="<?php echo $_item->getProductUrl() ?>">
                        <?php echo $this->htmlEscape($_item->getName()) ?>
                </a>
            </div>
            <div class="price">
                       <?php echo $this->getPriceHtml($_item, true) ?>
            </div>
       </div>
    <?php endforeach; ?>
    <div class="clearer"></div>
    </div>
<?php endif; ?>

1 Answer 1

2

You have to add condition that if product enabled then it shows on product details page. Try below code where we have to add if($_item->getStatus() == 1) condition:-

<?php if($_crossSellProducts = $_product->getCrossSellProducts()): ?>

    <div class="topproducts_containter">
    <div class="topsellerTitle">TOP <?php echo strtoupper(Mage::getModel('catalog/layer')->getCurrentCategory()->getName()); ?> PRODUCTS</div>

    <?php foreach ($_crossSellProducts as $_item): ?>
        <?php $_item = Mage::getModel('catalog/product')->load($_item->getId()); ?>

    <?php if($_item->getStatus() == 1):?>
        <div class="col-sx-12 col-sm-2 items">
            <div class="img">
                <a class="product-image" href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>">
                    <img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(100, 150); ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />
                </a>
            </div>
            <div class="name">
                <a href="<?php echo $_item->getProductUrl() ?>">
                        <?php echo $this->htmlEscape($_item->getName()) ?>
                </a>
            </div>
            <div class="price">
                       <?php echo $this->getPriceHtml($_item, true) ?>
            </div>
       </div>
    <?php endif; ?>
    <?php endforeach; ?>
    <div class="clearer"></div>
    </div>
<?php endif; ?>
1
  • That did indeed do the trick. Thanks @raj Commented Jul 25, 2018 at 12:46

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.