Magento, hide the toolbar only in a specific block - php

I'm trying to hide the toolbar of the products only in the block footer but without success. I have tried using the function
<?php if(Mage::app()->getLayout()->getBlockName () != "footer"): ?>
but it returned an error. I do not want me to hide the toolbar across the page, but only in a specific block.
How can I hide the toolbar in a specific block?
Thanks for any help.

You can remove block using chenging layout xml,add like as below in your local.xml file
<reference name="footer">
<remove name="block name" />
</reference>
This URL have more details about local.xml
http://magebase.com/magento-tutorials/5-useful-tricks-for-your-magento-local-xml/

Although I would suggest doing most of those kind of task in the layout, as Shijin is proposing it. If you tried it that way and for some reason cannot do it properly in the layout, you can use the method getNameInLayout available for any block extending Mage_Core_Block_Abstract
<?php if($this->getNameInLayout() != "footer"): ?>

Related

Trying to add a child block to adminhtml form

I am stuck a little with following problem:
I created a Fom in the adminhtml area using the pure php from Magento and standard template.
This all works as expected, but I cannot dynamically add and remove fields, I need Javascript and my own phtml file for that.
My Idea now was to simply include a childblock which is using a custom template at the end of the form.
So far I have added
$cblock =$this->getLayout()
->createBlock('netcon_konmod/adminhtml_caps_edit_mat')
->setTemplate('netcon_konmod_mat.phtml');
$this->_addContent($this->getLayout()
->createBlock('netcon_konmod/adminhtml_caps_edit')->setChild($cblock));
to my controller, created an empty Netcon_Konmod_Block_Adminhtml_Caps_Edit_Mat class which extends Mage_Adminhtml_Block_Widget
and created template file mat.phtml in .../template/netcon/konmod/
I also have my konmod.xml which updates my layout and which includes
<adminhtml_caps_edit>
<reference name="content">
<block type="netcon_konmod/adminhtml_caps_edit" name="netcon_konmod_caps_edit">
<block type="netcon_konmod/adminhtml_caps_edit_mat" name="netcon_konmod_caps_mat" template="netcon_konmod_mat.phtml" />
</block>
</reference>
</adminhtml_caps_edit>
I am rather new to Magento, and until now I only found ways to include a child block by calling getChildHtml from an already existing phtml file of the parent block.
However, since I create my form completely with the form and fieldset methods, I don't have my own phtml file in which I could include that call.
The way I have it set up right now, it is displaying the form normally, but does not include the childblock.
Any help would be appreciated, if it is at all possible to do this, I would like to avoid recoding my whole form as template.
Try
$childBlock = $this->getLayout()
->createBlock('konmod/adminhtml_caps_edit_mat')
->setTemplate('netcon_konmod_mat.phtml');
$this->_addContent($this->getLayout()
->createBlock('konmod/adminhtml_caps_edit')
//->setTemplate('netcon_konmod_mat.phtml')
->append($childBlock));

How do I access methods like $this->getChildHtml() in other directories/pages in Magento?

All I want to do is move a search box. This search box is currently displayed in the header, directly next to the logo, and is generated by the following code:
<?php echo $this->getChildHtml('topSearch') ?>
in /app/design/frontend/MYTHEME/default/template/page/html/header.phtml
I would like this search box inline with the navigation links, which are located in top.phtml which is located in another directory. But when I use the code
<?php echo $this->getChildHtml('topSearch') ?>
the search box does not appear. I understand that this is because the meaning of $this has changed, but what I don't understand is how to display the search box? What do I replace $this with?
I tried replacing it with Mage_Page_Block_Html_Header since that was the definition of $this in header.phtml, but to no avail. Can anybody point me in the right direction, or provide an explanation as to how I access methods once the definition of $this has changed?
You need to make layout update and include block topSearch into block containing top.phtml.
Look into app/design/frontend/.../layouts/...xml files, find how topSearch is declared and then find where the block using top.phtml template is declared. Then move topSearch block as a child of top block. I mean add layout xml update like this:
<default>
<reference name="catalog.topnav">
<block type="core/template" name="top.search" as="topSearch" template="catalogsearch/form.mini.phtml"/>
</reference>
</default>
Another solution is try next in your template:
echo $this->getLayout()->getBlock('top.search')->toHtml()
If it would not work then find in layouts topSearch block and try to use in code above name of block instead of alias.
You can read more about Magento view level here: http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/0_-_theming_in_magento/designing-for-magento
Good luck!

How can I display a static block conditionally by way of product page template in Magento?

Here is the situation. We have a configurator extension that we would like to feature on the product page of only specific products. The extension is set up as a static block. My thought was that I could use an if statement in PHP using XML to define the template.
Here is my catalog XML:
<reference name="content">
<block type="partfinder/selector" name="partfinder_selector" template="partfinder/selector.phtml"/>
</reference>
This is my PHP in the view.php file:
<?php
if ($_product->getSku() = 10007)
{
echo $this->getChildHtml('partfinder_selector');
}
?>
What am I missing? Right now, this just destroys my product pages; all of them.
Is there a better way to do what I am trying to accomplish? I know it is bad form to use CMS blocks this way, however, that seems to be the nature of the extension.
You can use as follows :
<?php
if ($_product->getSku() == '10007') {
echo $this->getLayout()->createBlock('cms/block')->setBlockId('partfinder_selector')->toHtml();
}
?>
you also missed == in your if statement.

