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
Related
I've been really beating myself up over this up, hopefully someone can help me out.
I have a few observer events working perfectly, it's just one that doesn't seem to work.
The creditmemoRefund function won't add a comment to the order. The order 2 work fine, it's definitely firing like intended, and does pull the order object. Why won't it add a comment?
Here's my code...
config.xml
<?xml version="1.0"?>
<config>
<modules>
<Avi_OrderAudit>
<version>0.0.1</version>
</Avi_OrderAudit>
</modules>
<global>
<helpers>
<orderaudit>
<class>Avi_OrderAudit_Helper</class>
</orderaudit>
</helpers>
<models>
<orderaudit>
<class>Avi_OrderAudit_Model</class>
<resourceModel>orderaudit_mysql4</resourceModel>
</orderaudit>
</models>
<events>
<sales_order_place_after> <!-- identifier of the event we want to catch -->
<observers>
<sales_order_place_after_handler> <!-- identifier of the event handler -->
<type>model</type> <!-- class method call type; valid are model, object and singleton -->
<class>orderaudit/observer</class> <!-- observers class alias -->
<method>orderPlaced</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</sales_order_place_after_handler>
</observers>
</sales_order_place_after>
<sales_order_payment_capture> <!-- identifier of the event we want to catch -->
<observers>
<sales_order_payment_capture_handler> <!-- identifier of the event handler -->
<type>model</type> <!-- class method call type; valid are model, object and singleton -->
<class>orderaudit/observer</class> <!-- observers class alias -->
<method>paymentCapture</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</sales_order_payment_capture_handler>
</observers>
</sales_order_payment_capture>
<sales_order_creditmemo_refund> <!-- identifier of the event we want to catch -->
<observers>
<sales_order_creditmemo_refund_handler> <!-- identifier of the event handler -->
<type>model</type> <!-- class method call type; valid are model, object and singleton -->
<class>orderaudit/observer</class> <!-- observers class alias -->
<method>creditmemoRefund</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</sales_order_creditmemo_refund_handler>
</observers>
</sales_order_creditmemo_refund>
</events>
Observer.php
class Avi_OrderAudit_Model_Observer
{
public function orderPlaced(Varien_Event_Observer $observer)
{
if (Mage::getSingleton('admin/session')->isLoggedIn()) {
//if admin
$order = $observer->getEvent()->getOrder();
$user = Mage::getSingleton('admin/session');
$username = $user->getUser()->getUsername();
$comment = "Order placed by <strong>".$username."</strong>";
$order->addStatusHistoryComment($comment)
->setIsVisibleOnFront(false)
->setIsCustomerNotified(false);
}
else {
//placed by customer online
$order = $observer->getEvent()->getOrder();
$order->addStatusHistoryComment('Order placed online by customer')
->setIsVisibleOnFront(false)
->setIsCustomerNotified(false);
}
}
public function paymentCapture(Varien_Event_Observer $observer)
{
$invoice = $observer->getEvent()->getInvoice();
$order = $invoice->getOrder();
$user = Mage::getSingleton('admin/session');
$username = $user->getUser()->getUsername();
$comment = "Payment captured by <strong>".$username."</strong>";
$order->addStatusHistoryComment($comment)
->setIsVisibleOnFront(false)
->setIsCustomerNotified(false);
}
public function creditmemoRefund(Varien_Event_Observer $observer)
{
$creditmemo = $observer->getEvent()->getCreditmemo();
$order = $creditmemo->getOrder();
$user = Mage::getSingleton('admin/session');
$username = $user->getUser()->getUsername();
$comment = "Refund by <strong>".$username."</strong>";
$order->addStatusHistoryComment($comment)
->setIsVisibleOnFront(false)
->setIsCustomerNotified(false);
}
}
After
$order->addStatusHistoryComment($comment)
->setIsVisibleOnFront(false)
->setIsCustomerNotified(false);
you need
$order->save();
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(); ?>
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.
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"
}
}
I want an additional column in the Order(s) Grid for Admin. Assuming its Customer Group Id.
My app/etc/modules/MyProject_Adminhtml looks like:
<?xml version="1.0"?>
<config>
<modules>
<MyProject_Adminhtml>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Sales />
</depends>
</MyProject_Adminhtml>
</modules>
</config>
My app/code/local/MyProject/Adminhtml/etc/config.xml looks like:
<?xml version="1.0"?>
<config>
<modules>
<MyProject_Adminhtml>
<version>1.0.0</version>
</MyProject_Adminhtml>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_grid>MyProject_Adminhtml_Block_Sales_Order_Grid</sales_order_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
And in app/code/local/MyProject/Adminhtml/Block/Sales/Order/Grid.php I have overridden Mage_Adminhtml_Block_Sales_Order_Grid
class MyProject_Adminhtml_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _prepareColumns()
{
.... unchanged code from Mage_Adminhtml_Block_Sales_Order_Grid::_prepareColumns ...
$this->addColumn('customer_group_id', array(
'header' => Mage::helper('sales')->__('Customer Group Id'),
'index' => 'customer_group_id',
'type' => 'text',
));
.... unchanged code from Mage_Adminhtml_Block_Sales_Order_Grid::_prepareColumns ...
}
}
Is there something I am missing because I don't see anything in Order Grid. I am using Magento 1.4.1.1
On Anda B's comment I wrote the following line:
var_dump($this->getLayout()->createBlock('MyProject_Adminhtml_Block_Sales_Order_Grid'));
in app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreateController.php
Then, I selected 'Create New Order' and Cancel the order to see the result of execution of var_dump, and I see the following:
/var/www/magento/var/report/72990635: line 10: syntax error near unexpected token `}' /var/www/magento/var/report/72990635: line 10: `#9 {main}";s:3:"url";s:80:"/index.php/admin/sales_order_create/cancel/key/0624033594dd63d9e145fc538f4c6bbb/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:5:"admin";}'
You are almost there.
You will need to create a renderer for the GroupID as one does not exists in the core.
First, add the renderer to your addColumn like this:
$this->addColumn('customer_group_id', array(
'header' => Mage::helper('sales')->__('Customer Group Id'),
'index' => 'customer_group_id',
'type' => 'text',
'renderer' => 'adminhtml/widget_grid_column_renderer_customergroup',
));
Now you will need to create the directory /app/code/local/MyProject/Adminhtml/Widget/Grid/Column/Renderer/ as it probably doesn't exist.
Now create a Customergroup.php file containing this class:
class MyProject_Adminhtml_Block_Widget_Grid_Column_Renderer_Customergroup extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
private static $_customergroups = array();
public static function getCustomerGroupsArray() {
if(count(self::$_customergroups) == 0) {
$customer_group = Mage::getModel('customer/group');
$customer_groups = $customer_group->getCollection()->toOptionHash();
self::$_customergroups = $customer_groups;
}
return self::$_customergroups;
}
public function render(Varien_Object $row){
$value = $this->_getValue($row);
$customer_groups = self::getCustomerGroupsArray();
return isset($customer_groups[$value]) ? $customer_groups[$value] : false;
}
}
And finally you will need to add this to the config.xml in MyProject. Put this:
<widget_grid_column_renderer_customergroup>Myproject_Adminhtml_Block_Widget_Grid_Column_Renderer_Customergroup</widget_grid_column_renderer_customergroup>
right next to your other rewrite.
After you refresh your cache you should have your Group labels in your sales order grid.
PS.
if you want to add a filter to the top of the sales order grid to work with this column, add this to the 'addColumn' right after the renderer. (order is not actually important)
'options' => TheReadyStore_Adminhtml_Block_Widget_Grid_Column_Renderer_Customergroup::getCustomerGroupsArray(),
and change the type from 'text' to 'options'
Cheers
Roy
In modules/etc you have Atzen_Adminhtml but your project is MyProject_Adminhtml.
Except this problem the code should work: even if you don't have the customer_group_id in sales table, the new column should appear in the grid.