Question

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.

Was it helpful?

Solution

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; ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top