0

I have my upsells in a block on my product page as default but I would like to display a message or custom block in the same position as my upsells would go if a product has no upsells products ie "sorry this product has no upsell products".

This is my current upsell.phtml file

    <div class="upsells">

<?php if(count($this->getItemCollection()->getItems())): ?>


    <h2><?php echo $this->__('You might also like') ?></h2>

    <?php // $this->setColumnCount(5); // uncomment this line if you want to have another number of columns. also can be changed in layout ?>

    <?php $this->resetItemsIterator() ?>



    <?php for($_i=0;$_i<$this->getRowCount();$_i++): ?>

    <ul>

        <?php for($_j=0;$_j<$this->getColumnCount();$_j++): ?>

            <?php if($_link=$this->getIterableItem()): ?>
            <li>
                <a href="<?php echo $_link->getProductUrl() ?>" title="<?php echo $this->escapeHtml($_link->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_link, 'small_image')->resize(225) ?>" alt="<?php echo $this->escapeHtml($_link->getName()) ?>" /></a>

                <h3 class="upsell-name"><a href="<?php echo $_link->getProductUrl() ?>" title="<?php echo $this->escapeHtml($_link->getName()) ?>"><?php echo $this->escapeHtml($_link->getName()) ?></a></h3>



              </li>  

            <?php else: ?>

            <div class="empty">Sorry we couldn't find any products simular to this</div>

            <?php endif; ?>

        <?php endfor; ?>

    <?php endfor; ?>

    </ul>

<?php endif ?>

</div><!-- end upsells -->

Thankyou.

1 Answer 1

0

You can edit the Upsell products template for this. app\design\frontend\YOUR_PACKAGE\YOUR_THEME\template\catalog\product\list\upsell.phtml

Note: you may need to copy this file to YOUR_PACKAGE\YOUR_THEME from: app\design\frontend\base\default\template\catalog\product\list\upsell.phtml

The file contents are wrapped in and if statement:

<?php if(count($this->getItemCollection()->getItems())): ?>
....list of upsell products 
<?php endif ?>

Change this to:

<?php if(count($this->getItemCollection()->getItems())): ?>
....list of upsell products 
<?php else : ?>
....your custom code 'sorry this product has no upsell products'
<?php endif ?>

Edited answer

Your code is close but it looks like the 'else' content is in the wrong location (I also moved the closing ). Try this:

<div class="upsells">

<?php if(count($this->getItemCollection()->getItems())): ?>


    <h2><?php echo $this->__('You might also like') ?></h2>

    <?php // $this->setColumnCount(5); // uncomment this line if you want to have another number of columns. also can be changed in layout ?>

    <?php $this->resetItemsIterator() ?>



    <?php for($_i=0;$_i<$this->getRowCount();$_i++): ?>

    <ul>

    <?php for($_j=0;$_j<$this->getColumnCount();$_j++): ?>

    <?php if($_link=$this->getIterableItem()): ?>
        <li>
            <a href="<?php echo $_link->getProductUrl() ?>" title="<?php echo $this->escapeHtml($_link->getName()) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_link, 'small_image')->resize(225) ?>" alt="<?php echo $this->escapeHtml($_link->getName()) ?>" /></a>

            <h3 class="upsell-name"><a href="<?php echo $_link->getProductUrl() ?>" title="<?php echo $this->escapeHtml($_link->getName()) ?>"><?php echo $this->escapeHtml($_link->getName()) ?></a></h3>



        </li>  

    <?php endif; ?>

    <?php endfor; ?>

    </ul>

    <?php endfor; ?>

<?php else: ?>

<div class="empty">Sorry we couldn't find any products simular to this</div>    

<?php endif; ?>

</div><!-- end upsells -->
3
  • I already have that in my upsell.phtml but it doesnt work. I have edited my original question to show the content of my upsell.phtml file.
    – Duggy G
    Commented Feb 19, 2015 at 19:03
  • @DuggyG I've updated the answer with an edited version of your code that should provide what you're wanting to accomplish. Commented Feb 19, 2015 at 20:56
  • Brilliant thankyou for your help Jason.
    – Duggy G
    Commented Feb 20, 2015 at 15:52

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.