Magento 1.9.3 custom code.

I have a foreach loop for related products to show in a custom piece of code for the product view page. here is the code.

<?php foreach ($allRelatedProductIds as $id): ?>
<?php $relatedProduct = $productModel->load($id); ?>
<div class="uk-text-center uk-margin-remove">
    <a href="<?php echo $relatedProduct->getUrlPath(); ?>">
        <img class="b-lazy" src="<?php echo $relatedProduct->getImageUrl(); ?>"
            alt="<?php echo $relatedProduct->getDescription(); ?>-<?php echo $relatedProduct->getSku(); ?>"
            title="<?php echo $relatedProduct->getDescription(); ?>-<?php echo $relatedProduct->getSku(); ?>"
            width="60px" />
        <div style="font-size: 10px;" class="uk-margin-remove">

However I am finding that even if a product is set to disabled or out of stock or even not visible individually it will still show in the related products loop.

How can I edit this piece of code to not show disabled items in the loop?

Thanks in advance.

1 Answer 1


I would suggest to check if the product is salable in your loop if you can't limit the collection to the salable products.

Something like this should work:

<?php foreach ($allRelatedProductIds as $id): ?> 
    <?php $relatedProduct = $productModel->load($id); ?> 
    <?php if ($relatedProduct->isSalable()) :?>

    your code

    <?php endif; ?>
  • 100% that is what i was after. Thank you. May I ask how you came across this? is there a cheat sheet of code that can be used in magento to help me program without hacking from other code to work it out? Commented May 29, 2020 at 4:12

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.