Solved

Magento block and reference - simple question

Posted on 2009-07-05
18
4,183 Views
Last Modified: 2013-12-13
Hi,
I am stuck with following issue. I want show product_viewed block (recently viewed products) at specific position on my front page (templates/page/mylayout.phtml).
Look at code snippets of page.xml, catalog.phtml and mylayout.phtml
Header and footer and other default magento blocks are showing right. But problem is only with showing product_viewed block - no html code load. What I am doing wrong ? Why <?php echo $this->getChildHtml('product_viewed') ?> doesnt return any code ?

Thanks you for any help!
<!-- layout/page.xml code snippet-->
<!-- some code before -->
<block type="page/html_header" name="header" as="header"></block>
<block type="reports/product_viewed" name="product_viewed" as="product_viewed"  template="reports/product_viewed.phtml"/>
<!-- some code after --> 
 
<!-- my global layout template code snippet (templates/page/mylayout.phtml) -->
<div class="container">
<div class="header">
    <?php echo $this->getChildHtml('header') ?>
</div>
      </div>
      <div class="main" >
        <div class="content">
                <!-- and here is problem: -->
             <?php echo $this->getChildHtml('product_viewed') ?>
            <div id="product">
                <div class="main">
<!-- some code after -->
 
<!-- layout/catalog.xml code snippet -->
<reference name="product_viewed">
     <block type="reports/product_viewed" name="product_viewed" template="reports/product_viewed.phtml"/>
</reference>

Open in new window

0
Comment
Question by:Michal-Drozd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
18 Comments
 
LVL 29

Accepted Solution

by:
fibo earned 450 total points
ID: 24780987
First you need to be sure that the block is displayed or not...
Switch to admin / system / configuration
Then select the web site on which you are working (you MUST do that)
then go to "advanced" at borrom left and select that you want hints for blocks and phtml.
Return to your fornt-end display. Is your block displayed (even blank) or not?

If not, then the problem is (90%) in the xml
If yes, and the display is empty... then your phtml hase a problem.
Are you sure that it called from a php where $this-> has the expected value?
0
 
LVL 3

Author Comment

by:Michal-Drozd
ID: 24781047
thanks for comment
I know that problem is in XML becouse I use template/block hints, and there is no hint about product_viewed block on page in browser. I dont understand how it is possible there is header block hint, but no product_viewed hint. Look at attacched pictures.
browser-page.jpg
code-layout.jpg
0
 
LVL 29

Assisted Solution

by:fibo
fibo earned 450 total points
ID: 24781624
Since these blocks and templates are really a nightmare, you might consider pacing echo instructions in strategic places, eg before and after the call to getchild
AND within product-viewed
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 3

Author Comment

by:Michal-Drozd
ID: 24789877
thanks for comment,
i am using it everywhere to resolve this problem, i just cant find out why it is not working
0
 
LVL 29

Assisted Solution

by:fibo
fibo earned 450 total points
ID: 24801335
if echo $this->getChildHtml('product_viewed') displays nothing:
1 - maybe product_viewed is not a (block) chil of the current block
2 - or the block is activated but thinks there is no product to display.
Check in the block if there is an <?php if as the first or 2nd line, that would skip any display. Place an "echo 'in product_viewed';" before that if or after the first <div if there is no php before.

UPDATE THE CACHE

then check. are you displaying the block or not?
0
 
LVL 3

Author Comment

by:Michal-Drozd
ID: 24801963
thanks for comment, i have echo in block, before any if. However there is no hint (red hint) about this block, so it means there is some problem with xml config.

I tryied use getBlockHtml(), where block doesnt need to be child, and it doesnt help. I tryied use almost all variants in xml configuration (block as, reference etc.). I explored magento core code to find where is problem, but unsucessfully, it is too complicated to understand how it works. I need more time for it, but i havent.

After every change (which can help me) I am always deleting cache.
0
 
LVL 29

Assisted Solution

by:fibo
fibo earned 450 total points
ID: 24802620
<<thanks for comment, i have echo in block, before any if. However there is no hint (red hint) about this block, so it means there is some problem with xml config.>>

You do not confirm if the block is displaying your "echo" or not...

