Magento remove div by local.xml - magento

I know that I can remove blocks by commands, but is one thing more correct, than the other?
Also, would I somehow be able to remove a .div class instead of a block? so that I can remove a .div from a specific page?
Heres the examples of the snippets I know of, to remove blocks.
<remove name="name_name"></remove>
<action method="unsetChild"><name>name.name</name></action>

Remove - remove block from any level, example block AAA is child of the block left that is child of the block content then I can remove it at top level of page layout:
<xxx_index_index>
<remove name="AAA" />
....
unsetChild we can use only within parent of the target block, example if I have this layout:
<default>
<reference name="content">
<block name="BBB" ... />
Then I can remove BBB with method unsetChild
<yyy_index_index>
<reference name="content">
<action method="unsetChild"><name>BBB</name></action>

No, you can't remove a div HTML element unless there is a block comprised of this element and defined in the layout configuration. You can then remove that block and with it the desired div.
There are no provisions to remove HTML elements by using the Magento Layout settings.
You can, however, use layout config to include a javascript file that employs CSS selectors to remove div elements you want.

Related

Hide category menu using local.xml

How to hide category/menu bar on selected pages like login & registration using local.xml ?
I'm using magento 1.7.0.2
You need to remove block named 'catalog.topnav' for login and registration page handlers. The page handler similar to page URL, but all slashes replaced with '_'. For the login page it will be *customer_account_login* and for the registration page - *customer_account_create*. You can use
<remove name="[blockname]">
or
<action method="unsetChild"><block>[blockname]</block></action>
instructions, first allow you to remove block globally and the second one remove it from certain block.
The Layout update for default magento theme will looks like:
<?xml version="1.0"?>
<layout version="0.1.0">
<customer_account_login>
<remove name="catalog.topnav" />
</customer_account_login>
<customer_account_create>
<remove name="catalog.topnav" />
</customer_account_create>
</layout>
There is some explanation about Magento layouts which can be useful - http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-4-magento-layouts-blocks-and-templates
Remove nodes will be processed after all layout handles are merged, and are a good way to remove a block regardless of which layout handle loaded the block; you just want to get rid of it entirely for some handles! It also removes recursively, so all you need to specify is the layout handle.
On the other hand, you may only want to remove a block from a reference in a specific layout handle, in which case you should use unsetChild. It is often used to remove a block from a reference, but then re-insert the same block with a different position. This would not have been possible with remove.

How can I display a magento block in more than one location?

I'm trying to display the cross sell block in it's normal location as well as at the bottom of the page. There is more than one phtml template file involved so $this->getChildHtml does not work in the second location since the block is setup to only be on the cart.phtml file as of now.
In summary, how can I display the same block in more than one template file? I wan't to place these changes in my local.xml and do not want to modify core Magento template files.
The core layout directive for this block is set up as a child of the checkout.cart block:
<checkout_cart_index translate="label">
<!-- ... -->
<reference name="content">
<block type="checkout/cart" name="checkout.cart">
<!-- ... -->
<block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
</block>
</reference>
</checkout_cart_index>
To add it to the footer, you might need to only establish another parent-child relationship in your local.xml:
<checkout_cart_index>
<reference name="footer">
<action method="insert"><!-- or "append" to add to the end -->
<child>checkout.cart.crosssell</child>
</action>
</reference>
</checkout_cart_index>
This approach relies on the stock page/html/footer.phtml template which contains an empty getChildHtml() call, which causes it to render all child blocks.
Note that the footer block class Mage_Page_Block_Html_Footer has a never-expiring block_html cache lifetime, and it does not evaluate child contents for its cache entry. You may need to disable the cache for this block or rewrite the block class to account for the varied content of the crosssell block.

Placement of Related products - Magento

I have a magento installation where I need to place related products in the center column. This is the easy part. I moved
<block type="catalog/product_list_related" name="catalog.product.related" after="container1" template="catalog/product/list/related.phtml"/>
From the right reference block, to the bottom of the center reference block.
With this I achieved to place the related products in the center column, but all the way at the bottom.
I need the related products to be placed just above the price block in the div (class: product-shop)
I tried to position it with the After/before parameter in the XML but this doesnt seem to work.
If I place the block code higher up in the XML it doesn't show at all.
Moving blocks is quite easy to do correctly (i.e. using best practices). Best practices include not customizing any core layout file when possible as well as working with original block instances rather than reinstantiating them. All of this can be achieve using the custom layout file available to end-implementers.
Create a local.xml file in your custom theme's layout folder e.g. app/design/frontend/[package]/[theme]/layout/local.xml, and in there add the following:
<?xml version="1.0" encoding="UTF-8"?>
<layout>
<!--
In Magento v1 a move is accomplished by unsetting in one place
and inserting in another. This is possible using just layout xml
when the "new" parent block is a "Mage_Core_Block_Text_List"
instance. Otherwise a template needs editing.
In Magento v2 there will be actual "move" functionality.
-->
<catalog_product_view>
<reference name="right">
<!-- remove the block name from a parent -->
<action method="unsetChild">
<block>catalog.product.related</block>
</action>
</reference>
<reference name="content">
<!-- add the block name to a parent -->
<action method="insert">
<block>catalog.product.related</block>
</action>
<!--
Mage_Core_Block_Abstract::insert() accepts multiple arguments,
but by default it will insert the added block at the beginning
of the list of child blocks.
-->
</reference>
</catalog_product_view>
</layout>
You can revert the change to the original layout xml file at this point.

