i am new to magento by following this guide Custom Module with Custom Database Table
i have implemented my already existed module to the backend adminhtml. i am taking stuff from the database and sjowing ot on the adminhtml page. Everything works ok except i am getting the grid twice on the adminhtml. i am getting the same Grid two time. i have looked the code for like 2 hours cannot figure it out. if anyone one knows how to fix this problem i will be greatly thakful. cheers
thats the code from my grid.php
<?php
class Ecom_Pricenotify_Block_Adminhtml_Pricenotify_Grid extends Mage_Adminhtml_Block_Widget_Grid{
public function __construct()
{
parent::__construct();
$this->setId('pricenotifyGrid');
// This is the primary key of the database
$this->setDefaultSort('pricenotify_id');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection()
{
$collection = Mage::getModel('pricenotify/pricenotify')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('pricenotify_id', array(
'header' => Mage::helper('pricenotify')->__('Notification ID'),
'align' =>'left',
'width' => '50px',
'index' => 'pricenotify_id',
));
$this->addColumn('prod_id', array(
'header' => Mage::helper('pricenotify')->__('Product ID'),
'align' =>'left',
'width' => '50px',
'index' => 'prod_id',
));
$this->addColumn('prod_price', array(
'header' => Mage::helper('pricenotify')->__('Product Price'),
'align' =>'left',
'width' => '50px',
'index' => 'prod_price',
));
$this->addColumn('user_price', array(
'header' => Mage::helper('pricenotify')->__('User Price'),
'align' =>'left',
'width' => '50px',
'index' => 'user_price',
));
$this->addColumn('email', array(
'header' => Mage::helper('pricenotify')->__('E-Mail Address'),
'align' =>'left',
'width' => '150px',
'index' => 'email',
));
$this->addColumn('created_time', array(
'header' => Mage::helper('pricenotify')->__('Creation Time'),
'align' => 'left',
'width' => '120px',
'type' => 'date',
'default' => '--',
'index' => 'created_time',
));
$this->addColumn('status', array(
'header' => Mage::helper('pricenotify')->__('Status'),
'align' => 'left',
'width' => '80px',
'index' => 'status',
'type' => 'options',
'options' => array(
'success' => 'Inactive',
'pending' => 'Active',
),
));
return parent::_prepareColumns();
}
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}}
and this indexAction function is from the controller
public function indexAction() {
$this->_initAction();
$this->_addContent($this->getLayout()->createBlock('pricenotify/adminhtml_pricenotify'));
$this->renderLayout();
}
Maybe you're inserting it in the layout, check pricenotify.xml in
adminhtml>default>default>layout.
Such as:
<pricenotify_adminhtml_manager_pricenotify>
<block type="core/text_list" name="root" output="toHtml">
<block type="pricenotify/adminhtml_pricenotify_grid" name="pricenotify.grid"/>
</block>
</pricenotify_adminhtml_manager_pricenotify>
Remove this block or comment the line where you add the content.
I fixed it. i only had to comment out
//$this->_addContent($this->getLayout()->createBlock('pricenotify/adminhtml_pricenotify'));
from indexAction i guess iwas loading it twice.
make sure that the grid block isn't already loaded in the corresponding layout.xml file.
Well I was facing the same issue but in my case it was due to $this->setId('messages'); line (in your Grid.php construct). Because magento already has same <div id="messages"></div> in its grid page(for showing notifications) due to which my grid content was getting loaded within this 'div' tag hence showing grid twice. So lesson learned is don't give general name while setting your 'id' in Grid.php which could already be present in the grid page.
In my case, it's happened on Edit/Form, and I had duplicated unintentionally renderLayout() on my Adminhtml controller.
$this->renderLayout();
Related
I have problems with google sitemap.
as you can see in here, I lost the detail table!...if i click add sitemap i get white page!
How can do to restore?
thank you
regards
You may have issue in "/app/code/core/Mage/Adminhtml/Block/Sitemap/Grid.php". So check the file. Here is a code of Grid.php
class Mage_Adminhtml_Block_Sitemap_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('sitemapGrid');
$this->setDefaultSort('sitemap_id');
}
protected function _prepareCollection()
{
$collection = Mage::getModel('sitemap/sitemap')->getCollection();
/* #var $collection Mage_Sitemap_Model_Mysql4_Sitemap_Collection */
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('sitemap_id', array(
'header' => Mage::helper('sitemap')->__('ID'),
'width' => '50px',
'index' => 'sitemap_id'
));
$this->addColumn('sitemap_filename', array(
'header' => Mage::helper('sitemap')->__('Filename'),
'index' => 'sitemap_filename'
));
$this->addColumn('sitemap_path', array(
'header' => Mage::helper('sitemap')->__('Path'),
'index' => 'sitemap_path'
));
$this->addColumn('link', array(
'header' => Mage::helper('sitemap')->__('Link for Google'),
'index' => 'concat(sitemap_path, sitemap_filename)',
'renderer' => 'adminhtml/sitemap_grid_renderer_link',
));
$this->addColumn('sitemap_time', array(
'header' => Mage::helper('sitemap')->__('Last Time Generated'),
'width' => '150px',
'index' => 'sitemap_time',
'type' => 'datetime',
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('store_id', array(
'header' => Mage::helper('sitemap')->__('Store View'),
'index' => 'store_id',
'type' => 'store',
));
}
$this->addColumn('action', array(
'header' => Mage::helper('sitemap')->__('Action'),
'filter' => false,
'sortable' => false,
'width' => '100',
'renderer' => 'adminhtml/sitemap_grid_renderer_action'
));
return parent::_prepareColumns();
}
/**
* Row click url
*
* #return string
*/
public function getRowUrl($row)
{
return $this->getUrl('*/*/edit', array('sitemap_id' => $row->getId()));
}
}
I am trying to extend the sales-> order Grid by Sku and product name.
I managed to get the sku and name in the Grid with all the data.
Now the problem is it's breaking the pagination.
Can anyone please help me with that.
Code:
class Mage_Adminhtml_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
public function __construct()
{
parent::__construct();
$this->setId('sales_order_grid');
$this->setUseAjax(true);
$this->setDefaultSort('created_at');
$this->setDefaultDir('DESC');
$this->setSaveParametersInSession(true);
}
/**
* Retrieve collection class
*
* #return string
*/
protected function _getCollectionClass()
{
return 'sales/order_grid_collection';
}
protected function _prepareCollection()
{
// Custom code
$collection = Mage::getResourceModel($this->_getCollectionClass())
->join(
'sales/order_item',
'`sales/order_item`.order_id=`main_table`.entity_id',
array(
'sku' => new Zend_Db_Expr('group_concat(`sales/order_item`.sku SEPARATOR ",")'),
'name' => new Zend_Db_Expr('group_concat(`sales/order_item`.name SEPARATOR ",")'),
)
);
$collection->getSelect()->group('entity_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
// Custom code
$this->addColumn('name', array(
'header' => Mage::helper('Sales')->__('Name'),
'width' => '100px',
'index' => 'name',
'type' => 'text',
));
$this->addColumn('sku', array(
'header' => Mage::helper('Sales')->__('Sku'),
'width' => '100px',
'index' => 'sku',
'type' => 'text',
));
$this->addColumn('real_order_id', array(
'header'=> Mage::helper('sales')->__('Order #'),
'width' => '80px',
'type' => 'text',
'index' => 'increment_id',
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('store_id', array(
'header' => Mage::helper('sales')->__('Purchased From (Store)'),
'index' => 'store_id',
'type' => 'store',
'store_view'=> true,
'display_deleted' => true,
));
}
$this->addColumn('created_at', array(
'header' => Mage::helper('sales')->__('Purchased On'),
'index' => 'created_at',
'type' => 'datetime',
'width' => '100px',
));
$this->addColumn('billing_name', array(
'header' => Mage::helper('sales')->__('Bill to Name'),
'index' => 'billing_name',
));
$this->addColumn('shipping_name', array(
'header' => Mage::helper('sales')->__('Ship to Name'),
'index' => 'shipping_name',
));
$this->addColumn('base_grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Base)'),
'index' => 'base_grand_total',
'type' => 'currency',
'currency' => 'base_currency_code',
));
$this->addColumn('grand_total', array(
'header' => Mage::helper('sales')->__('G.T. (Purchased)'),
'index' => 'grand_total',
'type' => 'currency',
'currency' => 'order_currency_code',
));
$this->addColumn('status', array(
'header' => Mage::helper('sales')->__('Status'),
'index' => 'status',
'type' => 'options',
'width' => '70px',
'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
));
if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
$this->addColumn('action',
array(
'header' => Mage::helper('sales')->__('Action'),
'width' => '50px',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('sales')->__('View'),
'url' => array('base'=>'*/sales_order/view'),
'field' => 'order_id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
}
$this->addRssList('rss/order/new', Mage::helper('sales')->__('New Order RSS'));
$this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV'));
$this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel XML'));
return parent::_prepareColumns();
}
Problem is with my Collection. I am working here with core class but I will override it once I get my result.
You have a GROUP BY clause in your collection, and the grid pager uses $collection->getSize() to determine the number of pages. The problem is that getSize() applies a SELECT COUNT(*) to the collection, and fetches the first column of the first row to get the number of results. With the GROUP BY still applied, the pager then considers that there is only one result.
To prevent this problem, you should either use your own orders collection with a relevant getSize(), or use sub-queries to retrieve SKUs and names.
Does anyone know how to add a field containing an input type text element or a dropdown element to a magento backend product list grid?
I managed to add a new column to my custom module backend product listing grid like this:
$this->addColumn('blabla', array(
'header' => Mage::helper('customer')->__('On Hold?'),
'width' => '120',
'index' => 'bla',
'type' => 'options',
'options' => array('1' => 'Yes', '0' => 'No')
));
but this command only adds the dropdown to my grid header, while i need the dropdown to appear in the left side of every product listed on that grid (just like the checkbox appears when you go for instance in backend on a product edit page and you select related products, or upsell products)
Simple and fast solution as tip for next research - rewrite Mage_Adminhtml_Block_Catalog_Product_Grid, function _prepareColumns. Example you will create your block Module_Name_Block_Sample:
class Module_Name_Block_Sample extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
protected function _prepareColumns()
{
$this->addColumn('blabla', array(
'header' => Mage::helper('customer')->__('On Hold?'),
'width' => '120',
'index' => 'bla',
'type' => 'options',
'options' => array('1' => 'Yes', '0' => 'No')
));
return parent::_prepareColumns();
}
}
You will get it as first field. And it may need rewrite _prepareCollection.
But it may be not better solution, I know.
What you need is a custom renderer, where you can display any HTML you want. Something like this:
$this->addColumn('blabla', array(
'header' => Mage::helper('customer')->__('On Hold?'),
'width' => '120',
'index' => 'bla',
'renderer' => 'module/sample_grid_renderer'
));
And then you create your renderer class, where you create HTML you need:
class Module_Name_Block_Sample_Grid_Renderer
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$html = '<select name="row'.$row->getId().'"></select>';
return $html;
}
}
$country = $fieldset->addField('country', 'select', array(
'name' => 'country',
'label' => 'Country',
'values' => Mage::getModel('adminhtml/system_config_source_country') ->toOptionArray()
));
Try it! Have a nice day. Thank you.
I'm trying to add new product attributes to Reports->Products Ordered grid. I copied the /app/code/core/Mage/Adminhtml/Block/Report/Product/Sold/Grid.php to
/app/code/local/Mage/Adminhtml/Block/Report/Product/Sold/Grid.php and add attributes like this:
protected function _prepareColumns()
{
$this->addColumn('created_at', array(
'header' =>Mage::helper('reports')->__('Create At'),
'index' =>'created_at'
));
$this->addColumn('sku', array(
'header' =>Mage::helper('reports')->__('sku'),
'index' =>'sku'
));
$this->addColumn('name', array(
'header' =>Mage::helper('reports')->__('Product Name'),
'index' =>'name'
));
$this->addColumn('color', array(
'header' =>Mage::helper('reports')->__('Color'),
'index' =>'color',
'type' => 'options'
));
$this->addColumn('size', array(
'header' =>Mage::helper('reports')->__('Size'),
'index' =>'size'
));
$this->addColumn('price', array(
'header' =>Mage::helper('reports')->__('Price'),
'width' =>'120px',
'type' =>'currency',
'currency_code' => $this->getCurrentCurrencyCode(),
'index' =>'price'
));
$this->addColumn('ordered_qty', array(
'header' =>Mage::helper('reports')->__('Quantity Ordered'),
'width' =>'120px',
'align' =>'right',
'index' =>'ordered_qty',
'total' =>'sum',
'type' =>'number'
));
$this->addExportType('*/*/exportSoldCsv', Mage::helper('reports')->__('CSV'));
$this->addExportType('*/*/exportSoldExcel', Mage::helper('reports')->__('Excel'));
return parent::_prepareColumns();
}
Problem is, 'Color' and 'Size' are dropdown attributes thats why they are showing the option values instead of option text. How to show the Text value for dropdown attributes in grid?
EDIT 1
Thanks BOOMER... I have changed my Grid.php as you suggested and it showing the blank color column now. Heres what I did:
protected function _prepareCollection()
{
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('color');
parent::_prepareCollection();
$this->getCollection()
->initReport('reports/product_sold_collection');
return $this;
}
/**
* Prepare Grid columns
*
* #return Mage_Adminhtml_Block_Report_Product_Sold_Grid
*/
protected function _prepareColumns()
{
$colors = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter(80) // set your attribute ID here
->setStoreFilter()
->load()
->toOptionHash('option_id', 'value');
$this->addColumn('created_at', array(
'header' =>Mage::helper('reports')->__('Create At'),
'index' =>'created_at'
));
$this->addColumn('sku', array(
'header' =>Mage::helper('reports')->__('sku'),
'index' =>'sku'
));
$this->addColumn('name', array(
'header' =>Mage::helper('reports')->__('Product Name'),
'index' =>'name'
));
$this->addColumn('color', array(
'header' =>Mage::helper('reports')->__('Color'),
'index' =>'color',
'type' => 'options',
'options' => $colors
));
}
First ensure you are adding the attribute to select in your _prepareCollection() method, for example:
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('color');
And within your _prepareColumns() you'll need to define the collection of options to use:
$colors = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter(15) // set your attribute ID here
->setStoreFilter()
->load()
->toOptionHash('option_id', 'value');
Be sure and set the attribute ID accordingly. Then use this collection list in your addColumns like so:
$this->addColumn('color', array(
'header' =>Mage::helper('reports')->__('Color'),
'index' =>'color',
'type' => 'options',
'options' => $colors
));
Hope this helps!
I would suggest you to use a different approach, at least for custom attributes. Try replacing your $colors = actions with the following
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');
$colors = array();
foreach( $attribute->getSource()->getAllOptions(true, true) as $option ) {
$colors[$option['value']] = $option['label'];
}
I usually replace the statement inside the foreach with an if condition to avoid duplicate blank options, i.e.:
if ($option['label'])
$sets[$option['value']] = $option['label'];
I am trying to create a custom module in magento admin. I have reached the point where a new link has been added to the menu and by clicking on it, I can navigate to the index action of the controller of the module. But here I cannot see the grid, only the header text and the button which has been added in the block construct appear.
I can see that since this block extends the Mage_Adminhtml_Block_Widget_Grid_Container class, it will by itself add the grid block inside this module as its child.
And the Grid.php is included which I verified by printing out something in the overriden _prepareColumns method.
What am I missing here ?
These are the contents of the Grid.php file
class Book_Brands_Block_Adminhtml_Brands_Grid extends Mage_Adminhtml_Block_Widget_Grid {
public function __construct() {
parent::__construct();
$this->setId('brandsGrid');
$this->setDefaultSort('brands_id');
$this->setDefaultDir('ASC');
$this->setSaveParametersInSession(true);
}
protected function _prepareCollection() {
$collection = Mage::getModel('brands/brands')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('brands_id', array(
'header' => Mage::helper('brands')->__('ID'),
'align' =>'right',
'width' => '50px',
'index' => 'brands_id',
));
$this->addColumn('title', array(
'header'=> Mage::helper('brands')->__('Title'),
'align' =>'left',
'index' => 'title',
));
$this->addColumn('status', array(
'header'=> Mage::helper('brands')->__('Status'),
'align' => 'left',
'width' => '80px',
'index' => 'status',
'type' => 'options',
'options' => array(
1 => 'Enabled',
2 => 'Disabled',
),
));
$this->addColumn('action', array(
'header' => Mage::helper('brands')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('brands')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
return parent::_prepareColumns();
}
public function getRowUrl($row) {
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
}
Thanks
PS. I have tried flushing the cache but no luck
From memory I think _prepareColumns() is called before _prepareCollection() so if there is an error in the collection the grid won't get rendered even though you have confirmed the columns method.
Part of parent::_prepareCollection() tries to estimate the number of pages from the collection's getSize() and getSelectCountSql() methods, I often forget to check those are producing sane results which trips me up. Make sure all logging is turned on and put the following in your .htaccess file:
php_flag display_errors on
SetEnv MAGE_IS_DEVELOPER_MODE true
Try seeing what query is being generated with these commands:
Mage::log((string)$collection->getSelect());
Mage::log((string)$collection->getSelectCountSql());
Looks like you have the grid blocks set up correctly. However, you still need to load the grid into the layout and render it. This can either be done in the adminhtml layout xml or in the controller.
In your /app/design/adminhtml/../layout/brands.xml:
<?xml version="1.0"?>
<layout>
<brands_index_index>
<reference name="content">
<block type="brands/brands_grid" name="brands_grid"></block>
</reference>
</brands_index_index>
</layout>
In your controller:
public function indexAction()
{
$this->loadLayout();
$this->_addContent(
$this->getLayout()->createBlock('brands/brands_grid','brands')
);
$this->renderLayout();
}
Please note that you have to modify the above to your particular implementation. I think the layout xml is harder to comprehend initially than the programmatic instantiation in the controller, however, in the long run, it leads to less code bloat.
Just had a quick view and the only thing I can see in your code is:
protected function _prepareCollection() {
$collection = Mage::getModel('brands/brands')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
//Try to use it like this:
protected function _prepareCollection() {
$collection = Mage::getModel('brands/brands')->getCollection();
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}