We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Magento block and reference - simple question

Michal-Drozd
Michal-Drozd asked
on
Medium Priority
4,570 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

Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
thanks for comment,
i am using it everywhere to resolve this problem, i just cant find out why it is not working
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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.
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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

Author

Commented:
just note, I also inserted helpText to product_viewed (for some echo)
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
NerdsOfTechTechnology Scientist
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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.


Author

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

Author

Commented:
thanks for effort
CERTIFIED EXPERT

Commented:
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.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.