magento $this->getCurrencies() can't work? - magento

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.

Related

How to add custom content on Magento Catalog page under each product name?

I have created a custom module that attaches some information to each product. Now, I want to show the custom information on Catalog page under product name.
Here is snapshot to show you what I am trying to do.
Here is my layout xml
<?xml version="1.0"?>
<!--
/**
* Custom module
*/
-->
<layout>
<catalog_category_default translate="label">
<reference name="name.after">
<block type="catalog/product_list" name="custom_content" template="custommodule/default.phtml"/>
</reference>
</catalog_category_default>
</layout>
But this is not working. Is it possible to render custom content under product name without modifying any .phtml files?
Many Thanks!
I'm aware that this is a quick and dirty hack that more diligent magento devs will sneer at, but it will give you the result you want and is easy for you to impliment
The 'correct' solution probably involves writing a custom block and calling it in the foreach loop of your app/design/frontend/YourPackage/YourTheme/template/product/list.phtml - however, I can offer you a quick and dirty hackaround that shouldn't tax you too much. You can do it very easily by abusing the product attributes system.
First create a new product attribute in the attribute set used by your product.
We'll use this to store the custom content we want to inject between your product name and price. For the sake of this example we'll call it customcontent. Make sure you set the attribute's Visible on "Product View Page on Front-end" value to 'No'.
Edit your product and set this new attribute equal to your desired custom content - it's fine you use html tags in here too. By default, product attributes have (for some reason) an arbitrary limit of 30 characters but if you need it to hold a longer string you can easily change this by editing app/code/mage/eav/model/entity/Attribute.php and changing the value of ATTRIBUTE_CODE_MAX_LENGTH.
Then in your app/design/frontend/YourPackage/YourTheme/template/product/list.phtml and find the following line of code somewhere around line 49
<?php foreach ($_productCollection as $_product): ?>
directly after it insert the following:
<?php $_customcontent =$_product->getResource()->getAttribute('customcontent')->getFrontend()->getValue($_product);?>
You've now stored the string from your products customcontent attribute in a variable called $_customcontent
So if you find the following line in your list.phtml
<h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></h2>
you can easily add a new paragraph or heading beneath it and populate it with your custom content using
<?php echo $_customcontent ?>
I know this probably represents a deeply unorthodox solution, but it does work and can be acheived very quickly, and without requiring you to know anything about writing your own custom blocks.
Can you try :
<catalog_categoryd_default>
<reference name="content">
<block type="catalog/product_list" name="custom_content" template="custommodule/default.phtml" after="nameofTheNameBlock"/>
</reference>
</catalog_categoryd_default>

Magento - How to create a getChildHtml block and call it in another

Currently i'm working to redesign a custom Magento template.
I'm facing the following problem.
In file called: 2columns-left.phtml i am facing code like this several times:
<?php echo $this->getChildHtml('global_messages') ?>
or
<?php echo $this->getChildHtml('global_messages') ?>
These code lines makes me think that they are calling blocks and the content in them by getting the name of .phtml file.
So i have file called slider_layred_nav.phtml and i want to call all the content in it in file 2columns-left.phtml so i tried this code:
So in file 2columns-left.phtml i put:
<?php echo $this->getChildHtml('slider_layred_nav') ?>
But it is just not displaying anything.
I assume that i have to do something with the layout but i have no idea what.
Can you help me our resolve this mystery ?
Thanks in advance!
I guess you have some problem understanding the magento layouts and how the blocks are rendered in the template files.
In simple words,getChildHtml() renders all the blocks inside that particular block i.e child blocks of that parent block.If you use getChildHtml('slider_layred_nav') than it will render the block named slider_layred_nav not the template file.
Here $this has it own meaning. It refers to the block which has template file 2columns-left.phtml.
At first you have to create a block inside the block which use the template 2columns-left.phtml
For example:
<block type="core/template" name="slider_layred_nav" template="something/slider_layred_nav.phtml"/>
After creating this block inside the block that uses the template 2columns-left.phtml you can do echo $this->getChildHtml('slider_layred_nav'); to display that block inside your 2columns-left.phtml file.
Hope i made you clear to some extent.
You are indeed missing the layout part.
If you open up page.xml (under layout folder of your theme), you can find the definition of the block global_messages like so :
<block type="core/messages" name="global_messages" as="global_messages"/>
But don't take that block as example, since it is a special one which does not have a template associated with it.
The cleaner way to make what you want is like so :
Create a local.xml file under your in app/design/frontend/your_package/your_theme/layout (nota: your_package & your_theme will vary).
Then paste this one in the new local.xml file :
<?xml version="1.0"?>
<layout version="0.1.0"><!-- everything goes in here -->
<default>
<block type="core/template" name="slider_layred_nav" template="path/to/slider_layred_nav.phtml"/>
</default>
</layout>
path/to/slider_layred_nav.phtml being the path to your phtml from app/design/frontend/your_package/your_theme/template/
slider_layred_nav in the name attribute of the block being the name passed as parameter when you are calling $this->getChildHtml('some_name').

