Magento 1.9: Add custom block to header - magento

I have this in my local.xml.
<reference name="header">
<block type="core/template" name="wc-customheadblock" template="wc-customheadblock.phtml" />
</reference>
Inside wc-customheadblock.phtml i have:
<div style="height:100px; background-color:green">
<h1>This is the wc custom head block</h1>
</div>
But this block is not appearing in the header. I've tried the same for the sidebars (by changing the xml reference name) and it works just fine. Why does this not work for the header?
This site claims its possible to make add blocks to the header using local.xml!
http://www.classyllama.com/development/magento-development/the-better-way-to-modify-magento-layout

Go to your header.phtml file (app/design/frontend/package/theme/page/html/header.phtml) and place one of the following lines:
<?php echo $this->getChildHtml(); ?>
or
<?php echo $this->getChildHtml('wc-customheadblock'); ?>
Most likely header doesn't render child blocks by default because it would cause some template crashes. On the sidebars, extra childs can be easily appended at the end.

Related

Magento - have custom 'Footer Links' static block on all Checkout pages

I have added some custom header/footer phtml files to my Magento installation, the footer uses an static block called 'Footer Links' - this is a HTML block added via the Magento 'CMS' section.
It is possible to have some logic which 'hides' or disables this block on any checkout page?
One way to do it would be via the local.xml in /app/design/frontend/yourpackagename/yourthemename/layout/local.xml. The handles that you need to add depend on how your checkout process is configured. Here is some example code to get you started:
<checkout_cart_index>
<remove name="footer">
</checkout_cart_index>
<checkout_onepage_index>
<remove name="footer">
</checkout_onepage_index>
This is removing the entire footer block, but you can use it to remove any block name that you have. Alan Storms layoutviewer module is great for figuring out what the layout handles are: http://alanstorm.com/layouts_blocks_and_templates.
Hi i have a custom code to disable the footer links block in checkout page add this code in footer.phtml file
<?php $page_route=Mage::app()->getRequest()->getRouteName(); ?>
<?php if($page_route !='checkout'){ ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('footer_links')->toHtml() ?>
<?php } ?>
And comment or remove this code in cms.xml file
<block type="cms/block" name="cms_footer_links" before="footer_links">
<action method="setBlockId"><block_id>footer_links</block_id></action>
</block>

Change location to insert HTML Youama Ajax Login & Registration - Magento

I'm using the extension "Youama Ajax Login and Register", and it inserts an html page that serves as a modal window (lightbox popup or do not know how to call it).
however, it inserts the html on:
<div class="wrapper">
<div class="page">
<div class="main-container">
<div class="main">
<div class="col-main">
<div class="col-home">
/* [HERE] */
I wish he was inserted after the body.
any idea how to do this?
You would need to identify the XML that Youama is using to place that block at that point in you code and then move it to be rendered as a child of the block after_body_start.
So for example in your local.xml file you could do something like...
<default>
<!-- Youama Ajax Login and Register -->
<reference name="after_body_start">
<block type="youama/ajaxlogin" name="youama_login" as="youama_login" template="youama/ajaxlogin.phtml" />
</reference>
</default>
Please note I made all the attribute values up - you will need to copy them from the Youama XML for the block.
You will also need to remove the Youama block from it's current assignment or you will have it on your page twice. Which might be something like
<reference name="content">
<action method="unsetChild"><name>youama_login</name></action>
</reference>
Plan B : write some JavaScript to rip the DOM node out from its current posisiton and re-insert it after <body> but that is a bit of a hack and not really encouraged.
Either way you might have to check the CSS selectors in case moving the HTML changes the CSS paths to the nodes that are being styled or selected via JavaScript.

How to add topLinks to a cms page in magento

