Adding data from customer_entity_varchar to order grid magento - magento

I am trying to add 'accountno' field to order grid from customer_entity_varchar table. To achieve this I changed _prepareCollection() method in Grid.php like this:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$customerEntityVarchar = Mage::getSingleton('core/resource')->getTableName('customer_entity_varchar');
$collection->getSelect()->join(array(
'customer_entity_varchar_table'=>$customerEntityVarchar),
'`main_table`.`customer_id`=`customer_entity_varchar_table`.`entity_id`
AND `customer_entity_varchar_table`.`attribute_id`=122',
array('accountno' => 'value'));
print_r($collection->getSelect()->__toString());
$this->setCollection($collection);
return parent::_prepareCollection();
}
In attribute_id=122' 122 is the attribute_id ofaccountno`
When I print_r the select query and execute it in phpmyadmin, it gets me the accurate results back but when I try to add Column to grid in _prepareColumns() method, it shows the field blank.
My _prepareColumns method looks like this:
protected function _prepareColumns()
{
$this->addColumn('real_order_id', array(
'header'=> Mage::helper('sales')->__('Order #'),
'width' => '80px',
'type' => 'text',
'index' => 'increment_id',
));
$this->addColumnAfter('accountno', array(
'header' => Mage::helper('sales')->__('Account No'),
'index' => 'accountno',
), 'real_order_id');
//rest of the code here
}

Related

create custom collection in magento

We have a table with delivery dates and i want to add it to the adminhtml in sales/order/grid.
The problem that i have is that the left join examples dont work for me and im out of options.
Mage/Adminhtml/Block/Sales/Order/Grid.php
The method im trying now is to create a custom query in _prepareCollection()
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT * FROM mage_sales_flat_order_grid
left join mage_aw_deliverydate_delivery
on mage_sales_flat_order_grid.entity_id =
mage_aw_deliverydate_delivery.order_id';
$collection = $readConnection->fetchAll($query);
//$collection = Mage::getResourceModel($this->_getCollectionClass());
//$this->setCollection($collection);
return parent::_prepareCollection();
Then i add this column code in _prepareColumns()
$this->addColumn('delivery_date', array(
'header' => 'delivery_date',
'index' => 'delivery_date',
'type' => 'datetime',
'width' => '100px',
));
I dont even see the new column when i look at the orders grid in the backoffice.
Any help is appreciated!
If you want to add the anthor table
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->joinLeft('aw_deliverydate_delivery', 'main_table.entity_id = aw_deliverydate_delivery.order_id',array('*' ) );
$this->setCollection($collection);
return parent::_prepareCollection();
}
And the below code in _prepareColumns()
$this->addColumn('aw_deliverydate_delivery', array(
'header' => Mage::helper('sales')->__('Delivery Date '),
'index' => 'aw_deliverydate_delivery',
'type' => 'datetime',
'width' => '100px',
));

How add custom filter to colum grid in Magento?

i have custom grid in Adminhtml.
protected function _prepareCollection()
{
/* #var $collection Mage_Catalog_Model_Resource_Product_Collection */
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('*');
$collection->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left');
$collection->groupByAttribute('entity_id');
$collection->addStaticField('category_id');
$collection->addExpressionAttributeToSelect('category_grp', 'GROUP_CONCAT(category_id)', 'category_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
parent::_prepareColumns();
$this->addColumn('category_id', array(
'header' => Mage::helper('newsletter')->__('Category'),
'index' => 'category_grp',
'type' => 'categories',
'options' => $options,
'align' => 'left',
// 'filter_index' => $this->_getFlatExpressionColumn('category'),
return $this;
}
category_grp is array with int
My question is how can i add filter to header column which filters the item from the field?
for example filter only product with category_id=7 (category_grp is 3,6,7,13)...
I don't think you will need any custom filter.
Just try to set index in accord with field name:
'index' => 'category_id',
Take a look at magento filter_condition_callback option
$this->addColumn('categories', array(
....
'filter_condition_callback' => array($this, '_applyMyFilter'),
..
)
);
protected function _filterCategoriesCondition($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return;
}
$this->getCollection()->addFieldToFilter('categories', array('finset' => $value));
}
See
Filtering a text-type column with MySQL-computed values in Magento Admin Grid
Magento: How to search or filter by multiselect attribute in admin grid?

Magento - grid pagination

I created grid in magento backend, but pagination doesn't work. No matter how many records per page I choose there are always all of them visible on the page. Now I have 41 records in database and 'stats' above the grid are ok (number of pages and records found):
Page 1 of 3 pages | View 20 per page | Total 41 records found
Which file is responsible of pagination?
There's also another problem with order by certain column. For ex. records are displayed the same way either I choose ASC or DESC order by ID...
Grid:
public function __construct() {
parent::__construct();
$this->setId('logger_grid');
$this->setUseAjax(FALSE);
$this->setDefaultSort('id');
$this->setDefaultDir(Varien_Data_Collection::SORT_ORDER_ASC);
$this->setSaveParametersInSession(TRUE);
}
public function _prepareCollection() {
$collection = Mage::getModel('logger/logger')->getCollection()->load();
$this->setCollection($collection);
return parent::_prepareCollection();
}
public function _prepareColumns() {
$this->addColumn('id', array(
'header' => Mage::helper('logger')->__('ID'),
'sortable' => TRUE,
'index' => 'log_id',
'editable' => FALSE,
));
$this->addColumn('interface', array(
'header' => Mage::helper('logger')->__('Interface'),
'sortable' => TRUE,
'index' => 'interface',
'editable' => FALSE,
));
$this->addColumn('type', array(
'header' => Mage::helper('logger')->__('Type'),
'sortable' => TRUE,
'index' => 'type',
'editable' => FALSE,
));
$this->addColumn('description', array(
'header' => Mage::helper('logger')->__('Description'),
'sortable' => TRUE,
'index' => 'description',
'editable' => FALSE,
));
$this->addColumn('message_data', array(
'header' => Mage::helper('logger')->__('Message'),
'sortable' => TRUE,
'index' => 'message_data',
'editable' => FALSE,
));
$this->addColumn('time', array(
'header' => Mage::helper('logger')->__('Time'),
'sortable' => TRUE,
'index' => 'time',
'editable' => FALSE,
'type' => 'datetime',
));
return parent::_prepareColumns();
}
public function getRowUrl($row) {
return $this->getUrl('*/*/edit', array('id' => $row->getId()));
}
Collection.php:
public function _construct(){
$this->_init("logger/logger");
}
Ok, problem solved. As always just one little thing...In _prepareCollection() function I used
$collection = Mage::getModel('logger/logger')->getCollection()->load(); and pagination didn't work because of load() function.
Thanks for replying, sparcksoft :)
If you created a custom collection resource model, it could be that you've overwritten or broken the implementation of {{_renderLimit()}} which, adds a limit to the underlying SQL query based on the current page and page size.
// Varien_Data_Collection_Db
protected function _renderLimit()
{
if($this->_pageSize){
$this->_select->limitPage($this->getCurPage(), $this->_pageSize);
}
return $this;
}
Can you post relevant portions from your collection resource model and maybe your grid block?
You can use this:
protected function _prepareCollection()
{
$collection = Mage::getModel('vendor/model')->getCollection();
$this->setCollection($collection);
//
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
In this case you can reach your custom model and display with pagination
To fix this, you need to edit one of your core file please follow the below link:
https://raisereview.com/wrong-grid-count-and-pagination-issue-in-magento-admin-grid/

Add a dropdown attribute in Product report grid Magento

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'];

Grid doesn't appear in custom admin module in Magento

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;
}

Resources