Displaying block using custom module - magento

I've created a custom module that handles various tasks for our membership system. I'm now trying to modify the module so that I'm able to drop in a block wherever I'd like. I've been following the tutorial on Gravitywell.com but I have a feeling that I've dropped off somewhere.
app/etc/modules/Hatclub_MembershipHandler.xml
<config>
<modules>
<Hatclub_MembershipHandler>
<active>true</active>
<codePool>local</codePool>
</Hatclub_MembershipHandler>
</modules>
app/code/local/Hatclub/MembershipHandler/Block/lookup.php
<?php
class Hatclub_MembershipHandler_Block_Lookup extends Mage_Core_Block_Template {
}
app/code/local/Hatclub/MembershipHandler/etc/config.xml
<modules>
<Hatclub_MembershipHandler>
<version>1.0.0</version>
</Hatclub_MembershipHandler>
</modules>
<global>
<models>
<hatclub_membership_handler>
<class>Hatclub_MembershipHandler_Model</class>
</hatclub_membership_handler>
</models>
<blocks>
<hatclub_membership_handler>
<class>Hatclub_MembershipHandler_Block</class>
</hatclub_membership_handler>
</blocks>
<events>
<customer_register_success>
<observers>
<registration_success_observer>
<class>hatclub_membership_handler/observer</class>
<method>registrationSuccess</method>
<type>singleton</type>
</registration_success_observer>
</observers>
</customer_register_success>
</events>
</global>
app/code/local/Hatclub/MembershipHandler/Model/Observer.php
<?php
class Hatclub_MembershipHandler_Model_Observer {
// members group id
const GROUP_ID = 4;
// called when a customer registers for the site
public function registrationSuccess(Varien_Event_Observer $observer) {
// extract customer data from event
$customer = $observer->getCustomer();
// a cookie should have been set with the membership id
if (isset($_COOKIE['membership_account_id'])) {
$customer
->setGroupId(self::GROUP_ID)
->setRmsId($_COOKIE['membership_account_id']);
}
return $this;
}
}
/app/design/frontend/enterprise/hatclub/template/persistent/customer/form/register.phtml
<?php
echo $this->getLayout()->createBlock('hatclub_membership_handler/lookup')->setTemplate('membership_lookup/lookup.phtml')->toHtml(); ?>

The code under blocks node in file app/code/local/Hatclub/MembershipHandler/etc/config.xml should look like this
<blocks>
<membershiphandler>
<class>Hatclub_MembershipHandler_Block</class>
</membershiphandler>
</blocks>
membershiphandler represents lowercase version of your module name and
<?php
echo $this->getLayout()->createBlock('hatclub_membership_handler/lookup')->setTemplate('membership_lookup/lookup.phtml')->toHtml(); ?>
should be
<?php
echo $this->getLayout()->createBlock('membershiphandler/lookup')->setTemplate('path to the template file')->toHtml(); ?>

Related

Magento Add telephone no in order grid using event observer

I want add Telephone no of customers that is used in order -> shipping or billing. in order grid. I can add the title in grid but I don't know how get the phone no. The event that used for this is sales_order_grid_collection_load_before. Even I can't get order id using this.
Here is my code.
config.xml
<?xml version="1.0"?>
<config>
<modules>
<Elavarasan_OrderGrid>
<version>0.1.0</version>
</Elavarasan_OrderGrid>
</modules>
<global>
<helpers>
<ordergrid>
<class>Elavarasan_OrderGrid_Helper</class>
</ordergrid>
</helpers>
<models>
<ordergrid>
<class>Elavarasan_OrderGrid_Model</class>
<resourceModel>ordergrid_mysql4</resourceModel>
</ordergrid>
</models>
<events>
<core_block_abstract_to_html_before> <!-- identifier of the event we want to catch -->
<observers>
<core_block_abstract_to_html_before_handler> <!-- identifier of the event handler -->
<type>model</type> <!-- class method call type; valid are model, object and singleton -->
<class>ordergrid/observer</class> <!-- observers class alias -->
<method>getGrid</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</core_block_abstract_to_html_before_handler>
</observers>
</core_block_abstract_to_html_before>
<sales_order_grid_collection_load_before> <!-- identifier of the event we want to catch -->
<observers>
<sales_order_grid_collection_load_before_handler> <!-- identifier of the event handler -->
<type>model</type> <!-- class method call type; valid are model, object and singleton -->
<class>ordergrid/observer</class> <!-- observers class alias -->
<method>addPhoneNo</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</sales_order_grid_collection_load_before_handler>
</observers>
</sales_order_grid_collection_load_before>
</events>
</global>
</config>
Observer.php
<?php
class Elavarasan_OrderGrid_Model_Observer
{
public function getGrid(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if ($block->getId() == 'sales_order_grid') {
$block->addColumnAfter(
'telephone',
array(
'header' => Mage::helper('sales')->__('Telephone No'),
'align' => 'left',
'type' => 'options',
'options' => $paymentMethods,
'index' => 'telephone',
'filter_index' => 'telephone.telephone'
),
'shipping_name'
);
//$block->sortColumnsByOrder();
}
}
public function addPhoneNo(Varien_Event_Observer $observer)
{
echo $phone = $observer->getOrder()->getCustomer()->getPrimaryBillingAddress()->getTelephone();
}
}
Please help me guys..
Hope this link will help you.
http://www.ecomdev.org/2010/07/27/adding-order-attribute-to-orders-grid-in-magento-1-4-1.html

