magento accesing "SendFriend" module from category page - magento

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 ?>

Related

Magento Info Block in Payment Method

does anyone know how do I include a block information when selecting a payment method in my checkout page? within that block I want to insert an html(image) from the payment gateway that I'm using.
I've tried to do this using jquery inserting the html needed when the page loads, the image appears quickly but some update in the layout of the checkout page removes the image, ie, it appears and disappears soon after.
I am very new to magento and any help would be welcome, thanks
I´m using magento 1.8.1
I assume that you are referring to the right blocks which output the checkout progress ( see below if you are referring to the actual payment method selection step ).
You don't need to do any client side scripting. Magento supports that by default.
Checkout progress ( right blocks on checkout page ):
You need to follow these steps:
Open your Payment Method model.
Add the following code:
protected $_infoBlockType = 'mymodule/info';
The block code should look like:
<?php
class Company_MyModule_Block_Info extends Mage_Payment_Block_Info_Cc
{
protected function _prepareSpecificInformation($transport = null)
{
$info = $this->getInfo();
$transport = new Varien_Object(array('My Var' => "My Value"));
return $transport;
}
}
Payment method selection step:
If you are referring to the actual payment method selection step, when you click on a payment method and a small form appears under it you need to:
Add the following code in your payment model:
protected $_formBlockType = "mymodule/form";
You also need to create the mymodule/form:
<?php
class Company_MyModule_Block_Form extends Mage_Payment_Block_Form_Cc
{
public function __construct()
{
parent::__construct();
$this->setTemplate('path/to/template.phtml');
}
}
The template file should look something like:
<?php $_code=$this->getMethodCode(); ?>
<fieldset class="form-list">
<ul style="display: none;" id="payment_form_<?php echo $_code ?>">
<li>
<p>here is my custom content!:)</p>
</li>
</ul>
</fieldset>

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

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.

Magento, show image on custom frontend block

I've a module up and running. On the backend side I can upload images with several attributes and I'm saving image path and other information on a custom table. On frontend I already managed to detect when I need to show these images. I've used an Observer that adds a new layout handle when the displayed product is found on the custom created table. Then on that layout handle I call a phtml template file and from here I call a function inside a block that will be in charge of doing all the checks to be sure wich image to show.
My problem is that I can not find how to show these images. Everything I found references how to add image tag on a phtml file doing some extra verifications on the inserted php code. Buy on my case everything is on php code outside any phtml file.
My phtml file code:
<div>
<h3><?php $this->showCampaign(); ?></h3>
</div>
My block code for now:
<?php
class Dts_Banners_Block_Front extends Mage_Core_Block_Template {
/**
* Shows a campaign banner according to the current selected product
* Seeks on main banners table to see if it is an image/html/product alone campaign
* Once knows the campaign type search on needed table the needed fields
*/
public function showCampaign() {
//Zend_Debug::dump($this->getLayout()->getUpdate()->getHandles());
$product = Mage::registry('current_product');
$currentProdID = $product->getId();
if (Mage::registry('campaign_data') && Mage::registry('campaign_data')->getId()) {
$currentCampaign = Mage::registry('campaign_data');
} else {
return;
}
// get campaign type and show needed information
switch ($currentCampaign->getbanner_type()) {
case 1: // image
$myImgCollection = Mage::getModel('banners/bannersimg')->getCollection();
$myImgCollection->addfieldtofilter('bannerid',$currentCampaign->getID());
$currentImg = $myImgCollection->getFirstItem();
$currentImgPath = $currentImg->getimg_path();
//{{block type="catalog/product_new" product_id="16" template="catalog/product/view/your_new_page.phtml"}}
break;
case 2: // html
echo "html";
break;
case 3: // plain product url
echo "plain product url";
break;
}
}
}
As usual, couple of hours searching for an answer and when I post the question, found the answer. Just in case someone needs it: is as easy as creating an image html tag inside the code and return it to the template file. Sample code:
<?php
class Dts_Banners_Block_Front extends Mage_Core_Block_Template {
public function showCampaign() {
...
$currentImgPath = $currentImg->getimg_path();
//build html to output to the template
$html .= "<div class=\"visual\">";
$html .= "<img src=\"" . $currentImgPath . "\" alt=\"\" />";
$html .= "</div>";
....
return $html;
}
And the phtml file code:
<div class="visual">
<?php echo $this->showCampaign(); ?>
</div>

Retrieve product custom media image label in magento

I have a custom block loading products on my front page that loads the four newest products that have a custom product picture attribute set via:
$_helper = $this->helper('catalog/output');
$_productCollection = Mage::getModel("catalog/product")->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToFilter("image_feature_front_right", array("notnull" => 1));
$_productCollection->addAttributeToFilter("image_feature_front_right", array("neq" => 'no_selection'));
$_productCollection->addAttributeToSort('updated_at', 'DESC');
$_productCollection->setPageSize(4);
What I am trying to do is grab the image_feature_front_right label as set in the back-end, but have been unable to do so. Here is my code for displaying the products on the front end:
<?php foreach($_productCollection as $_product) : ?>
<div class="fll frontSale">
<div class="productImageWrap">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'image_feature_front_right')->directResize(230,315,4) ?>" />
</div>
<div class="salesItemInfo">
<p class="caps"><?php echo $this->htmlEscape($_product->getName());?></p>
<p class="nocaps"><?php echo $this->getImageLabel($_product, 'image_feature_front_right') ?></p>
</div>
</div>
I read that $this->getImageLabel($_product, 'image_feature_front_right') was the way to do it, but produces nothing. What am I doing wrong?
Thanks!
Tre
It seems you asked this same question in another thread, so to help others who might be searching for an answer, I'll anser it here as well:
I imagine this is some sort of magento bug. The issue seems to be that the Magento core is not setting the custom_image_label attribute. Whereas for the default built-in images [image, small_image, thumbnail_image] it does set these attributes - so you could do something like:
$_product->getData('small_image_label');
If you look at Mage_Catalog_Block_Product_Abstract::getImageLabel() it just appends '_label' to the $mediaAttributeCode that you pass in as the 2nd param and calls $_product->getData().
If you call $_product->getData('media_gallery'); you'll see the custom image label is available. It's just nested in an array. So use this function:
function getImageLabel($_product, $key) {
$gallery = $_product->getData('media_gallery');
$file = $_product->getData($key);
if ($file && $gallery && array_key_exists('images', $gallery)) {
foreach ($gallery['images'] as $image) {
if ($image['file'] == $file)
return $image['label'];
}
}
return '';
}
It'd be prudent to extend the Magento core code (Ideally Mage_Catalog_Block_Product_Abstract, but I don't think Magento lets you override Abstract classes), but if you need a quick hack - just stick this function in your phtml file then call:
<?php echo getImageLabel($_product, 'image_feature_front_right')?>
Your custom block would need to inherit from Mage_Catalog_Block_Product_Abstract to give access to that method.
You could also use the code directly from the method in the template:
$label = $_product->getData('image_feature_front_right');
if (empty($label)) {
$label = $_product->getName();
}

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.

Resources