I'm having some trouble getting a custom Reference block to work in Magento.
These are the steps I have taken:
Step 1
Created a new “Reference” block in page.xml
<block type="core/text_list" name="newreference" as="newreference"></block>
Step 2
Added a reference to this block in the place I want it to appear in the page (above the footer in 1column.phtml, 2columns-left.phtml, 2columns-right.phtml, 3columns.phtml)
<?php $this->getChildHtml('newreference'); ?>
Step 3
Added a reference to catalog.xml which tells Magento I want to output a template part (specialfooter.phtml) in the ‘newreference’ Reference block on Category pages
<reference name="newreference">
<block type="core/template" name="specialfooter" template="page/html/specialfooter.phtml"></block>
</reference>
Step 4
Created ‘specialfooter.phtml’ in the page/html/ directory with a simple paragraph block to test.
And nothing happens.
The steps I have taken fit with my understanding of how Reference blocks work, but I could be wrong. I'm struggling to find any documentation, official or otherwise, or any previous SO questions which sheds any light on the subject.
I'm using Magento ver. 1.7.0.2.
Any help would be much appreciated.
Don't you have forget a echo ? :
<?php echo $this->getChildHtml('newreference'); ?>
I was having the same problem and this seems to work for me.
This block in layout/page.xml
<block type="page/html/new_newreference" name="newreference" as="newreference" template="page/html/new/newreference.phtml"/>
Can be referenced in a page eg. 1column.phtml in the template/page folder using:
<?php echo $this->getChildHtml('newreference') ?>
Note the correlation between the "type" naming and "template" path and the "name" and "as" with the getChildHtml().
Using the same principle for a product page. This block in layout/catalog.xml
<block type="catalog/product_new" name="catalogreference" as="catalogreference" template="catalog/product/new/catalogreference.html"/>
Can be referenced in template/catalog/product/view.phtml using:
<?php echo $this->getChildHtml('catalogreference'); ?>
Note both these examples are folder specific
If you want a block to use with a widget. Add this block in the appropriate reference block eg "content" or "head" in the relevant xml file eg. page.xml or catalog.xml:
<block type="core/text_list" name="mywidgetblock" as="mywidgetblock">
<label>My widget Block</label>
</block>
NB: I don't understand the "type" declaration but it works?
In the admin panel CMS/Widget/Widget instance new or existing Layout Updates/ Block Reference find "My widget Block" from the drop down.
I'm new to Magento and it took a lot of trial and error to work this out so I hope it helps someone in the same situation.
Related
i put the code in my theme header.phtml
echo $this->getCurrencyCount() and $this->getCurrentCurrencyCode() both of them are no any output. but i have set 4 currencies for the site.
but in the same file, $this->getWelcome() have the right output.why?
when i echo Mage::app()->getStore()->getCurrentCurrencyCode(); in the file ,it has a value. thank you.
if i want to output the currency switcher. how do i do?
These function are from the Mage_Directory_Block_Currency model, not the Mage_Page_Block_Html_Header one.
You might look at the directory/currency.phtml file to handle this kind of task
IF you want to determine the best position in the page thanks to the header.phtml file, just define this block as a child of the header one.
In your theme layouts, in directory.xml, in the <default> area add this :
<reference name="header">
<block type="directory/currency" name="header_currency" before="catalog.leftnav" template="directory/currency.phtml"/>
</reference>
Then in the header.phtml file just add echo $this->getChildHtml('currency'); where you need it to show.
If you just need these variables without showing the currency block use in your header.phtml this code
$currency_block = new Mage_Directory_Block_Currency;
$currency_block->getCurrentCurrencyCode();
The last part is provided as is and is untested.
I try to move an ext: Customers who bought this product also purchased from default left/right column to main column under the product, but I don't know how to do this.
I try to change the line <product_detail_leftposition translate="label"> to <product_page translate="label"> in system.xml file but it doesn't help.
Any suggestions?
Thanks for any help!
You can do this by adding it to the Products view page section inside your layout XML. Add the block inside the content reference block, somewhere at the bottom.
Make sure it's inside the handle as this defines the structure used on the product view page.
app/design/frontend/[namespace]/[themename]/layout/catalog.xml
<catalog_product_view translate="label">
...
<reference name="content">
...
<block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
<!-- Other blocks etc here.. add at the bottom -->
<block type="module/block" name="yourblock" as="yourblock" />
</block>
</reference>
</catalog_product_view>
This should automatically output the block for you if you add it in here, although that depends on the theme you are using.
if it doesn't automatically show, you may need to add a line to your view to display the block:
app/design/frontend/[namespace]/[themename]/template/catalog/product/view.phtml
<?php // use the block name used in the XML config.. ?>
<?php echo $this->getChildHtml('yourblock') ?>
In my site home page, I have three blocks like Recommend product,Top ten product and Talking about product, All products are coming properly by their attribute name.For this, I create three attribute in admin section and add layout design in home page in admin section and add block type in page.xml and i call this blocks by their name in 2column-left.phtml
echo $this->getChilidHtml("block_names")
But,My problem is when i click on any product it will redirect to product details page that is catlog/product/view.phtml, on this page i want to add two different blocks as like home page ,I found its xml page also , I think that is catalog.xml on this page make changes in side the content reference and it is coming ,but i want to add a new block as line content , i was trying but i am not getting any idea please any solution for this.
<catalog_category_layered>
<reference name="recommend">
<block type="catalog/product_list" name="catalog.product_list" as="recommend_list" template="catalog/product/view/recommend.phtml">
</block>
</reference>
</catalog_category_layered>
I add above line code in catalog.xml as a new block and call it in view.phtml as echo $this->getChildHtml('recommend') but it is not showing,I am sure this approach is wrong one .
any solution for this how can i call.
Thanks & Regards
Try this,In your catalog.xml
<catalog_product_view translate="label">// find this line
<reference name="content">
..............
<block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
<block type="catalog/product_list" name="catalog.product_list" as="recommend_list" template="catalog/product/view/recommend.phtml"/> //add your block into product.info block
.............
</block>
</reference>
................
</catalog_product_view>
call the your block using name in in catalog/product/view.phtml
echo $this->getChildHtml('recommend_list')
Hope this helps
PROBLEM:
I'm trying to build an AJAX cart in magento, but I can't seem to fetch the chosen configuration option(s) of configurable products. I believe am invoking the the right method ($this->getOptionList()) to get them. It should return an array with options and labels, but it returns nothing!! To be clear, they are showing up in the normal cart.
EXPLANATION:
The short story of how I'm doing it:
I use the Cart Controller of the Mage_Checkout module, but I change the template to something very minimal (using layout updates in the current theme), and a custom module with a router defined in it.
Long story:
I have created a module MyNameSpace_Checkout in which I have defined a router that binds the frontName 'ajaxcart' to the controllers in the Mage_Checkout module.
Now in the local.xml file of my current theme I put the following layout updates under the 'ajaxcart_cart_index' handle:
<ajaxcart_cart_index>
<reference name="root">
<action method="setTemplate">
<template>ajaxcart/cart/index.phtml</template>
</action>
<block type="checkout/cart" name="checkout.cart" template="ajaxcart/cart/show.phtml" as="cart"></block>
</reference>
</ajaxcart_cart_index>
My template ('ajaxcart/cart/show.phtml') is being used, so this worked pretty well. I tested it when I went to http://domain.com/ajaxcart/cart
index.phtml:
<?php echo $this->getChildHtml('cart'); ?>
show.phtml:
<?php foreach($this->getItems() as $_item): ?>
<?php $_renderer = $this->getItemRenderer($_item->getProductType())->setItem($_item); ?>
<?php /* render an item */ ?>
<?php endforeach; ?>
Seeing as $this in this context refers to the Cart Block of the Mage_Checkout module, and digging around in the method getItemHtml() of this class (its superclass actually) I found that the block object per item in the cart is retrieved using the second line in the show.phtml sample above ($_renderer).
Does anyone know why information is missing? The whole reason I'm using the original controller is that it is probably doing some essential stuff, but it's still not working!!
Thanks in advance.
I found the problem myself, the layout updates (local.xml of the current theme) were the problem:
<action method="addItemRender">
<type>configurable</type>
<block>checkout/cart_item_renderer_configurable</block>
<template>checkout/cart/item/default.phtml</template>
</action>
I took another look at checkout.xml of the base/default theme and it sported some addItemRender (layout xml) methods in the cart/checkout block under the checkout_cart_index handle. Specifically for my problem, the above element was missing and that messed up the rendering of a configurable product item in the cart. A special type of block object needs to be loaded that actually has the (PHP) method getOptionList().
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)