Magento Including a CUSTOM phtml file in view.phtml

I am trying to work out how to create custom phtml files to include on view.phtml (and ultimately to be called from any default Magento phtml file).
I have created a seperate phtml file with the content I want in it called productbadges.phtml
This will be pulled through as the last item in
I understand the callout usually is
<?php echo $this->getChildHtml('phtmlfilename') ?>
However I know I need to do add something to catalog.xml so Magento recognizes the callout and can source the correct file. But I do not properly understand Magento's XML syntax.
Could anyone assist?
vicch's response is the correct way of doing it.
However, it's also helpful to know that there is an alternate method:
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'choose_a_block_name',
array('template' => 'folder/myphtmlfile.phtml')
);
I am posting this for general knowledge. This is not the accepted way of doing this, since it is not consistent with how Magento templates and blocks are used.
you can use
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml(); ?>
see also here:
How do i call .phtml block at specfic page in magento?
and
want to call one phtml file in another phtml file using anchor tag
Given the information you provided, I can only give a general solution.
First you need to find the layout XML for this view.phtml. You should be looking for something like:
<block type="..." name="..." ... template="../view.phtml">
To add the declaration of the new template directly under the wrapping block, it should be:
<block type="..." name="..." ... template="../view.phtml">
<block type="..." name="phtmlfilename" template="../phtmlfilename.phtml"/>
...
</block>
It is also possible to reference the outter block somewhere else:
<reference name="[name_of_view.phtml_block]">
<block type="..." name="phtmlfilename" template="../phtmlfilename.phtml"/>
</reference>
Type of the new template is the class name, which should be core/template or a subtype of it.
The answer to this question is below codes,just change "directory/acc_drop.phtml" to your file path name.
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('directory/acc_drop.phtml')->toHtml(); ?>

In Magento, can I add a static block to the header by xml only?

I am trying to customize a theme using only local.xml whenever possible. I want to add a static block to the header without modifying header.phtml. This code works fine for the content area, but not for the header:
<default>
<reference name="content">
<block type="cms/block" name="how-it-works-button">
<action method="setBlockId"><block_id>how-it-works</block_id></action>
</block>
</reference>
</default>
Anybody know why? I thought that all I would need is to change “content” to “header”, but nothing shows up when I do.
Thanks for your help!
The content block is a special block known as a core/text_list block (PHP class Mage_Core_Block_Text_List). These blocks will automatically render out any child blocks that are added to them.
The header block, on the other hand, is a page/html_header block (PHP class Mage_Page_Block_Html_Header). This block class inherits from Mage_Core_Block_Template, making it a core/template block. Template blocks will only render sub-blocks if their corresponding phtml template requests the block. So, by adding your block to the header, you're only doing half the work you need to. You'll need to create a custom phtml template.
The simplest way to do this (post 1.4.1.1 is to, in your own theme, create a file at
template/page/html/header.phtml
And then at the end of this file add
<?php echo $this->getChildHtml('how-it-works-button'); ?>
Assuming you've added a block to header block via layout xml, this should render your template.
Please Try this
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('how-it-works')->toHtml() ?>
And this code in header.phtml
add output="toHtml" in the block tag. I think it is only that.

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