We use a custommenu.phtml to show recently view items via a popup block.

<div id="pt_menu_pt_item_menu_4" class="pt_menu">
    <div class="parentMenu">
        <span class="block-title">Recently Viewed</span>

    <div id="popup_pt_item_menu_4" class="popup cmsblock" style="display: none;">
        <div class="block3" id="block2_pt_item_menu_4">
                <?php echo $this->getLayout()->createBlock('reports/product_widget_viewed')->setTemplate('reports/widget/viewed/content/viewed_grid.phtml')->toHtml(); ?>

Everything works great except we do not need the whole <div>to show when these is no recently viewed product.

I tried wrapping the whole div with the following:

<?php if ($_products = $this->getRecentlyViewedProducts()): ?>

<?php endif>

but the whole div disappears whether we have viewed products or not.

How do I achieve this?


Try with the condition

<?php if($products = Mage::getSingleton('Mage_Reports_Block_Product_Viewed')->getItemsCollection()): ?>
<?php endif; ?>

Refernce: https://stackoverflow.com/questions/11413918/get-recently-viewed-products-array-in-magento

  • no it shows the title "recently viewed" as well as the popup div no matter what – Dylan Daicy Siao Apr 26 '16 at 19:15
  • Try var_dump your data in $_products variable. Another option could be to use count($_product) > 0 condition. – Jaimin Sutariya Apr 26 '16 at 20:35

the following code does the trick, just for the records.

<?php $products = Mage::getSingleton('Mage_Reports_Block_Product_Viewed')->getItemsCollection(); ?>
<?php if(count($products)>0): ?>

<?php endif?>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.