As a substitue for the mechanism that displays the block: first create (and activate!) a cms page (not a static block) in which you change the layout to display your block. (I think I remeber that you can address the header block from there. otherwise test initially with main or footer

Once this works, you will be sure of what exactly needs to be placed in the layout.xml.

0
 
LVL 3

Author Comment

by:Michal-Drozd
ID: 24805652
there wasnt showing any echo from block, hint neither

ok. I decided to create completely clean new magento site.
Then I added following line to page.xml
<block type="reports/product_viewed" name="product_viewed" as="product_viewed" template="reports/product_viewed.phtml" />

and added following 2 lines to template/page/html/header.phtml (at end)

<?php echo $this->getBlockHtml('product_viewed') ?>
<?php echo $this->getChildHtml('product_viewed') ?>

and block isn't showing. Why ?

so here are souce code of page.xml and header.phtml:
<!-- ****** page.xml - part, header section ****** -->
            <block type="page/html_header" name="header" as="header">
                <block type="page/template_links" name="top.links" as="topLinks"/>
                <block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
                <block type="core/text_list" name="top.menu" as="topMenu"/>
                <block type="reports/product_viewed" name="product_viewed" as="product_viewed" template="reports/product_viewed.phtml" />
            </block>
 
 
<?php /****** header.phtml ******/ ?>
<div class="header-top-container">
    <div class="header-top">
        <h1 id="logo"><a href="<?php echo $this->getUrl('') ?>"><img src="<?php echo $this->getLogoSrc() ?>" alt="<?php echo $this->getLogoAlt() ?>" /></a></h1>
        <p class="no-display"><a href="#main"><strong><?php echo $this->__('Skip to Main Content') ?> &raquo;</strong></a></p>
        <?php echo $this->getChildHtml('topSearch') ?>
        <div class="quick-access">
           <?php echo $this->getWelcome() ?><br />
            <div class="shop-access">
                <?php echo $this->getChildHtml('topLinks') ?>
            </div>
            <?php echo $this->getChildHtml('store_language') ?>
        </div>
    </div>
</div>
<?php echo $this->getChildHtml('topMenu') ?>
<p>helpTEXThelpTEXThelpTEXThelpTEXThelpTEXThelpTEXT</p>
<?php echo $this->getBlockHtml('product_viewed') ?>
<?php echo $this->getChildHtml('product_viewed') ?>

Open in new window

0
 
LVL 3

Author Comment

by:Michal-Drozd
ID: 24805668
just note, I also inserted helpText to product_viewed (for some echo)
0
 
LVL 29

Assisted Solution

by:fibo
fibo earned 450 total points
ID: 24806392
I remember now, but I have to check my notes: the header block is handled very diffrently from the others. (eg, compare header and footer)
That might be (or not) the source of your problem.
Sorry, this needs some thoughts and I cannot make it now.
0
 
LVL 19

Assisted Solution

by:NerdsOfTech
NerdsOfTech earned 50 total points
ID: 24809269
simple but...try using ; at the end of your code
<!-- ****** page.xml - part, header section ****** -->
            <block type="page/html_header" name="header" as="header">
                <block type="page/template_links" name="top.links" as="topLinks"/>
                <block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
                <block type="core/text_list" name="top.menu" as="topMenu"/>
                <block type="reports/product_viewed" name="product_viewed" as="product_viewed" template="reports/product_viewed.phtml" />
            </block>
 
 
<?php /****** header.phtml ******/ ?>
<div class="header-top-container">
    <div class="header-top">
        <h1 id="logo"><a href="<?php echo $this->getUrl('') ?>"><img src="<?php echo $this->getLogoSrc() ?>" alt="<?php echo $this->getLogoAlt(); ?>" /></a></h1>
        <p class="no-display"><a href="#main"><strong><?php echo $this->__('Skip to Main Content'); ?> &raquo;</strong></a></p>
        <?php echo $this->getChildHtml('topSearch'); ?>
        <div class="quick-access">
           <?php echo $this->getWelcome(); ?><br />
            <div class="shop-access">
                <?php echo $this->getChildHtml('topLinks'); ?>
            </div>
            <?php echo $this->getChildHtml('store_language'); ?>
        </div>
    </div>
</div>
<?php echo $this->getChildHtml('topMenu') ?>
<p>helpTEXThelpTEXThelpTEXThelpTEXThelpTEXThelpTEXT</p>
<?php echo $this->getBlockHtml('product_viewed'); ?>
<?php echo $this->getChildHtml('product_viewed'); ?>

Open in new window

0
 
LVL 29

Assisted Solution

by:fibo
fibo earned 450 total points
ID: 24809470
What about placing directly

<?php echo $this->getLayout()->createBlock('reports/product_viewed')
->setBlockId(product_viewed')->toHtml() ?>
in header.phtml?
0
 
LVL 3

Author Comment

by:Michal-Drozd
ID: 24815793
thanks for comments, but no success :(
semicolon (;) is not necessary when there is only one statement in <?php...?>, however i tried it, unsuccessfully

I tryied use (as fibo suggested)
<?php echo $this->getLayout()->createBlock('reports/product_viewed')
->setBlockId('product_viewed')->toHtml() ?>
in header.html but unfortunately no change (returned empty string)

Note i have echo in product_viewed block, so something should show, but not

even I tryied something totally different
I changed section <customer_account_login> in account.xml layout file to following:
    <customer_account_login>
        <!-- Mage_Customer -->
        <remove name="right"/>
        <remove name="left"/>

        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="customer/form_login" name="customer_form_login" template="customer/form/login.phtml"/>
            <block type="reports/product_viewed" name="product_viewed" as="product_viewed" template="reports/product_viewed.phtml" />
        </reference>
    </customer_account_login>
and then i added folowing lines
<p>helpTEXThelpTEXThelpTEXThelpTEXThelpTEXThelpTEXT</p>
<?php echo $this->getBlockHtml('product_viewed'); ?>
<?php echo $this->getChildHtml('product_viewed'); ?>
to customer/form/login.phtml

but http://magento.clean/customer/account/login/ isnt showing my block (neither red hint) ,nightmare...

!!! BUT !!!
I tried another block.
when I replaced

<block type="reports/product_viewed" name="product_viewed" as="product_viewed" template="reports/product_viewed.phtml" />

with
            <block type="poll/activePoll" name="right.poll">
                <action method="setPollTemplate"><template>poll/active.phtml</template><type>poll</type></action>
                <action method="setPollTemplate"><template>poll/result.phtml</template><type>results</type></action>
            </block>

and then in login.phtml added

<p>helpTEXThelpTEXThelpTEXThelpTEXThelpTEXThelpTEXT</p>
<?php echo $this->getBlockHtml('right.poll'); ?>
<?php echo $this->getChildHtml('right.poll'); ?>

block poll/activePoll started showing!

why ? I am really confused with these magento blocks. Why this is showing and product_viewed not.
I cant understand this.
Can somebody explain this ? Thanks.


0
 
LVL 3

Author Comment

by:Michal-Drozd
ID: 24818024
amazing! I explored magento core source code again and finally I found issue!!!
I cant understand why i didnt see it earlier

when you look at \app\code\core\Mage\Reports\Block\Product\Viewed.php you will see what si in code snippet

and look at function _toHtml - here is problem, following:
 if (!$this->_hasViewedProductsBefore()) {
        return ' ';
 }
so when there are no products viewed before, template reports/product_viewed.phtml is ignored as whole block isn't displayed, neither hint

i doesnt need more explaing :)
I am happy, i can continue working on my project

I am going to delete this question, but anyway thanks fibo for your attention (i give you points for it)

    /**
     * Prepare to html
     * check has viewed products
     *
     * @return string
     */
   protected function _toHtml()
    {    	
        if (!$this->_hasViewedProductsBefore()) {
            return '';
        }
 
        $collection = $this->_getRecentProductsCollection();
        //echo 'POKUS';print_r($collection);echo 'POKUS'; 
        //die('toHtml()');
        $hasProducts = (bool)count($collection);
        if (is_null($this->_hasViewedProductsBefore())) {
            Mage::getSingleton('reports/session')->setData('viewed_products', $hasProducts);
        }
        if ($hasProducts) {
            $this->setRecentlyViewedProducts($collection);
        }
 
        return parent::_toHtml();
    }

Open in new window

0
 
LVL 3

Author Closing Comment

by:Michal-Drozd
ID: 31599931
thanks for effort
0
 
LVL 29

Expert Comment

by:fibo
ID: 24822425
Hi Michal, glad that it worked at last after so many wrong tracks.
Thx for the points and grade.
DO NOT DELETE the question, as it adds to the general knowledge base.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question