Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4230
  • Last Modified:

Magento block and reference - simple question

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
Michal-Drozd
Asked:
Michal-Drozd
  • 8
  • 7
7 Solutions
 
fiboCommented:
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
 
Michal-DrozdAuthor Commented:
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
 
fiboCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Michal-DrozdAuthor Commented:
thanks for comment,
i am using it everywhere to resolve this problem, i just cant find out why it is not working
0
 
fiboCommented:
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
 
Michal-DrozdAuthor Commented:
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
 
fiboCommented:
<<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
 
Michal-DrozdAuthor Commented:
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
 
Michal-DrozdAuthor Commented:
just note, I also inserted helpText to product_viewed (for some echo)
0
 
fiboCommented:
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
 
NerdsOfTechCommented:
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
 
fiboCommented:
What about placing directly

<?php echo $this->getLayout()->createBlock('reports/product_viewed')
->setBlockId(product_viewed')->toHtml() ?>
in header.phtml?
0
 
Michal-DrozdAuthor Commented:
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
 
Michal-DrozdAuthor Commented:
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
 
Michal-DrozdAuthor Commented:
thanks for effort
0
 
fiboCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now