Magento: Using different footers for different page layouts

I've tried various tutorials but I can't get this to work.
Basically, I want to have a different footer on my homepage. I've setup two page layouts and have applied them to the cms pages fine.
So in the homepage layout I refer to...
<?php echo $this->getChildHtml('footer_home') ?>
And on all the other pages this...
<?php echo $this->getChildHtml('footer_alt') ?>
Pretty simple!
Then in the page xml I've amended the part which refers to the footer as follows...
<block type="page/html_footer" name="footer_alt" as="footer_alt" template="page/html/footer_alt.phtml">
<block type="page/html_wrapper" name="bottom.container" as="bottomContainer" translate="label">
<label>Page Footer</label>
<action method="setElementClass"><value>bottom-container</value></action>
</block>
<block type="page/switch" name="store_switcher" as="store_switcher" template="page/switch/stores.phtml"/>
<block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links.phtml"/>
</block>
<block type="page/html_footer" name="footer_home" as="footer_home" template="page/html/footer_home.phtml">
<block type="page/html_wrapper" name="bottom.container" as="bottomContainer" translate="label">
<label>Page Footer2</label>
<action method="setElementClass"><value>bottom-container</value></action>
</block>
<block type="page/switch" name="store_switcher" as="store_switcher" template="page/switch/stores.phtml"/>
<block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links.phtml"/>
</block>
I think this is where the problem lies. With the above all pages show the 'footer_alt' footer and I'm not sure why.
I can confirm that 'page/html/footer_alt.phtml' and 'page/html/footer_home.phtml' are setup fine.
I hope that makes sense. Thanks.
All of these answers are way too complex for what is a straightforward solution. Rewriting is overkill. Rewriting Magento core, even if done properly, should always set off alarms and immediately compel that developer to thoroughly read the Magento source. In my experience, every Magento heartache is accompanied by a cryptic but entirely gratifying solution. This is one of those heartaches with a gratifying solution.
It comes as no surprise that Magento arbitrarily decided to ensure that the footer templates do not generate a unique cache key. This means that the footer cannot be different depending on the section of the site loaded; to be clear, it actually can, but only if the block cache is disabled. The block cache should never be disabled, though, so ultimately, this is tantamount to being restricted to a single footer across the entire site.
There are legitimate use cases for wanting a different footer in different parts of the site. For example, in the checkout: the checkout should be immersive and distraction-free. However, the moment any page on the site is hit, the footer from those pages will be cached, and then the checkout will display that same footer.
Solutions described here require either a core rewrite, which is no good, or some other conditional check, which will not reasonably scale beyond a few conditions.
My solution is simple: add a cacheKey to the new template. Target the layout handle for the given page, reference the footer, set the template, and then add the cacheKey. This is just standard Magento layout XML. This layout XML changes the footer on the onepage checkout--and only on the onepage checkout. Furthermore, caching will continue to work for every unique footer defined in this way.
<checkout_onepage_index>
<reference name="footer">
<action method="setTemplate">
<template>linusmoneymaker/page/html/checkout-footer.phtml</template>
</action>
<action method="setCacheKey">
<key>your-own-unique-cache-key-for-linus-moneymaker</key>
</action>
</reference>
</checkout_onepage_index>
This works for the following reason. Here is the source for app/code/core/Mage/Core/Block/Abstract.php, which handles all the block caching:
/**
* Get Key for caching block content
*
* #return string
*/
public function getCacheKey()
{
if ($this->hasData('cache_key')) {
return $this->getData('cache_key');
}
/**
* don't prevent recalculation by saving generated cache key
* because of ability to render single block instance with different data
*/
$key = $this->getCacheKeyInfo();
//ksort($key); // ignore order
$key = array_values($key); // ignore array keys
$key = implode('|', $key);
$key = sha1($key);
return $key;
}
Notice that if a cacheKey is defined, that one will take precedence over the one that will be generated from the getCacheKeyInfo method in app/code/core/Mage/Page/Block/Html/Footer.php, which does not produce a per-template unique cacheKey. By providing a cacheKey from the layout XML, Magento effectively abandons the default, non-unique footer cacheKey in favour of the one provided manually through the layout XML for the given part of the site.
Not only is this the proper way, but it scales infinitely. Every page on the site could realistically define its own footer.
The reason for you problem should be magento's block cache.
Like the header the footer is cached by default and
the cache key does not include the template
to verify its a cache problem try this first:
Check if the block cache is enabled.
Then navigate to your page. The footer on the first page should be on any of the following.
So if your first page view is your alt footer it will be on any other page and vise versa.
if the problem is the cache you should be able to solve this
by rewriting the "Mage_Page_Block_Html_Footer" and
modify getCacheKeyInfo() to include the template like this
public function getCacheKeyInfo()
{
return array(
'PAGE_FOOTER',
Mage::app()->getStore()->getId(),
(int)Mage::app()->getStore()->isCurrentlySecure(),
Mage::getDesign()->getPackageName(),
Mage::getDesign()->getTheme('template'),
Mage::getSingleton('customer/session')->isLoggedIn(),
$this->getTemplate()
);
}
this should solve your problem.
Create 2 static Blocks named "home_page_footer" and "inner_page_footer".
Open footer.phtml and place the following code.
<?php $home = Mage::getSingleton('cms/page')->getIdentifier(); ?>
<?php if ($home):?>
<div style="clear:both;">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('home_page_footer')->toHtml(); ?>
</div>
<?php else: ?>
<div style="clear:both;">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('inner_page_footer')->toHtml(); ?>
</div>
<?php endif; ?>
Hope it will help.

