0

I am trying to display a message in template when there are no viewed_products. So I have tried to overwrite class Mage_Reports_Block_Product_Viewed and its method like this.

Module etc/config.xml file

<config>
    <global>
        <blocks>
            <reports>
                <rewrite>
                    <product_viewed>Myclass_Overwrites_Block_Reports_Product_Viewed</product_viewed>
                </rewrite>
            </reports>
        </blocks>
    </global>
</config>

Block in app/code/local/Myclass/Overwrites/Block/Product/Viewed.php

class Myclass_Overwrites_Block_Reports_Product_Viewed extends Mage_Reports_Block_Product_Viewed
  {
    ...

      protected function _toHtml(){

             if ($this->getCount()) {
               $this->setRecentlyViewedProducts($this->getItemsCollection());
             }
         return parent::_toHtml();
       }
...
  }

Now it overwrites it, if i click on a product and refresh the page i can clearly see with debuging ON that its getting overwritten and that product is shown. But if i don't click on a product (if visited product/s arent stored) it doesnt show anything (i guess it runs the class from Core, i also tried to change it just to see if it would work in core files and it does). I am doing this because i want to show else statement from template

Template design/frontend/Project/default/template/reports/product_viewed.phtml

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

 //Code if there are Viewed Products

 <?php else: ?> // This doesn't show
   <div class="block block-list block-viewed row">
    <div class="col-md-12">
    <div class="panel panel-primary">
    <div class="block-title panel-heading">
    <h3 class ="panel-title"><span><?php echo $this->__('Recently Viewed Products') ?></span></h3>
    </div>
    <div class="block-content panel-body">
      <p>You haven't viewed any Products</p>
     </div>
     </div>
 </div>

Am i doing something wrong or could i do this some other way?

2 Answers 2

1

Please follow bellow steps:

Module etc/config.xml file

<config>
    <global>
        <blocks>
            <reports>
                <rewrite>
                    <product_viewed>Myclass_Overwrites_Block_Reports_Product_Viewed</product_viewed>
                </rewrite>
            </reports>
        </blocks>
    </global>
</config>

created block in app/code/local/Myclass/Overwrites/Product/Viewed.php > and a class has to be defined

class Myclass_Overwrites_Block_Reports_Product_Viewed extends Mage_Reports_Block_Product_Viewed
{
// some code
}
5
  • I have it like that, thanks for the help.Perhaps i should have wrote everything i will edit it. It works when user clicked on a product and then i can see it beeing overwritten, but my problem is when user didnt click on any product it doesnt show nothing only when i put it in Core
    – MePo
    Oct 29, 2015 at 8:21
  • can you please send your code or url?
    – Abdul
    Oct 29, 2015 at 9:07
  • I have edited in my post what i have changed or added in each file. Sadly i only have it local. And also if i was unclear about debug i turned Template Path Hints and Add Block Names to Hints. And as i said with them if i click on a random product i see that product in product_viewed and i see my overwritten class. but if i delete history and reload page i cant see the else statement from template (but if i do this directly in core it works). Somehow if it is empty it wont overwrite
    – MePo
    Oct 29, 2015 at 9:24
  • Okay let me try and give reply you :)
    – Abdul
    Oct 29, 2015 at 9:40
  • Thank you :), The main problem is when there are no viewed_products i would like to display in template a message, maybe i could do that some other way?
    – MePo
    Oct 29, 2015 at 11:15
0

I havent figured out how to display Recently viewed products if the user didnt click on any product. So i have left it by defualt to not show.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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