Insert new block to product.info doesn't work - magento

I have created two new blocks, which should be output before and after this block:
<block type="catalog/product_view" name="product.info.addtocart" as="addtocart" template="catalog/product/view/addtocart.phtml"/>
To do this I've created a new module, which updates the layout. In the refered XML file stands this:
<catalog_product_view>
<reference name="product.info">
<block type="disablecartonlogout/output" name="disablecartonlogout.outputbefore" as="whatever" before="product.info.addtocart" template="disablecartonlogout/product-options-bottom-before.phtml" />
<block type="disablecartonlogout/output" name="disablecartonlogout.outputafter" as="whatever" after="product.info.addtocart" template="disablecartonlogout/product-options-bottom-after.phtml" />
</reference>
</catalog_product_view>
I get no output.
If I change the reference to "content", the blocks will be outputted, but on the wrong position.
Can you tell me, what I am doing wrong?

You need to explicitly call your blocks in the product.info block template (that should be catalog/product/view.phtml), at the place you want.
The method you want to use only works for some specific blocks (usually, core/text_list or core/template doing an empty getChildHtml() call, what's not the case of product.info)

<catalog_product_view>
<reference name="product.info.addtocart">
<block type="disablecartonlogout/output" name="disablecartonlogout.outputbefore" as="whatever1" before="-" template="disablecartonlogout/product-options-bottom-before.phtml" />
<block type="disablecartonlogout/output" name="disablecartonlogout.outputafter" as="whatever2" after="-" template="disablecartonlogout/product-options-bottom-after.phtml" />
</reference>
</catalog_product_view>
then go to:
app\design\frontend\default\themeName\template\catalog\product\view\addtocart.phtml
and add
$this->getChildHtml('whatever1', true, true);
$this->getChildHtml('whatever2', true, true);
Try this as it may help you. I tried something like this

Related

Magento: Append block to before_body_end

I'm wondering why the method given here https://magento.stackexchange.com/a/3471 doesn't work for the xml block <reference name="before_body_end"/>?
I know the block type for before_body_end is core/text_list but Mage_Core_Block_Text still inherits Mage_Core_Block_Abstract which has the append method, so my understanding of Magento is that it should work, but I'm obviously missing something here?
EDIT:
This is an example of what is being attempted:
<block type="core/template" name="myblock" template="path/to/myblock.phtml" />
<reference name="before_body_end">
<action method="append" ifconfig="mymodule/settings/enabled">
<block>myblock</block>
</action>
</reference>

Magento creating CMS blocks from xml block

<reference name="left">
<!-- Layered Navigation Block -->
<block type="catalog/layer_view" name="catalog.leftnav" template="catalog/layer/view.phtml" >
<action method="setCategoryId"><category_id>2</category_id></action>
</block>
</reference>
I was able to create a CMS block from the above code
{{block type="catalog/layer_view" category_id="2" name="catalog.leftnav" template="catalog/layer/view.phtml"}}
How would I create a CMS block from the XML below...it have a bit more to it.
<reference name="content">
<block type="catalog/product_list" name="home" template="catalog/product/list.phtml">
<!-- Product List View -->
<action method="setCategoryId"><category_id>2</category_id></action>
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>
Unfortunately you cannot do what you need.
Here is why. When using the {{block}} directive magento automatically calls toHtml after creating the block and returns you the html (rendered block).
This directive also does not support blocks inside blocks.
When working with layout files, the blocks are not rendered automatically. First the layout file is parsed, the block classes are instantiated, they are assigned as child blocks or parent blocks and everything is rendered at the end.
In your case you will need something like this:
{{block type="catalog/product_list" template="catalog/product/list.phtml" name="home" toolbar_block_name="product_list_toolbar"}}
But this won't work because the block with the name product_list_toolbar does not exist yet.
If you try to create it before with something like this:
{{block type="catalog/product_list_toolbar"....}}
still won't work, because the block will just be outputed (most probably you will get an error too).
Blocks generated using {{block}} are not added to the layout so they cannot be referenced in other blocks.

How to place my custom block inside another block in Magento using layout xml?

first of all I want to say that I have searched for this whole day on te internet and could not find what I wanted. I am a newbie here as well, so please forgive me if I broke any rule.
I am trying to develop a module which will add videos to product page along with images. I am stuck in this concept:
How do I insert my block into an existing base block ? For example, in the product page, there is a block product.info. Inside this block there is "Availability", "Price" etc.
How do I insert my custom block just below "Availability" and above "Prices" using my module's layout xml and template.
So I am trying to achieve something like this using my module's layout file:
<catalog_product_view translate="label">
<reference name="content">
<reference name="product.info">
WRITE BLOCK HERE SO THAT MY BLOCK SHOWS BELOW AVAILABLITY
</reference>
</reference>
</catalog_product_view>
Is this possible ? or Do I have to override the core class Mage_Catalog_Block_Product_View to do this ?
PS: Basically my aim is to list my videos next to images. Right now, I am able to list my videos from module, but images don't come in that case. I used
<block type="myblock/myblock" name="somename" as="media" template="abc.phtml"/>
So I want to append my block to the existing content.
I solved it. I had to rewrite the Mage_Catalog_Block_Product_View_Media .
In my class I over-rid the function _toHtml function like this:
public function _toHtml()
{
$html = parent::_toHtml();
$html.=$this->getChildHtml('media_video');
return $html;
}
where "media_video" is my block. My layout xml file:
<catalog_product_view translate="label">
<reference name="content">
<reference name="product.info">
<reference name="product.info.media">
<block type="myblock/myblock" name="somename" as="media_video" template="beta/abc.phtml"
before="-"/>
</reference>
</reference>
</reference>
</catalog_product_view>
You can add new block instead of over-right existing.
<catalog_product_view translate="label">
<reference name="content">
<reference name="product.info">
<block type="myblock/myblock" name="somename" as="media_new" template="abc.phtml"/>
</reference>
</reference>
</catalog_product_view>
Get New Block using following code in phtml file
<?php echo $this->getChildHtml('media_new') ?>
Thanks
Note output="toHtml" in the below code. This will print your block in product.info section.
<catalog_product_view translate="label">
<reference name="content">
<reference name="product.info">
<block type="myblock/myblock" name="somename" as="media"
template="abc.phtml" output="toHtml" before="-" />
</reference>
</reference>
</catalog_product_view>

What is preventing my layout updates from being displayed

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.

Magento: Update block position with before/after attribute from local.xml layout reference

I need to append the before attribute to a block via a layout update reference call.
This is my local.xml file:
<?xml version="1.0"?>
<layout version="0.1.0">
<default>
<reference name="content">
<block type="page/html_wrapper" name="content.footer" as="contentFooter" translate="label" after="-">
<label>Content Footer</label>
<action method="setElementId"><value>content-footer</value></action>
<action method="setElementClass"><value>clear</value></action>
</block>
</reference>
</default>
<catalog_category_default>
<reference name="breadcrumbs.container">
<action method="unsetChild"><name>category.title</name></action>
</reference>
<reference name="content">
<block type="catalog/category_view" name="category.title" template="catalog/category/title.phtml" before="content.footer"/>
</reference>
</catalog_category_default>
</layout>
My problem is, on the content block I create a content.footer block that you can assign widgets to in admin. I use after="-" on the content.footer block so in my mind, should put it ALWAYS at the bottom of the content block but this is not the case.
When you view a catalog category and it inserts the category.products block in to the content block, it displays underneath the content.footer block. The only way to make it work is if I redefine it in my local.xml and include all the child blocks in category.products, and set before before="content.footer".
So I thought why can't I use a reference to category.products in the catalog_category_default layout and set the block's before attribute, I tried the following:
<reference name="category.products">
<action method="setBefore"><value>content.footer</value></action>
</reference>
Which had no affect.
I also noticed the setAttribute() function in Mage_Core_Block_Abstract which saw it's just a wrapper for setData() but thought I would try it anyway, still nothing:
<reference name="category.products">
<action method="setAttribute"><key>before</key><value>content.footer</value></action>
</reference>
Is it possible to do what I want? Does before/after only apply to blocks in the same reference?
Layout updates are processed in order of layout update handles. Your block is being added last to content but only for the default LUH. Other handles (catalog_product_view, catalog_category_layered, etc) are processed after this handle.
If you truly need a content footer everywhere and want to make sure that it is the last thing inside of the content div, you should add your block to the root node in the default handle and customize the root templates (directly under page/, e.g. page/1column.phtml) by adding a getChildHtml('your.block') call after the getChildHtml('content') call. This will ensure that your block is always immediately at the end of the content blocks.

Resources