Call to a member function insert() on a non-object Magento

I am Using Observer Event in my custom module's Config.xml
<controller_action_layout_render_before>
<observers>
<Test_Check_Model_Observer>
<class>Test_Check_Helper_Data</class>
<method>checkValidi</method>
</Test_Check_Model_Observer>
</observers> </controller_action_layout_render_before>
Now in Test/Check/Helper/Data checkValidi Method I am inserting a block in Content.
>
class Test_Check_Helper_Data extends Mage_Core_Helper_Abstract {
> public function checkValidi($observer) {
> $layout = Mage::app()->getLayout();
> $content = $layout->getBlock('content');
> $block = 'hello! I am Working';
> $content->insert($block);
> }
But In Frontend When I am Filling Checkout billing and other information it gives me an Error Call to a member function insert() on a non-object in right side bar of Your Checkout Progress ,Please Give me any Solution for that Thanks
If you look at layout of checkout module checkout.xml you can see that some handles do not have content block. Eg, checkout_onepage_progress_billing or checkout_onepage_progress_shipping
So, you will get non-object error with your code. I think you should check $block variable before calling method.
$layout = Mage::app()->getLayout();
$content = $layout->getBlock('content');
if ($content) {
$block = 'hello! I am Working';
$content->insert($block);
}
It is not good idea to call helper from observer.
Observer always call a model file ,it did not class
i have modify and code config.xml code is
<global>
<models>
<testcheck>
<class>Test_Check_Model</class>
</testcheck>
</models>
</global>
<events>
<controller_action_layout_render_before>
<observers>
<test_check_codel_observer>
<type>singleton</type>
<class>testcheck/observer</class>
<method>your_function_name</method>
</test_check_codel_observer>
</observers>
</controller_action_layout_render_before>
</events>
And then Create Observer Observer.php file Under app/code/YourcodePoll/Test/Check/Model/
and below
code in Observer.php
is
<?php class Test_Check_Model_Observer
{
public function your_function_name($observer){
$block = $this->getLayout()
->createBlock('core/text', 'example-block')
->setText('<h1>This is a text block</h1>');
$observer->getEvent()->getLayout()->getBlock('content')->append($block);
//$observer->getEvent()->getLayout()->getUpdate();
}
}

Magento block output gives empty string

I am trying to troubleshoot an issue where the output of my block is an empty string.
I traced it down to the point where I could see PHP statements being evaluated in the template file, but inside toHtml() of class Mage_Core_Block_Abstract, $html = $this->_toHtml(); assigns empty string to $html.
I dig it further, and found that inside fetchView(), $html = ob_get_clean(); assigns empty string to it, even when the template was included above this line, and I could see it evaluating with the use of debugger.
From here I am clueless on how to debug this, may be I am missing something wrong in my module (I am a beginner in Magento).
Here is the concerned code from the module:
app/code/local/AnattaDesign/AbandonedCarts/etc/config.xml
<config>
<global>
<blocks>
<anattadesign_abandonedcarts>
<class>AnattaDesign_AbandonedCarts_Block</class>
</anattadesign_abandonedcarts>
</blocks>
</global>
<adminhtml>
<layout>
<updates>
<anattadesign_abandonedcarts>
<file>layout.xml</file>
</anattadesign_abandonedcarts>
</updates>
</layout>
</adminhtml>
app/design/adminhtml/base/default/layout/layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<adminhtml_dashboard_index>
<reference name="head">
<action method="addCss">
<stylesheet>anattadesign/abandonedcarts/css/style.css</stylesheet>
</action>
<action method="addJs">
<script>anattadesign/abandonedcarts/zepto.js</script>
</action>
<action method="addJs">
<script>anattadesign/abandonedcarts/adminhack.js</script>
</action>
</reference>
</adminhtml_dashboard_index>
</layout>
app/code/local/AnattaDesign/AbandonedCarts/controllers/WidgetController.php
<?php
class AnattaDesign_AbandonedCarts_WidgetController extends Mage_Adminhtml_Controller_Action {
public function indexAction() {
echo "index action of widget controller";
die();
}
public function renderAction() {
$html = $this->getLayout()->createBlock( 'anattadesign_abandonedcarts/widget', 'root' )->setTemplate( 'anattadesign/abandonedcarts/widget.phtml' )->toHtml();
$this->getResponse()->setBody( $html );
die();
}
}
app/code/local/AnattaDesign/AbandonedCarts/Block/Widget.php
<?php
class AnattaDesign_AbandonedCarts_Block_Widget extends Mage_Core_Block_Template {
}
I am running this by an admin controller and making the renderAction() fire.
Remove the die() in method renderAction()
class AnattaDesign_AbandonedCarts_WidgetController extends Mage_Adminhtml_Controller_Action {
....
public function renderAction() {
$html = $this->getLayout()->createBlock( 'anattadesign_abandonedcarts/widget')
->setTemplate( 'coming.phtml' )
->toHtml();
$this->getResponse()->setBody( $html );
}
}

Magento custom module grid not displaying

So I'm trying to get a grid to display in my custom module (displaying anything for the time being, I'll worry about the collection once it's working!).
The problem is that the _prepareCollection() and/or _prepareColumns() methods of my grid widget class never seem to get called and the grid never shows (nor do the buttons and header text). (The Magento admin header and footer and navigation display correctly. It's just blank in the middle!)
This is what I have so far:
app/code/local/MyNamespace/Mymodule/etc/config.xml
<?xml version="1.0" ?>
<config>
<modules>
<MyNamespace_Mymodule>
<version>0.0.1</version>
</MyNamespace_Mymodule>
</modules>
<!-- Define frontend and backend routers -->
<admin>
<routers>
<mymodule>
<use>admin</use>
<args>
<module>MyNamespace_Mymodule</module>
<frontName>mymodule</frontName>
</args>
</mymodule>
</routers>
</admin>
<!-- /Define frontend and backend routers -->
<global>
<helpers>
<mymodule>
<class>MyNamespace_Mymodule_Helper</class>
</mymodule>
</helpers>
<blocks>
<mymodule>
<class>MyNamespace_Mymodule_Block</class>
</mymodule>
</blocks>
</global>
<adminhtml>
<menu>
<mymodule module="mymodule">
<title>My Module</title>
<sort_order>80</sort_order>
<children>
<items module="mymodule">
<title>Manage My Module</title>
<sort_order>0</sort_order>
<action>mymodule/adminhtml_mymodule</action>
</items>
</children>
</mymodule>
</menu>
<!-- define layout updates -->
<layout>
<updates>
<mymodule>
<file>mymodule.xml</file>
</mymodule>
</updates>
</layout>
<!-- /define layout updates -->
</adminhtml>
</config>
Then my controller:
app/code/local/MyNamespace/Mymodule/controllers/Adminhtml/MymoduleController.php
<?php
class MyNamespace_Mymodule_Adminhtml_MymoduleController extends Mage_Adminhtml_Controller_action
{
public function indexAction() {
$this->getLayout()->createBlock('mymodule/adminhtml_mymodule');
$this->loadLayout();
$this->renderLayout();
}
}
Then in my grid container:
app/code/local/MyNamespace/Mymodule/Block/Adminhtml/Mymodule.php
<?php
class MyNamespace_Mymodule_Block_Adminhtml_Mymodule extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
echo __METHOD__ . " (Line #" . __LINE__ . ")<br/>";
parent::__construct();
$this->_controller = 'adminhtml_mymodule';
$this->_blockGroup = 'mymodule';
$this->_headerText = Mage::helper('mymodule')->__('my header text'); // this is not rendered
$this->_addButtonLabel = Mage::helper('mymodule')->__('my button text'); // this is not rendered
}
protected function _prepareLayout()
{
$this->setChild( 'grid',
$this->getLayout()->createBlock( $this->_blockGroup.'/' . $this->_controller . '_grid',
$this->_controller . '.grid')->setSaveParametersInSession(true) );
return parent::_prepareLayout();
}
}
Then in my grid widget:
app/code/local/MyNamespace/Mymodule/Block/Adminhtml/Mymodule/Grid.php
<?php
class MyNamespace_Mymodule_Block_Adminhtml_Mymodule_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('mymoduleGrid');
$this->setDefaultSort('id');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
echo __METHOD__ . " (Line #" . __LINE__ . ")<br/>"; // This is never called
$collection = Mage::getModel('catalog/product')->getCollection(); // just a temp collection for the time being
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
echo __METHOD__ . " (Line #" . __LINE__ . ")<br/>"; // This is never called
$this->addColumn('id', array(
'header' => Mage::helper('mymodule')->__('ID'),
'align' =>'right',
'width' => '10px',
'index' => 'id',
));
return parent::_prepareColumns();
}
}
And finally my layout xml:
app/design/adminhtml/default/default/layout/mymodule.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<adminhtml_mymodule_index>
<reference name="content">
<block type="mymodule/adminhtml_mymodule" name="mymodule" />
</reference>
</adminhtml_mymodule_index>
</layout>
There are no errors being shown in the logs and I'm now a bit stumpped and other SO answers don't seem to fit.
Anyone shed any light on why my grid (even an empty one) isn't showing?
Thank you.
EDIT Noticed that some classes had the wrong case (Mynamespace should be MyNamespace). Changed them but no difference
It is the problem of your layout's handle tag.
It should be:
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_adminhtml_mymodule_index>
<reference name="content">
<block type="mymodule/adminhtml_mymodule" name="mymodule" />
</reference>
</mymodule_adminhtml_mymodule_index>
</layout>
For the explanation and few tips, you can read this:
My layout isn't loading in my Magento admin view
= UPDATE =
You also need not to call $this->getLayout()->createBlock('mymodule/adminhtml_mymodule'); in your controller as is has been called in your mymodule.xml
OR
you can omit your mymodule.xml (no need to call it) by changing your controller's action into:
public function indexAction() {
$this->loadLayout();
$myblock = $this->getLayout()->createBlock('mymodule/adminhtml_mymodule');
$this->_addContent($myblock);
$this->renderLayout();
}
see the definition:
Mage_Adminhtml_Controller_Action
protected function _addContent(Mage_Core_Block_Abstract $block)
{
$this->getLayout()->getBlock('content')->append($block);
return $this;
}
those codes above do the same thing like the mymodule.xml, appending block 'mymodule/adminhtml_mymodule' to the content
It's all your choice!
Can you please make sure your index action is being called properly?
when you do this :
<?php
class Mynamespace_Mymodule_Adminhtml_MymoduleController extends Mage_Adminhtml_Controller_action
{
public function indexAction() {
echo "im here";exit; //<----does this display?
$this->getLayout()->createBlock('mymodule/adminhtml_mymodule');
$this->loadLayout();
$this->renderLayout();
}
}
and then finally its important to load your layout first. so in my opnion change your index action to this :
<?php
class Mynamespace_Mymodule_Adminhtml_MymoduleController extends Mage_Adminhtml_Controller_action
{
public function indexAction() {
$this->loadLayout(); // <---- This first
$this->getLayout()->createBlock('mymodule/adminhtml_mymodule');// <---- then this
$this->renderLayout();
}
}
I know this isn't strictly related to the answer, but hopefully this might help someone.
Just in case anyone is reading this, completely bereft of hope - make sure there's a layout file in app/design/adminhtml/default/default/layout.
I had no problems with my custom grid locally, but when I migrated it, it was displaying the symptoms above (blank screen etc)
It was due to me not copying that file over.

