Adding a dynamic unsubscribe button to a static block - magento

First of all this concerns magento.
I want to have a newsletter with a standard header and footer. This header and footer are being built through static blocks.
How can i add a dynamic unsubscribe option in the static footer block? (an unsubscribe per customer)
And should this not be possible, how can I do it through other means?
Any help would be most welcome.

First create a static block and call phtml from static block
The code of .phtml file
call in a phtml file in static block
{{block type="core/template" template="newsletter/unsci.phtml"
name="newsletterall" }}
Create a phtml in unsci.phtml under app/design/frontend/yourpackage/youtemplate/newsletter
Call a static block like the code is below-
If you want only to unsubscribe then you should add below code in phtml file
<?php $subscriber = Mage::getModel('newsletter/subscriber')
->loadByEmail("dev.amit.bera#gmail.com")
if( $subscriber->getId()){
$unscibeurl=Mage::helper('newsletter')->getUnsubscribeUrl($subscriber); ?>
Un subcribe
<?php } ?>
Call static block in anywhere using below code
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($indenfirename)->toHtml(); ?>

Related

Magento get stock quantity at related products, upsells, crosssells in frontend

I need to get the stock quantity for each product in the section where "Related products" is showed in frontend. Using Magento 1.9.
This function will not help me show the actual qty in my related products section:
Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();
The function shows stock qty in the product view page and the catalog collection but does not work for related products.
What to do?
I am not sure if you are actually calling the above piece of code inside a PHTML file.
Assuming you are using the RWD theme and calling it inside a PHTML directly (which is not as per Magento Standards), the file location would be app/design/frontend/rwd/default/template/catalog/product/list/related.phtml
Inside, the FOREACH loop, call the below piece of code (screenshot attached for reference - frontend display):
<?php echo Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getQty(); ?>
The ideal solution would be overriding the CORE file:
app/code/core/Mage/CatalogInventory/Model/Stock/Item.php
like below:
app/code/local/Namespace/Modulename/Model/Stock/Item.php
in order to add a new function:
<?php
class Namespace_Modulename_Model_CatalogInventory_Stock_Item extends Mage_CatalogInventory_Model_Stock_Item
{
public function getItemStockQty($product)
{
return $this->loadByProduct($product)->getQty();
}
}
And inside the PHTML file, under the FOREACH LOOP call this function as below:
<?php echo $this->getItemStockQty($_item); ?>
Screenshot
Hope this helps.
Happy Coding...

How to render content of .phtml in controller and observer in magento

I have an observer "checkout_cart_add_product_complete"
I want to render the content of cart.phtml in observer and controller which is located in /template/ajaxminicart/checkout/cart.phtml
How can i do this?
Check following,
<?php
$newBlock =Mage::app()->getLayout()
->createBlock('checkout/cart')
->setTemplate('ajaxminicart/checkout/cart.phtml')
-->toHtml();
echo $newBlock;
?>

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 layout: load only a single block

Is there a way in magento to create a block and call its toHtml() method without having to load the entire layout object.
For example, in one of my ajax controllers i want to send a certain block as json output and i am not interested in any other blocks but i am forced to do the following:
$this->loadLayout();
$this->getLayout()->getBlock('my_block_name')->toHtml();
Which loads the entire layout which seems unnecessary.
I just accomplished this like so:
In my layout XML for my module:
<mymodule_ajax_action>
<block type="core/template_facade" name="root" template="path/to/template/file.phtml"/>
</mymodule_ajax_action>
By naming the block "root", it replaced the entire layout with just this template file.
So in my controller:
public function actionAction() {
$this->loadLayout();
$this->renderLayout();
}
returns just that block.
You can try the following:
$layout = Mage::getSingleton('core/layout');
$html = $layout
->createBlock('module/block_type')
->setTemplate('template/file.phtml')
->toHtml();
From your code, it looks as though you are in a controller so you could shorten the code a little to the following (absolutely no difference in functionality, simply 1 less line of code)...
$html = $this->getLayout()
->createBlock('module/block_type')
->setTemplate('template/file.phtml')
->toHtml();

"getLogoAlt" not available for content section in Magento

In Magento CE 1.5.1, why is getLogoAlt not available for the content section though it is available for the header section?
Inside the content section of the home page, my theme's home.phtml has
<h1 class="no-display"><?php echo $this->getLogoAlt() ?></h1>
which outputs as
<h1 class="no-display"></h1>
But header.phtml,
<h4><strong><?php echo $this->getLogoAlt() ?></strong></h4>
properly outputs as
<h4><strong>Buy widgets here!</strong></h4>
Puzzled...
The "header section" is a block added with the following layout update XML
<block type="page/html_head" name="head" as="head">
The block's type is page/html_head, which translated to the class Mage_Page_Block_Html_Header. If you look at the class definition, you can see the header.phtml template being set.
#File: app/code/core/Mage/Page/Block/Html/Header.php
public function _construct()
{
$this->setTemplate('page/html/header.phtml');
}
When you use $this->someMethod() from a template, you're calling a method on the template's block class. Each template "belongs" to a block. If we look at the header class again
#File: app/code/core/Mage/Page/Block/Html/Header.php
public function getLogoAlt()
{
if (empty($this->_data['logo_alt'])) {
$this->_data['logo_alt'] = Mage::getStoreConfig('design/header/logo_alt');
}
return $this->_data['logo_alt'];
}
we can see the definition of getLogoAlt.
The other template you mentioned, home.phtml, is added with the following layout update xml
<block type="core/template" name="default_home_page" template="cms/default/home.phtml"/>
Its block is a core/template block, which translates to Mage_Core_Block_Template. This block does not have a getLogoAlt method. However, like all blocks, it does have Magento's magic getters and setters. You can "set" and "get" data properties on Magento blocks like this
$this->setFooBar('setting a value for the foo_bar data property');
echo $this->getFooBar();
even if the block doesn't have those methods defined. So, this means you can call getLogoAlt on any block without an error being thrown, but only the header block is going to return a value. If you want that value in any template, you can just call
$logo_alt = Mage::getStoreConfig('design/header/logo_alt');
at the top of your template, and then use $logo_alt wherever you'd like.

Resources