The answer is probably out there as a combination of several posts but I am not very good at Magento yet so I have to ask anyway:
I would like to have the topLinks inserted into a cms page.
I tried <?php echo $this->getChildHtml('topLinks') ?> but that does not work, it just shows the code as text on the page.
I tried {{block type="core/template" name="top.Links" as="topLinks" template="page/template/links.phtml"}} but nothing shows up.
I did successfully add the search form to the cms page with {{block type="core/template" name="top.search" as="topSearch" template="catalogsearch/form.mini.phtml"}} so I figured I probably just have the block type wrong or something.
What did I do wrong?
TL;DR: Well...You can't.
Why?:The topLinks block is a "container" block of type page/template_links. This is just added in the layout, but other layout handles or blocks add links to it. For example this part of xml in the customer.xml layout file
<reference name="top.links">
<action method="addLink" translate="label title" module="customer"><label>My Account</label><url helper="customer/getAccountUrl"/><title>My Account</title><prepare/><urlParams/><position>10</position></action>
</reference>
adds the My account link to it. There are others.
In conclusion the topLinks block does not have meaning on it's own. It is just a placeholder that can e modified by other blocks.
When a cms page is rendered the layout is already loaded parsed, so the block you add cannot be modified anymore by other blocks or layout files.
You can hardcode put top links in your cms pages like this..
<ul id="nav">
<li class="level0 parent"><span>My Account</span></li>
<li class="level0 parent"><span>My Wishlist</span></li>
<li class="level0 parent"><span>My Cart</span></li>
<li class="level0 parent"><span>Checkout</span></li>
<li class="level0 parent"><span>Log In</span></li>
</ul>
#Marius Thanks, I learned something new there. I am still struggling with understanding the intricate details of Magento's structure but I'm working on it.
#chirag I tried that but php does not work directly in cms pages so it tries to link to http://mymagentopage/<?php echo $this->getUrl('customer/account')?>. I can of course link directly to http://mymagentopage/customer/account but for a few links I would miss functionality:
"Login" changing to "Logout" when logged in and logging the customer out instead of going to the account screen.
"Cart" changing to "Cart(2)" when product is added to the cart.
etc (I don't use wishlist)
Is there a way to regain this functionality?
I found this snippet that does it but it's php which won't work in cms pages:
<?php if (Mage::getSingleton('customer/session')->isLoggedIn()==0): ?>
<?php echo $this->__('Log In') ?>
<?php else: ?>
<?php echo $this->__('Log Out') ?>
<?php endif ?>I would also be happy with a solution to enable me to use php in cms pages, I am the only admin anyway.
EDIT
I found a working solution:
I created a new phtml file containing the above mentioned snippet. I created a new folder 'customphp' in my template folder and saved it there as test.phtml.
In the cms page I added a block: {{block type="core/template" name="whatever_unique-name-i-want" template="customphp/test.phtml"}}
Tada!
This is where I got the idea: http://www.magentocommerce.com/boards/viewthread/439880/

Moving Magento "Add to Cart" button to new file not working

I want to move Add to cart button from view.phtml file to 2columns-right.phtml file and I am cant make it work. What I did is that I copied <?php echo $this->getChildHtml('addtocart') ?> from view.phtml file to 2columns-right.phtml and it does not appear at all. I did Flush Magento cache too but nothing again.
Any suggestions on how to make this work?
You should get familiar with magento layout system. To make possible <?php echo $this->getChildHtml('addtocart') ?> work in 2columns-right.phtml this block should be declared as child block of root block (the root is the block that is rendered with 2columns-right.phtml). Actually, I don't see much sense in moving add to cart to other template, because addtocart.phtml itself is just a button that submits whole form that is located at catalog/product/view.phtml. If you take it out of there it won't work.
First of all i agree with nevermourn you can not get childhtml if you havn't declared it. But you can use
<?php echo $this->getLayout()->createBlock('catalog/product_view')->setTemplate('catalog/product/view/addtocart.phtml')->toHtml(); ?>
By using this in 2columns-right.phtml you will get addtocart.phtml for sure.
in order to call add to cart button by using
<?php echo $this->getChildHtml('addtocart') ?>
on the page you desire in layout/local.xml
<yourModule_YourController_yourAction>
<reference name="content">
<block type="catalog/product_view" name="product.info.addtocart" as="addtocart" template="catalog/product/view/addtocart.phtml"/>
</reference>
</yourModule_YourController_yourAction>

Magento blocks - still not getting it

I have a custom template block (phtml) that is defined like this:
<block type="catalog/product" name="a.unique.name" template="dir/dir/customlist.phtml"/>
that is defined in CMS home page within this block
<reference name="left">
However if I move it to header block, it does not appear
<reference name="header">
What am I missing here?
** ---- addition -----
I tried this as suggested by D. Sloof, but it does not work. (I suspect more due to my mis-actions than his explanation.)
I added getChildHtml('customlist'); ?> to
mytheme\template\page\html\header.phtml
just under div "top-col-1"
<div class="wrapper">
<div class="top-col-1">
<?php echo $this->getChildHtml('customlist'); ?>
where "customlist" can befound in
mytheme/template/dir/dir/customlist.phtml
What am I doing wrong?
got it...
block name a.unique.name in xml file
<block type="catalog/product" name="a.unique.name" template="dir/dir/customlist.phtml"/>
must match what is called in mytheme\template\page\html\header.phtml
<?php echo $this->getChildHtml('a.unique.name'); ?>
The header block is of type Mage_Page_Block_Html_Header and will not automatically render child blocks. You can have a look at the template file page/html/header.phtml how the child block html is retrieved.
You essentially have 2 options:
Add your own getChildHtml call in the header template (by copying it to your own theme), or;
Put your block inside a container that will automatically render child blocks. The left block is of type Mage_Core_Block_Text_List which will do this. There is a similar block inside the header block called top.container that is of type Mage_Page_Block_Html_Header that will do this as well: you can reference this block instead.

Resources