I am trying to display a simple message on my Magento site using a special block that I have created. I have been able to easily unset blocks and insert them in other places on the home page, but I am running into trouble when I try to do the same thing on one of the product pages. I have created a file at app/design/frontend/base/default/layout/packagename/modulename.xml
with the following contents:
<?xml version="1.0"?>
<layout>
<default>
<reference name="product.info">
<block type="core/text" name="free_shipping">
<action method="setText"><text><![CDATA[<div>Free Shipping!</div>]]> </text></action>
</block>
</reference>
<reference name="header">
<action method="unsetChild">
<name>top.search</name>
</action>
</reference>
</default>
</layout>
It seems to me like the code above would remove the search bar from a product page and add a block in the product info section that says "Free shipping!" but when I load the page there are no changes. I have tried using "remove" to alter some of the blocks on the page and it works, so the file is definitely being loaded into the layout.xml. I have also tried making my changes in the local.xml file instead, with the same results. Other than that, I am kind of at a loss for things to try to get this to work correctly.
Edit: To provide some more information on the problem, if I were to replace my changes with something like
<reference name="root">
<action method="unsetChild">
<name>header</name>
</action>
</reference>
The header is sucessfully removed. So I guess the question now is, why does calling unset child work as expected when used on the "root" block but not on "header"?
I think you need to be more specific with your layout handler, you are setting it as and that means all pages, i recommend you change that handler by
<?xml version="1.0"?>
<layout>
<catalog_product_view>
<reference name="product.info">
<block type="core/text" name="free_shipping">
<action method="setText"><text><![CDATA[<div>Free Shipping!</div>]]> </text></action>
</block>
</reference>
<reference name="product.info">
<block name="header">
<action method="unsetChild">
<name>top.search</name>
</action>
</block>
</reference>
</catalog_product_view>
</layout>
Greetings.
First of all, never put your stuff in the base/default folder.
On to your question. What if you try it like this:
<reference name="header">
<action method="unsetChild">
<name>top.search</name>
</action>
</reference>
To answer your other question:
The header is sucessfully removed. So I guess the question now is, why does calling unset child work as expected when used on the "root" block but not on "header"?
It's not the fact it's on the root block, it's that you should use unsetChild within the <reference/> part.
Related
I need to change category and product view for each category/product, for change category list I have add into local.xml:
<layouts>
<CATEGORY_3>
<reference name="product_list">
<action method="setTemplate">
<name>catalog/product/list-1.phtml</name>
</action>
</reference>
</CATEGORY_3>
</layouts>
but I don't know how to change layout from product of category_3, I try adding
<reference name="product.info">
<action method="setTemplate">
<action method="setTemplate">
<template>catalog/product/view-1.phtml</template>
</action>
</action>
</reference>
inside tag but nothing, where is the mistake?
First off, your second XML config block seems to be missing a .phtml extension inside <template></template>.
Second, your action method="setTemplate" is nested twice. Remove the first one
<reference name="product.info">
<!-- action method="setTemplate" REMOVED-->
<action method="setTemplate">
<template>catalog/product/view-1.phtml</template>
</action>
<!-- /action REMOVED-->
</reference>
Also the template you're trying to call
Additionally, Please don't do this from local.xml. You will have caching issues. I guarantee it. This is probably one of the reasons why you're not seeing changes. We did a similar thing last month and it bit us in the arse.
Instead go to Catalog > Manage Categories > (Click on/Select a Category) > Custom Design Tab and then enter your XML layout changes at the Custom Layout Update box. This worked for us better and thus it might give you a better chance of success, too.
Thank you for any help in advance.
I am trying to change the order in which sideboxes appear in magento. Such as the: My Cart, Newsletter, Poll, Recently Viewed Products.
Currently I am trying to make the Newsletter box display last.
In app/design/frontend/default/MY_TEMPLATE/layout/newsletter.xml
I have the following code:
<default>
<!-- Mage_Newsletter -->
<reference name="left">
<block type="newsletter/subscribe" name="newsletter" after="-" template="newsletter/subscribe.phtml" />
</reference>
</default>
Yet even with after="-" as shown in the code, the newsletter is still showing up 1st.
I have flushed all caches, and have re-indexed every time I make a change to no avail.
EDIT:
Changing
<reference name="left">
TO
<reference name="right">
Moves it to the bottom of the list. However, the column is actually on the left. Any ideas why this works?
It's all in the order of left's sortedChildren entries. Try getting rid of the custom newsletter.xml, creating a local.xml file in your custom theme, and adding the following:
<?xml version="1.0"?>
<layout>
<default>
<action method="unsetChild" block="left">
<block>left.newsletter</block>
</action>
<action method="insert" block="left">
<block>left.newsletter</block>
<sib />
<after>1</after>
</action>
</default>
</layout>
What this does is remove the block reference from the list of sorted children blocks and then adds it again at the end. Ref. Mage_Core_Block_Abstract::insert() [link].
I've employed an unused but valid block attribute for the <action /> tag rather than wrap it in <reference />. I believe the effect is the same.
Check the block names(in layout xml files) which is displaying in your left/right sidebar.
You can find something like this
<reference name="right">
<block type="catalog/product_compare_sidebar" after="cart_sidebar" name="catalog.compare.sidebar" template="catalog/product/compare/sidebar.phtml"/>
</reference>
The above code will display cart sidebar then compare sidebar. Consider this is your last block in your sidebar. If you want to newsletter after this block you have to specify
<reference name="right">
<block type="newsletter/subscribe" name="newsletter" after="catalog.compare.sidebar" template="newsletter/subscribe.phtml" />
</reference>
You have to specify block names in after/before element tag. Find your left/right sidebars last block name and use that name in your newsletter block.
I would like to have the topLinks within Magento CE 1.6 displayed within a static block. This is due to the fact that my site is running four different stores [multi-store - different domains] and need to have topLinks on only two stores, whilst using one template.
I did try to convert the php call [getChildHtml('topLinks'); ?>] into a block tag within the static block but was not successful. Have looked in depth at the xml for the template_links [made from varied xmls] but could not come to terms as how to just make a {{block}} within the static block to display the topLinks.
The call for the static block is in place, just need help achieving the topLinks within.
Any help will be appreciated.
With best regards
Fab
A fine tune of my question:
Basically I need to amend the page.xml
from
<block type="page/template_links" name="top.links" as="topLinks"/>
to
<layout>
<static_block_top_links>
<reference name="header">
<action method="unsetChild">
<name>topLinks</name>
</action>
<block type="cms/block" before="-" name="some_name" as="topLinks">
<action method="setBlockId">
<name>some_static_block</name>
</action>
</block>
</reference>
</static_block_top_links>
<STORE_store>
<update handle="static_block_top_links" />
</STORE_store>
<STORE_law>
<update handle="static_block_top_links" />
</STORE_law>
Use local.xml to implement your changes:
<?xml version="1.0" encoding="UTF-8"?>
<layout>
<default>
<reference name="header">
<!-- Unset original toplinks block -->
<action method="unsetChild">
<name>topLinks</name>
</action>
<!-- Add static block in place with same alias -->
<block type="cms/block" before="-" name="some_name" as="topLinks">
<action method="setBlockId">
<name>some_static_block</name>
</action>
</block>
</reference>
</default>
</layout>
Please note that 'some_name' can be anything except for 'top.links', as that would cause several things in core XML files to try and perform actions on your cms block.
In response to your edit, you can easily do it for only some stores like so:
<?xml version="1.0" encoding="UTF-8"?>
<layout>
<static_block_top_links>
<reference name="header">
<action method="unsetChild">
<name>topLinks</name>
</action>
<block type="cms/block" before="-" name="some_name" as="topLinks">
<action method="setBlockId">
<name>some_static_block</name>
</action>
</block>
</reference>
</static_block_top_links>
<STORE_myfirststore>
<update handle="static_block_top_links" />
</STORE_myfirststore>
<STORE_mysecondstore>
<update handle="static_block_top_links" />
</STORE_mysecondstore>
</layout>
Hello for anyone of you having a magento CE 1.6+ multi-store multi-domain and would like to remove the topLinks in general for certain stores, this is the correct method.
Create a local.xml in your app/design/frontend/default/yourtheme/layout/
Your local.xml should be like this
<?xml version="1.0" encoding="UTF-8"?>
<layout>
<STORE_mystore1>
<reference name="header">
<action method="unsetChild">
<name>topLinks</name>
</action>
</reference>
</STORE_mystore1>
<STORE_mystore2>
<reference name="header">
<action method="unsetChild">
<name>topLinks</name>
</action>
</reference>
</STORE_mystore2>
</layout>
Replace the mystore1 and mystore2 with the code under Store View [Admin -> Manage Stores -> Store View Name -> Code]
Make sure that you encode the layout.xml in UTF-8
Upload the layout.xml in the app/design/frontend/default/yourtheme/layout/ folder.
Refresh the cache.
I would like to thank Daniel Sloof and Robert Popovic for their input.
Trying to get the home page to display a 4 column grid for the display of items using a two_column_right template, in the local.xml file. Unfortunately it's taking on the three column grid I've specified for catalog pages elsewhere :/
Possibly need to insert <update handle="four_column_grid" /> under a tag referencing the home page??
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<four_column_grid>
<reference name="product_list">
<action method="setColumnCount">
<count>4</count>
</action>
</reference>
</four_column_grid>
<three_column_grid>
<reference name="product_list">
<action method="setColumnCount">
<count>3</count>
</action>
</reference>
</three_column_grid>
<default>
<!-- Header -->
<reference name="header">
<action method="unsetChild"><name>welcome</name></action>
</reference>
<!-- Root -->
<reference name="root">
<action method="unsetChild"><name>breadcrumbs</name></action>
</reference>
<reference name="footer">
<!-- Remove all the other Magento links - "Site Map, Search Terms, Advanced Search, and Contact Us" -->
<!-- <action method="unsetChild"><name>footer_links</name></action> -->
</reference>
<!-- Right sidebar -->
<reference name="right">
<remove name="paypal.partner.right.logo"/>
</reference>
</default>
<catalog_category_default>
<update handle="three_column_grid" />
</catalog_category_default>
<catalog_category_layered>
<update handle="three_column_grid" />
</catalog_category_layered>
</layout>
Short answer: you can't set values on blocks "inside" of CMS blocks using layout XML.
When loadLayout() is called in action controllers, the layout XML is processed, all blocks are instantiated, and the <action> nodes are executed. But the blocks are not yet rendered.
When renderLayout() is called the blocks are rendered by calling their toHtml() method.
If the block happens to be a cms/block (or cms/page) instance containing a {{block ...}} instance, that block will be instantiated at this time.
At this moment during the request flow all layout XML <action> nodes already have been processed.
In essence you are referencing a block instance in the layout XML that doesn't yet exist.
As a workaround, it might work for you to add the product list block to the homepage using layout XML, too. The downside is that you can't place it freely in other content of the CMS block.
<cms_index_index><!-- layout handle for the default homepage action -->
<reference name="content">
<block type="catalog/product_list" name="product_list">
<action method="setTemplate">
<template>catalog/product/list.phtml</template>
</action>
<action method="setCategoryId">
<catId>51</catId>
</action>
<action method="setColumnCount">
<count>4</count>
</action>
</block>
</reference>
</cms_index_index>
Of course you are not limited to the product list block. If you need to place the list inside of other content, you could add cms blocks to the homepage using layout XML ad well.
Note that this seems to have changed in magento ce 1.9+ when extending the rwd theme. You'll have to define to more blocks for 'name.after' and 'after'.
<cms_index_index>
<reference name="content">
<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
<block type="core/text_list" name="product_list.name.after" as="name.after" />
<block type="core/text_list" name="product_list.after" as="after" />
<action method="setCategoryId"><catId>3</catId></action>
<action method="setColumnCount"><count>4</count></action>
</block>
</reference>
</cms_index_index>
Is it possible to change the order of already existing blocks via the local.xml file?
I know you can change the order of a block with the after or before attribute, but how can one change those attributes of existing blocks.
For example, if I want to place the layered navigation block underneath the newsletter subscription block in the left column, how would I do that?
You need to perform a small trick, remove child block and add it in new position:
<reference name="parent.block.name">
<action method="unsetChild">
<alias>child_block_alias</alias>
</action>
<action method="insert">
<blockName>child.block.name</blockName>
<siblingName>name_of_block</siblingName>
<after>1</after>
<alias>child_block_alias</alias>
</action>
</reference>
This Layout XML instruction does what you want. Look at this short reference of parameters for insert method:
blockName is your block unique name across the layout, product.view for example
siblingName is an block unique name, that is already exists in insertion target block, used for positioning of your block. Leave empty to display it at the top or at the bottom.
after is a boolean identifier of block position. If equals to 1, then the block will be added after siblingName or in the bottom of the children list if siblingName is empty
alias is the alias of your block, if it is empty the name of block will be used.
Some Examples:
Move cart sidebar block after recently viewed products
<reference name="right">
<action method="unsetChild">
<alias>cart_sidebar</alias>
</action>
<action method="insert">
<blockName>cart_sidebar</blockName>
<siblingName>right.reports.product.viewed</siblingName>
<after>1</after>
</action>
</reference>
Move cart sidebar block before recently viewed products
<reference name="right">
<action method="unsetChild">
<alias>cart_sidebar</alias>
</action>
<action method="insert">
<blockName>cart_sidebar</blockName>
<siblingName>right.reports.product.viewed</siblingName>
<after>0</after>
</action>
</reference>
Move cart sidebar block at the end of the right block
<reference name="right">
<action method="unsetChild">
<alias>cart_sidebar</alias>
</action>
<action method="insert">
<blockName>cart_sidebar</blockName>
<siblingName></siblingName>
<after>1</after>
</action>
</reference>
Move cart sidebar block at the top of the left block
<reference name="right">
<action method="unsetChild">
<alias>cart_sidebar</alias>
</action>
</reference>
<reference name="left">
<action method="insert">
<blockName>cart_sidebar</blockName>
</action>
</reference>
Enjoy working with Magento!
You can remove previous layered navigation block and add a new layered navigation block after newsletter block.
<reference name="left">
<remove name="catalog.leftnav" />
<block type="catalog/layer_view" name="catalog.leftnavcustom" after="left.newsletter" template="catalog/layer/view.phtml"/>
</reference>
Note that I use a custom name for the new block.
The accepted answer didn't work for me (EE1.14) but something close to it, this:
<wishlist_index_index>
<reference name="customer.wishlist.items">
<action method="unsetChild">
<name>customer.wishlist.price</name>
</action>
<action method="insert">
<blockName>customer.wishlist.price</blockName>
<after>customer.wishlist.qty</after>
</action>
</reference>
</wishlist_index_index>