Add a dropdown attribute in Product report grid Magento - 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'];

Related

To remove empty option from filter in CgridView Filter -Yii1

I would like to remove the empty or first option of list data value. I have FruitList model and it has a list, so I need to prevent from users to select all.
But now the problem is the empty option that can let user to select all Fruits, so how can I remove.
This is my code
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'header' => 'Buyer',
'name' => 'Buyer',
'value' => 'customer_name',
'filter' => $fruits
),
array(
'header' => 'Fruits',
'name' => 'fruit_id',
'value' => '$data->Buyers->FruitList->Name',
'filter' => $fruits
),
array(
'class'=>'CButtonColumn',
),
),
));
By default filters for CGridView renders dropdown with empty option to allow disabling filtering. But you can overwriting this behavior by providing your own dropdown as a filter:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'header' => 'Buyer',
'name' => 'Buyer',
'value' => 'customer_name',
'filter' => CHtml::activeDropDownList($model, 'customer_name', $fruits),
),
array(
'header' => 'Fruits',
'name' => 'fruit_id',
'value' => '$data->Buyers->FruitList->Name',
'filter' => CHtml::activeDropDownList($model, 'fruit_id', $fruits)
),
array(
'class'=>'CButtonColumn',
),
),
));
Make sure that you set default value for these filters in your model - something like this in your controller:
// ...
$model->fruit_id = FruitList::DEFAULT_ID;
$model->customer_name = FruitList::DEFAULT_ID;
if (isset($_GET['FruitList'])) {
$model->setAttributes($_GET['FruitList']);
}
$dataProvider = $model->search();
// ...
you can set condition in the dataProvider so its return you a result of all not null value.for example
$dataProvider->criteria->addCondition('fruit_id IS NOT NULL ');
I hope its work!

Add a total column from two other existed column magento product sold report

I am working on magento product sold report. Currently, I have 2 existed columns, which are price and ordered_quantity. I want to add a column called "total cost", which is equal to price * ordered_quantity. But I don't know how. This is my code.
Please help!
$this->addColumn('ordered_qty', array(
'header' =>Mage::helper('reports')->__('Quantity Ordered'),
'width' =>'120px',
'align' =>'right',
'index' =>'ordered_qty',
'total' =>'sum',
'type' =>'number'
));
//Unit price column
$this->addColumn('price', array(
'header' =>Mage::helper('reports')->__('Unit Price'),
'width' =>'200px',
'index' =>'price',
'align' =>'right'
));
//Total money column
$this->addColumn('total', array(
'header' =>Mage::helper('reports')->__('Total'),
'width' =>'200px',
'index' => 'total',//This returns null
'total' =>'sum',
'align' =>'right',
'type' =>'number'
));
I found the answer. Using a render custom can work perfectly.
Here is my code.
public function render(Varien_Object $row)
{
$price = $row->getPrice();
$orderedQty=$row->getOrderedQty();
$total = $price * $orderedQty;
return $total;
}

Magento loose table details in Google sitemap

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

Extending sales order Grid breaking Pagination

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.

magento adminhtml custom module it's showing the grid twice

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();

Resources