Adding Stock Status column to Manage Product Grid in Admin - magento

I'm trying to add 'stock status' column to the Admin Manage Product Grid. Stock status is either "In Stock" or "Out of Stock".
Seems like I need to edit Adminhtml/Block/Catalog/Product/Grid.php
I added this line:
$this->addColumn('stock',
array(
'header'=> Mage::helper('catalog')->__('Stock Avail.'),
'width' => '70px',
'index' => 'status',
'type' => 'options',
'options' => Mage::getSingleton('cataloginventory/source_stock')->toOptionArray()
But just prints out Array text....

Try with following code as mentioned by Marius in this answer.
In Grid.php file find $this->setCollection($collection); and before this code add following code (Join) :
$collection->joinTable(
'cataloginventory/stock_status',
'product_id=entity_id',
array("stock_status" => "stock_status"),
null ,
'left'
)->addAttributeToSelect('stock_status');
And now you can add column like :
$this->addColumn('stock_status',
array(
'header'=> 'Stock Status',
'width' => '60px',
'index' => 'stock_status',
'type' => 'options',
'options' => array('1'=>'In Stock','0'=>'Out Of Stock'),
));
I hope this will help

Related

Magento issue with adding validation into multi-select checkbox

I have a form in magento admin panel. In the form i have checkboxes which i can select multiple options or one. The issue is i am unable to put validations for that. Because without selecting any option i can save records. My code is as in below:
$fieldset-> addField('time_ranges', 'checkboxes', array(
'label' => Mage::helper('CheckoutTime')->__('Time Ranges'),
'required' => true,
'class' => 'required-entry',
'name' => 'time_ranges[]',
'values' => array(
array(
'label' => Mage::helper('CheckoutTime')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('CheckoutTime')->__('Investment'),
)
),
));
Can anyone please tell me how to add validations into this form.
Thank You
Try by changing
'name' => 'time_ranges[]'
to
'name' => 'time_ranges'
This way is correct. There were some issues in other places in my coding. That's why it didn't work early. Or else this is the correct way to do that.

Fieldset in system configuration - Magento Admin module

I am creating an admin module. I have set of fields and i want to create a fieldset each of 3 fields in system configuration, i have created fields but wanted to add fieldset in it.
Any help would be appreciated. Thank you
You haven't given much information i.e Layout of your module, whether you are adding fields in code or .phtml but this is how I am adding fields to a field set:
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('form_settings', array('legend'=>Mage::helper('mymodule')->__('Module Settings')));
$newFieldset = $fieldset->addFieldset('form_settings_test', array('legend'=>Mage::helper('khaosconnect')->__('Order Settings')));
$newFieldset->addField('mysetting1', 'text', array(
'label' => Mage::helper('mymodule')->__('Setting 1'),
'class' => 'required-entry',
'required' => true,
'name' => 'mysetting1',
'value' => "val1",
'style' => 'width:500px'
));
$newFieldset->addField('mysetting2', 'text', array(
'label' => Mage::helper('mymodule')->__('Setting 2'),
'class' => 'required-entry',
'required' => true,
'name' => 'mysetting2',
'value' => "val2",
'style' => 'width:500px'
));
}
EDIT: Updated to show nested fieldsets.

What controls the position of columns in grids?

I've added a customer column to the Sales > Orders grid by extending prepareColumns. What determines the order the columns are rendered in? Is it possible to change a column's position on the grid?
By default columns are displayed in the order addColumn() was called. You can change this with the function addColumnAfter().
The following code would add a column with an id of "category_id" after the "entity_id" column. The 'entity_id' is the id of the column, not the "index". 99/100 times these are the same, but be aware of it.
$this->addColumnAfter('category_id',array(
'header' => 'Category ID',
'index' => 'category_id',
'type' => 'text',
'width' => 70
),
'entity_id'
);
Use $this->addColumnAfter() instead of $this->addColumn()
$this->addColumnAfter('customattribute', array(
'header'=> Mage::helper('customer')->__('customattribute'),
'index' => 'customattribute2',
'type' => 'options',
'options' => Mage::getSingleton('adminhtml/system_config_source_yesno')->toArray(),
'width' => '100px',
), 'before_grid_id');
Put $this->sortColumnsByOrder(); at last of _prepareColumns() function.
protected function _prepareColumns() {
parent::_prepareColumns();
$this->addColumnAfter('category_id', array(
'header' => $this->__('Category ID'),
'index' => 'entity_id',
'type' => 'text',
'width' => 70
), 'entity_id');
$this->addColumnAfter('name', array(
'header' => $this->__('Category name'),
'sortable' => true,
'index' => 'category_name'), 'entity_id');
$this->sortColumnsByOrder();
return $this;
}
IMHO none of the answers above answer the question about setting the order of an existing column, i.e. without adding it manually.
For example for placing the "SKU" column right after the "ID" one, use the following code:
$this->addColumnsOrder('sku', 'entity_id');
Where the first parameter is the ID/name of the column to be inserted after the column identified in the second parameter.
By using the addColumnAfter and/or addColumnOrder functions instead of addColumn.

Magento Backend Product list grid

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.

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