Magento Contact form in phtml file of custom created widget

I'm developing a widget for Magento, in this widget i need to load the magento contact form. I've tried several options, but none of them seem to work.
the widget is located in
app/
code/
local/
CompanyName/
WidgetName/
and the phtml files are located in
app/
design/
frontend/
default/
default/
template/
templatename/
templatefile1.pthml
templatefile2.pthml
templatefile3.phtml
to load the magento contact form i've added this in templatefile3.phtml
<?php echo $this->getChildHtml('contactForm') ?>
but it's not showing up, even after adding xml to 2 files, i've added this line into app/design/frontend/base/default/layout/catalog.xml, right below <reference name="content">
<block type="core/template" name="contactForm" form_action="/contacts/index/post" template="contacts/form.phtml"/>
and also added this code to app/design/frontend/base/default/layout/default.xml (this file didn't even exist)
<default>
<cms_page>
<reference name="content">
<block type="core/template" name="contactForm" as="contactForm" template="contacts/form.phtml">
<action method='setBlockId'><block_id>contactForm</block_id></action>
</block>
</reference>
</cms_page>
I have the idea i'm not putting the xml in the right files, but i have no idea witch files to use otherwise, and can't find any other tips than these two on the internet.
Can someone help me out?
You can add the contact form to any CMS page using CMS tags like this inside the CMS content box:
{{block type="core/template" name="contactForm" template="contacts/form.phtml"}}
You could create a new page, and then inssert that snippet to show the contact form on this new page.
you could also make a copy of form.phtml and modify it for your needs, and update the tag code to use the new template.
Please use the following function to display inside templates.
<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Contact::form.phtml")->toHtml(); ?>
You can't just create a default.xml - to let Magento know that that a layout xml file exists you'd have to go through and create a module and specify the layout file's location. that is a very long winded way round of trying to achieve what you need.
Also I believe the handle <cms_page> is not supported. Referencing a block in a CMS page is best done by placing your xml in the layout update section under the 'design' tab for that CMS page.
So it's fairly straight forward to put the contact form into a CMS page - again use the layout update facility. Simply enter the following xml...
<reference name="content">
<block type="core/template" name="contactForm" template="contacts/form.phtml"/>
</reference>
Note you don't need a layout handle - Magento already knows of course the specific CMS page you are inserting content into because this is a layout update for that page. So in the above you are just referencing the structural block you want to add to (in this case content) and telling Magento to add the contactForm block along with its associated template location.
I've worked on a site using this method before. you will likely find that once you have called the contactform into your CMS page, and despite appearing successfully in a browser, the form doesn't actually post. In fact the form will only send if used from the intended page yoursite.com/contacts
Simple fix. Create the file app/design/frontend/YOUR-PACKAGE/YOUR-THEME/template/contacts/form.phtml and duplicate the required content from the default form.phtml into it.
Change the script at the bottom of the form.phtml file from:
<script type="text/javascript">
var contactForm = new VarienForm('contactForm', true);
</script>
To:
<script type="text/javascript">
elem = $("contactForm");
elem.writeAttribute('action', '/contacts/index/post');
</script>
And your CMS page contact form will now post as required.
Just as an addition (you may or may not find this useful).
I had a need for the contact form on my CMS page to be different from the standard form which appears on contacts. This is really simple too using the method above.
Duplicate the file app/design/frontend/YOUR-PACKAGE/YOUR-THEME/template/contacts/form.phtml and rename as something like app/design/frontend/YOUR-PACKAGE/YOUR-THEME/template/contacts/customForm.phtml - then make your required amends like different fields etc...
now in the layout update for the CMS page you want it to appear in simply add the following xml:
<reference name="content">
<block type="core/template" name="customContactForm" template="contacts/customForm.phtml"/>
</reference>
And that's it.
Note how the block name has changed - the block name can literally be anything - Magento doesn't care what you call it but I would obviously recommend something logical!

Categories