Magento: Referencing a custom block does not work

I'm trying to reference a block from an other custom module to add a child block via layout file but it does not work.
The first layout file contains
<catalog_product_view>
<reference name="content">
<block type="core/template" name="tabcontainer" as="tabcontainer"
template="store/tabcontainer.phtml" >
<block type="catalog/product_list_related" name="kitparts"
template="store/product/kitparts.phtml"/>
</block>
</reference>
</catalog_product_view>
and in the second one I try to reference the tabcontainer block
<catalog_product_view>
<reference name="tabcontainer">
<block type="productshippinginfo/productshipping" name="productshippinginfo"
template="productshippinginfo/productshipping.phtml" after="kitparts"/>
</reference>
</catalog_product_view>
but the productshippinginfo block is not displayed while it is definitely included in the layout (using Alan Storm's layoutviewer plugin). If I reference content it is displayed.
What is wrong? Isn't it possible to add a child to a custom block from a custom extension?
Thanks for your help!
(I'm using Magento 1.6.1.0)
[edit]
in tabcontainer.phtml I'm calling <?php echo $this->getChildHtml(); ?>
First of all: Thank you Vinai!
Adding a dependency to control the loading order of my plugins it works!
in File: app/etc/modules/Company_ContentModule.xml
<Company_ContentModule>
<active>true</active>
<codePool>local</codePool>
<depends>
<Company_ContainerModule />
</depends>
</Company_ContentModule>
So the content module is loaded after the container module.
You are close. You just need to add this to you store/tabcontainer.phtml file:
getChildHtml('productshippinginfo'); ?>
The reason blocks that are children of "content" render without a template change is that the "content" block is a core/text_list block. If you look in Mage_Core_Block_Text_List, you will see that in its rendering method (_toHtml()) it renders its children.
You could also add an empty getChildHtml() call to your tabcontainer template to achieve a similar effect as a core/text_list - in fact, if you use getChildHtml('',false,true); you'll get the sorted children (set with before="" and after="" params).
EDIT: adjusted the getChildHtml() call syntax based on OP's comment correct findings that the first param must be an empty string a/o/t a boolean.
In the second layout I think you need to provide the nesting:
<catalog_product_view>
<reference name="content">
<reference name="tabcontainer">
<block type="productshippinginfo/productshipping" name="productshippinginfo"
template="productshippinginfo/productshipping.phtml" after="kitparts"/>
</reference>
</reference>
</catalog_product_view>
In order that maganto picks that up
And because you are doing
<?php echo $this->getChildHtml(); ?>
You do not need to specifically call it by name unless you want it to appear in a particular place in your HTML output.
In order to test if your block is appearing in the page at all add output="toHtml" in you block tag.
<block type="productshippinginfo/productshipping" name="productshippinginfo"
template="productshippinginfo/productshipping.phtml" after="kitparts" output="toHtml"/>

Magento Store - Remove Block using Update XML

I am using this code in my template file to display a static block in my left sidebar:
<?= $this->getLayout()->createBlock('cms/block')->setBlockId('leftSB1')->toHtml() ?>
I would like to exclude the block from one of my CMS pages. How do I do this?
I think it requires adding code to the 'Layout Update XML' section but I'm not sure what exactly.
Someone else can correct me here, but I'm fairly sure that you're going to have trouble trying to accomplish this given the way you called the block. Normal layout updates allow you to remove blocks, but those are blocks that were also created with the layout (e.g. the Layout object knows about them after you call loadLayout()).
In your case, you create the block on the fly and then immediately use it to echo some HTML. If you want to be able to delete it with layout updates, try moving it into the layout files first, then use the normal layout block removal method:
<reference name="your_parent_block_name">
<remove name="leftSB1"/>
</reference>
Otherwise, you could hide it either in the PHP (By setting some global variable and checking it before outputting the block. Poor form but it might work.) or in CSS. Let me know if any of these work for you.
Thanks,
Joe
Include the block in your layout instead:
<cms_page>
<reference name="left">
<block type="cms/block" name="leftSB1">
<action method="setBlockId"><id>leftSB1</id></action>
</block>
</reference>
</cms_page>
And then $this->getChildHtml('leftSB1') in your sidebar, if you're not including children automatically.
(and then remove it from the particular page as in the previous answer)

Resources