Magento Sales > Orders > Add print action

I'm trying to create a print action on the magento orders page(printscreen: sbx.mujjo.com/media/images/action.png). Right now 'Print Labels' is a url to a html page. I'm trying to get it to open a .pdf instead (just like 'Print invoice'. But I can't find the right code.
The code that create the url:
class AquiveMedia_Orderlabel_Model_Observer {
public function add_action($observer) {
$block = $observer->getEvent()->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Widget_Grid_Massaction) {
if ($block->getParentBlock() instanceof Mage_Adminhtml_Block_Sales_Order_Grid) {
$block->addItem('print_labels', array(
'label' => Mage::helper('sales')->__('Print Labels'),
'url' => $block->getUrl('orderlabel/adminhtml_orderlabel/massprint')
)
);
}
}
}
}
Since the link already exists just use the URL for yourself. Here is how to override.
In your module's config.xml file,
<config>
...
<admin>
<routers>
<adminhtml>
<args>
<modules>
<aquivemedia_orderlabel before="Mage_Adminhtml">
AquiveMedia_Orderlabel_Adminhtml
</aquivemedia_orderlabel>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>
Now you can make a controller and it will be called first.
app/code/local/AquiveMedia/Orderlabel/controllers/Adminhtml/Sales/Order/InvoiceController.php:
class AquiveMedia_Orderlabel_Adminhtml_Sales_Order_InvoiceController
extends Mage_Adminhtml_Controller_Action
{
public function printAction()
{
// this is called instead of the path "index.php/admin/sales_order_invoice/print"
}
}

Resources