How to display error or success message in a template file? - magento

I want to show message on some condition in a template file(custom module template file).I am having following code.
<?php
if(count($collection)): ?>
<?php foreach($collection as $coll): ?>
some calculations
<?php endforeach; ?>
<?php else: ?>
<?php $message = $this->__('There is no data available'); ?>
<?php echo Mage::getSingleton('core/session')->addNotice($message);?>
<?php endif;?>
But this is not working properly. The message is displayed on other pages not on the same page.

If you really need to implement that right in the template, you may use the code below:
<?php echo $this->getLayout()->createBlock('core/messages')->addNotice('My Message')->toHtml(); ?>
But the solution described by Amit Bera sounds like a better way to resolve it.

Muk,According to your code
Mage::getSingleton('core/session')->addNotice($message);
add an notice to magento.This is code set notice to session and which is reflecting on page refresh according php session ,a session variable set value is reflecting after page refresh.
If you already added this notice on other page before came to your file then ,you need to add core/session to custom module template file controllers file.
Code is $this->_initLayoutMessages('core/session');
In controller you need below code in controller.
/* load the layout from xml */
$this->loadLayout();
$this->_initLayoutMessages('core/session');
/* rendering layout */
$this->renderLayout();
Read more at inchoo blog

I found that $this->loadLayout() is what reads and clears messages from the session, therefore if you add messages before calling $this->loadLayout() then those messages should be displayed on the current page.
Example:
public function chooseFileAction() {
// a quick and dirty way to find the larger out of post_max_size and upload_max_filesize
$post_max_size = ini_get('post_max_size'); $post_max_size_int = str_replace('K', '000', str_replace('M', '000000', str_replace('G', '000000000', $post_max_size)));
$upload_max_filesize = ini_get('upload_max_filesize'); $upload_max_filesize_int = str_replace('K', '000', str_replace('M', '000000', str_replace('G', '000000000', $upload_max_filesize)));
$maxsize = $post_max_size_int < $upload_max_filesize_int ? $post_max_size : $upload_max_filesize;
// display max file size to user in a message box
$msg = 'Max file size: ' . $maxsize;
Mage::getSingleton('core/session')->addNotice($msg);
$this->loadLayout();
$this->_title($this->__('Catalog'))->_title($this->__('File Upload'));
$this->_setActiveMenu('catalog/customfileupload');
$block = $this->getLayout()->createBlock('customfileupload/adminhtml_customfileupload_choosefile');
$this->_addContent($block);
$this->renderLayout();
}
Caveat: this is tested in Magento 1.9, not 1.7.

Related

Magento Active Filters on Search Page

I want to implement active filters on my magento ecommerce site.
I have been successful in implementing it, but the issue is, the code works on only category pages and not search page
Here's the code that I'm using
<?php /*Create filter dependencies*/
$_activeFilters[] = array();
$_filters = Mage::getSingleton(‘Mage_Catalog_Block_Layer_State’)->getActiveFilters();
foreach ($_filters as $_filter):?>
<?php echo $this->stripTags($_filter->getLabel()) ?><a href=”<?php echo $_filter- >getRemoveUrl() ?>” title=”<?php echo $this->__(‘Remove This Item’) ?>”><?php echo $this->__(‘Remove This Item’) ?></a>
<?php endforeach; ?>
I'm using this code in toolbar.phtml. Any clue as in why its not working on search page. Any Solutions would be of great help.
Thanks,
Sushil
You can use this code for fetching filters on either category list page or search results page
<?php
if(Mage::registry('current_category')) {
$_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
} else {
$_filters = Mage::getSingleton('catalogsearch/layer')->getState()->getFilters();
}
?>
I have used this code in toolbar.phtml, to show removable filters below the toolbar, like flipkart does.
The problem is with this line:
$_filters = Mage::getSingleton(‘Mage_Catalog_Block_Layer_State’)->getActiveFilters();
This gets a singleton which only contains the necessary data when on a category page.
See this question for more details: Magento - How to add Layered Navigation to Advanced Search?

Show Product on Search Page - Magento 1.7

Currently I have a snippet of code that will forward a user to the product page if they search for a term and only 1 product is associated with that keyword.
<?php if($this->getResultCount() == 1): ?>
<?php $prodId = $this->_productCollection->getAllIds() ?>
<?php $singleProduct = Mage::getModel('catalog/product')->load($prodId) ?>
<?php header('Location: ' . $singleProduct->getProductUrl()) ?>
<?php exit; ?>
<?php elseif($this->getResultCount()): ?>
However, what I want to do now is actually serve up the product and all its details on the results page itself if its the only one with that tag/search term INSTEAD of redirecting to the product page. Im pretty new to php so please bear with me.
Block template is bad place for this. Good place - controller.
Maybe you need rewrite controller for this functionality.
For example/app/code/core/Mage/CatalogSearch/controllers/ResultController.php
In controller your code looks like:
$this->getResponse()->setRedirect($_product->getProductUrl());

magento custom payment method form not showing

I wrote a custom payment module. It seems to be working fine but I can't show the method form in the checkout process. I mean: I see my method, I can select it, but can't see the form.
I want to show a select box with a couple of options but no form is rendered.
app\code\local\Neurona\Sistarbanc\Block\Form\sistarbanc.php
class Neurona_Sistarbanc_Block_Form_Sistarbanc extends Mage_Payment_Block_Form
{
protected function _construct()
{
parent::_construct();
$this->setTemplate('payment/form/sistarbanc.phtml');
}
....
And the form in app\design\frontend\base\default\template\payment\form\sistarbanc.phtml
Any help will be appreciated!!
EDIT
So, it seems to be working in the default ckeckout process but not in the onestepcheckout
EDIT 2
I found that in onestepheckout this is the way the payment method form is rendered:
<?php if ($html = $this->getPaymentMethodFormHtml($_method)): ?>
<dd id="container_payment_method_<?php echo $_code; ?>" class="payment-method" <?php if(!$this->getSelectedMethodCode()==$_code || !($hide_nonfree_methods && $_code == 'free')): ?> style="display:none"<?php endif; ?>>
<?php echo $html; ?>
</dd>
So, for my method $this->getPaymentMethodFormHtml($_method) is FALSE. ¿why?
Your template filename is Sisterbanc.phtml, but your block will try to load sisterbanc.phtml (uppercase "s" vs lowercase "s").
Maybe you need add this line in Model/Payment.php.
protected $_formBlockType = "sistarbanc/form/sistarbanc"
I don't know the reason why though. I compared with other payment module code, and got this.

What function and where is $this->getPriceHtml() located?

On the default product view page for magento where is "getPriceHtml" function located or what is being called here:
<?php echo $this->getPriceHtml($_product) ?>
Several words are being displayed by this code such as "Price From:" with the price included afterwards. This is for a configurable product.
Mage_Catalog_Block_Product::getPriceHtml()
This method renders via app/design/frontend/base/default/template/catalog/product/price.phtml
a.k.a The Worst Template In Magento®
benmark's answer comes down to this:
<?php echo Mage_Catalog_Block_Product::getPriceHtml($_product, true) ?>
Where $_product relates to the product object.
$productBlock = new Mage_Catalog_Block_Product();
$priceBlock = $productBlock->getPriceHtml($_product, true);
echo $priceBlock;

magento accesing "SendFriend" module from category page

I'm trying to add the "send to a friend" action to a category page.
In the product view i can see this code:
"canEmailToFriend()): ?>
<p class="email-friend"><?php echo $this->__('Email to a Friend') ?></p>
<?php endif; ?>
If I try to add this code to my "list.phtml" (where products grid is displayed) I receive this error:
Invalid method Mage_Catalog_Block_Product_List::canEmailToFriend(Array
saying that this methos is not available in this context...
Who can I add the funcionality of "sendtofriend" module to any page I need?
Thanks in advance
This is because the $this->canEmailToFriend() call is a block method belonging to the product page, a class called Mage_Catalog_Block_Product_View. The product listing page uses a block class called Mage_Catalog_Block_Product_List which does not include this code.
The method canEmailToFriend() contains (as defined in app/code/core/Mage/Catalog/Block/Product/View.php) the logic:
$sendToFriendModel = Mage::registry('send_to_friend_model');
return $sendToFriendModel && $sendToFriendModel->canEmailToFriend();
You could embed that directly in your template and then call the helper to output the link if $sendToFriendModel->canEmailToFriend(), but the best way to achieve this would be to move the canEmailToFriend functionality out into a new helper and call it from there.
I founded an alternative solution, just load sendfriend model
$sendToFriendModel = Mage::getModel('sendfriend/sendfriend');
Then use
<?php if ( $sendToFriendModel->canEmailToFriend() ) : ?>
<p class="email-friend"><span><span><?php echo $this->__('Email to a Friend') ?></span></span></p>
<?php endif;?>
Resource from C:/xampp/htdocs/magento/app/code/core/Mage/Sendfriend/controllers/ProductController.php
I think Mage::registry('send_to_friend_model') returns an object of the class Mage_Sendfriend_Model_Sendfriend. The canEmailToFriend() method in Mage_Sendfriend_Model_Sendfriend checks whether the "email to a friend" functionality is enabled:
You can find these two methods in app/code/core/Mage/Sendfriend/Model/Sendfriend.php:
/**
* Check if user is allowed to email product to a friend
*
* #return boolean
*/
public function canEmailToFriend()
{
return $this->_getHelper()->isEnabled();
}
/**
* Retrieve Data Helper
*
* #return Mage_Sendfriend_Helper_Data
*/
protected function _getHelper()
{
return Mage::helper('sendfriend');
}
So you could check that yourself in your template like so:
<?php if (Mage::helper('sendfriend')->isEnabled()) : ?>
<?php // Do stuff ?>
<?php endif ?>

Resources