Adding custom column into configurable product grid in admin - magento

I have created a custom column for associated products (basically every simple product will have that column under "associated products" tab of configurable product). I store information of this field/column into an attribute which belong to configurable product instead of each simple product.
Below is the code for adding a column;
config.xml
<core_block_abstract_to_html_before>
<observers>
<something>
<class>namespace_JsonProductInfo_Model_Observer</class>
<method>addPositionColumn</method>
</something>
</observers>
</core_block_abstract_to_html_before>
Oberver.php
public function addPositionColumn(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Super_Config_Grid) {
if (!$block->isReadonly()) {
$block->addColumnAfter(
'position_simple',
array(
'header' => Mage::helper('jsonproductinfo')->__('Position'),
'type' => 'input',
'name' => 'position_simple',
'index' => 'position_simple',
'sortable' => false,
),
'name'
);
}
}
}
With above code I am able to add the column into a require grid. And I am also able to save the information which I put inside that column (which is basically an attribute). The only problem I am facing is, I am unable to display the stored information into that column again (If I edit the product). I have also created another observer of type eav_collection_abstract_load_befor but no luck so far.
Below is the code for it:
config.xml
<eav_collection_abstract_load_before>
<observers>
<jsonproductinfo>
<class>namespace_JsonProductInfo_Model_Observer</class>
<method>addPositionToCatalogProductCollection</method>
</jsonproductinfo>
</observers>
</eav_collection_abstract_load_before>
Observer.php
public function addPositionToCatalogProductCollection($observer)
{
$collection = $observer->getEvent()->getCollection();
if (!isset($collection)) {
return;
}
$collection->addAttributeToSelect('position_simple');
}
I would be grateful if someone can point to the mistake or guide me how can I display back that information again into that column.
EDIT And here is the code which creates the "position_simple" attribute.
$this->addAttribute(
'catalog_product',
'position_simple',
array(
'group' => 'General',
'type' => 'varchar',
'input' => 'hidden',
'backend' => '',
'frontend' => '',
'label' => 'Simple Position',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false,
'apply_to' => Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE,
'is_configurable' => false,
)
);

change 'index' => 'position_simple' to 'index' => 'entity_id' in your Observer.php (function addPositionColumn)
you need to get value of your position_simple attribute from configurable product and use it in added column - one way to do that:
create function in Observer.php
protected function _getDefaultConfigurationId() {
/** #var Mage_Catalog_Model_Product $product */
$product = Mage::registry('current_product');
if ($product) {
return array($product->getData('position_simple'));
}
return '';
}
and use that in your addPositionColumn function like that:
...
'values' => $this->_getDefaultConfigurationId(),
...
After that, there is no need of observer type eav_collection_abstract_load_befor at all.

Related

Adding a Category Attribute in Magento 1.8.1

I am currently trying to add a new category attribute to the category admin screen in Magento 1.8.1, I am having issues with getting anything to show though.
The only code examples I can find include mysql4, however I thought this had been retired? Please can anyone point us in the right direction here.
I can see my extension under Config > Advanced and in the core_resources table. But not in the front end of the site.
We've tried this recently with 1.8.2.0. You don't really need to create a module just to add a category attribute, once. It seems such a waste to go through so much file cruft to get something installed just once.
Category attributes tend to stay permanently once installed so what worked better for us is to just use a one-off script. Save this one right at magento root.
<?php
require_once "app/Mage.php";
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Sales_Model_Mysql4_Setup;
// change details below:
$attribute = array(
'type' => 'int',
'label'=> 'Your attribute label',
'input' => 'text',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => "",
'group' => "General Information"
);
$installer->addAttribute('catalog_category', 'your_attribute_code', $attribute);
$installer->endSetup();
Save it as add_category_attribute.php or something else memorable for you.
You can either use the browser to get to this file, or use php-cli to run this:
php -f add_category_attribute.php
Good luck.
Change the file name from mysql4-install-0.0.1.php to install-0.0.1.php
#h3nr1ke You can get attribute with:
$category = Mage::registry('current_category');
if ($category){
$value = $category->getData('YOUR_CUSTOM_ATTRIBUTE_CODE');
}
Run this script in your magento root folder to create Attribute
<?php
require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();
$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
if (!$installer->getAttributeId($entityTypeId, 'shipping_content')) {
$installer->addAttribute('catalog_category', 'shipping_content', array(
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Short description',
'input' => 'textarea',
'class' => '',
'source' => '',
'global' => '0',
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => false,
'unique' => false,
'wysiwyg_enabled' => true,
'apply_to' => '',
'is_configurable' => true
));
$installer->updateAttribute($entityTypeId, 'shipping_content', 'is_wysiwyg_enabled', 1);
$installer->updateAttribute($entityTypeId, 'shipping_content', 'is_html_allowed_on_front', 1);
}
$installer->endSetup();
?>
For Remove Category Attribute
<?php
require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Sales_Model_Mysql4_Setup;
$installer->startSetup();
$installer->removeAttribute('catalog_category', 'shipping_content');
$installer->endSetup();
?>

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.

Add custom fields in review form

I am looking forward to create a custom fields 'Email Id' & One drop-down in Review form .
I have tried this one but not saving the data, its hows the fields only
app\code\core\Mage\Review\Model\Mysql4\Review.php
protected function _afterSave(Mage_Core_Model_Abstract $object)
{
$detail = array(
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
'email' => $object->getEmail(), // New field 1
'fname' => $object->getFname(), // New field 2
);
Now add email,fname in the review_detail table in the database also go to app\code\core\Mage\Adminhtml\Block\Review\Edit\Form.php also add :
$fieldset->addField('fname', 'text', array( // New field 2
'label' => Mage::helper('review')->__('First Name'),
'required' => true,
'name' => 'fname'
));
$fieldset->addField('email', 'text', array( // New field 1
'label' => Mage::helper('review')->__('Email'),
'required' => true,
'name' => 'email'
));
before to
$fieldset->addField('nickname', 'text', array(
'label' => Mage::helper('review')->__('Nickname'),
'required' => true,
'name' => 'nickname'
));
Modify the Mage core class is a bit scary, which will be difficult to upgrade magento core class in the future. You can override the specific class by your own custom module (See module creator if you want to setup one)
Module's config.xml, add the models rewrite in as below:
<global>
<models>
<review_mysql4>
<rewrite>
<review>[[Your Company]]_[[Your Module]]_Model_Review</review>
</rewrite>
</review_mysql4>
</models>
...
</global>
And the specified class will extend from the Magento core class you want to override:
class [[Your Company]]_[[Your Module]]_Model_Review
extends Mage_Review_Model_Mysql4_Review
{
protected function _afterSave(Mage_Core_Model_Abstract $object)
{
....
}
}
Ps. to add the new field in magento review_detail table:
$installer = $this;
$installer->startSetup();
$installer->run("ALTER TABLE review_detail ADD COLUMN email VARCHAR(255) NULL");
$installer->endSetup();
Finally i have solved it...
Open
app\code\core\Mage\Review\Model\Resource\Review.php
you will find this code in line about 150
protected function _afterSave(Mage_Core_Model_Abstract $object)
{
$detail = array(
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
);
Add the new two fields you want to add.
protected function _afterSave(Mage_Core_Model_Abstract $object)
{
$detail = array(
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
'email' => $object->getEmail(), // New field 1
'fname' => $object->getFname(), // New field 2
);
Thats it no more.... :) Happy coding

Magento 1.6.2.0 sales orders custom attribute is not working

I already have spent all day on this and I think I can't get this working because the possibility to add custom EAV attributes to orders was removed.
At least, I've noticed that sales_order_entity is missing.
Well, what I try to do is to add a custom field to sales orders. I thought it would work the same way as for category products, but looks like nope.
My overall point in doing all this is because I want to track who is adding products to catalog and want to relate certain order with certain user (not customer).
public function getDefaultEntities()
{
return array(
'catalog_product' => array(
'entity_model' => 'catalog/product',
'attribute_model' => 'catalog/resource_eav_attribute',
'table' => 'catalog/product',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/product_attribute_collection',
'attributes' => array(
'seller_id' => array(
'group' => 'MyCustom',
'label' => 'Seller ID',
'type' => 'int',
'input' => 'text',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => false,
'required' => true,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false,
),
),
),
'order' => array(
'entity_model' => 'sales/order',
'table' => 'sales/order',
'increment_model' => 'eav/entity_increment_numeric',
'attributes' => array(
'seller_id' => array(
'group' => 'MyCustom',
'label' => 'Seller ID',
'type' => 'int',
'input' => 'text',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => false,
'required' => true,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false,
),
),
),
);
}
It works for products, but not for orders. I have required entries in eav_attribute table.
I don't know if I'm doing something wrong or this is just impossible to do?
I also thought about solving this different way by creating additional table to track relations between user - order|product. This would require more work tough.
I know this is an old post but I came across it when I was looking for answers to the same problem, so thought I would share my solution.
Lets take adding an attribute to a sales order as an example.
Firstly, Magento no longer users EAV entities for Sales, if you look at core/Mage/Sales/etc/config.xml
<sales>
<class>Mage_Sales_Model</class>
<resourceModel>sales_resource</resourceModel>
</sales>
resouceModel no longer points at sales_entity(EAV), it now points at sales_resource(flat). If you look at the children of the sales_resource node, you will find the order node:
<order>
<table>sales_flat_order</table>
</order>
This means that a Mage_Sales_Model_Order has a resource model of Mage_Sales_Model_Resource_Order which has a table of sales_flat_order.
The magento developers have provided the class Mage_Sales_Model_Resource_Setup which allows you to add attributes to this new "flat" structure in pretty much the same way you would have added attributes to the EAV structure. If you take a look inside Mage_Sales_Model_Resource_Setup you will see the following function:
/**
* Add entity attribute. Overwrited for flat entities support
*
* #param int|string $entityTypeId
* #param string $code
* #param array $attr
* #return Mage_Sales_Model_Resource_Setup
*/
public function addAttribute($entityTypeId, $code, array $attr)
{
if (isset($this->_flatEntityTables[$entityTypeId]) &&
$this->_flatTableExist($this->_flatEntityTables[$entityTypeId]))
{
$this->_addFlatAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr);
$this->_addGridAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr, $entityTypeId);
} else {
parent::addAttribute($entityTypeId, $code, $attr);
}
return $this;
}
With this information you should now see that it is simply a case of getting an instance of Mage_Sales_Model_Resource_Setup and calling its public addAttribute method with valid parameters.
Code snippet of adding a franchise_id attribute to a sales order:
$salesResourceSetupModel = Mage::getModel('sales/resource_setup', 'core_setup');
$data=array(
'type'=>'int',
'input'=>'text',
'label'=>'Franchise',
'global'=> 1,
'is_required'=>'0',
'is_comparable'=>'0',
'is_searchable'=>'0',
'is_unique'=>'0',
'is_configurable'=>'0',
'user_defined'=>'1',
//whether it should be including in the sales order grid
'grid'=>1
);
//first param should relate to a key of the protected $_flatEntityTables array
$salesResourceSetupModel->addAttribute('order', 'franchise_id', $data);
For Versions > 1.4.1.0 you will have to create a column in the sales_flat_order table. You can take a look in this post
is magento sales eav

Add new tab page in admin category - Magento

I'm looking for to add a new tabpage under category in magento admin, which loads the category grid. Idea is to associate multiple categories to one category and on the front end, when you click on that particular category, it should show products from all the associated categories.
Is there any better solution then create a new tabpage with categories grid?
EDITTED
Right... I have realised I could add new fields in category with install script. Now in my setup script I am doing something like this:
public function getDefaultEntities()
{
return array(
'catalog_category' => array(
'entity_model' => 'catalog/category',
'attribute_model' => 'catalog/resource_eav_attribute',
'table' => 'catalog/category',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/category_attribute_collection',
'attributes' => array(
'men_categories' => array(
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Men Categories',
'input' => 'select',
'class' => '',
'source' => 'pushon_config/config_source',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => 0,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'group' => 'Associated Categories',
),
),
),
);
}
In my source.php in doing this:
class PushOn_Config_Model_Config_Source extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
protected $_options = array();
public function getAllOptions($withEmpty = true, $defaultValues = false)
{
$option = Mage::getResourceModel('catalog/category_collection');
foreach($option as $id){
$this->_options[] = array('value'=> $id->getData('entity_id'), 'label' => $id->getData('name'));
}
return $this->_options;
}
}
Now when I run this it shows me error message:
a:5:{i:0;s:163:"Error in file: "C:\wamp\www\vhosts\staging.domainname.com\httpdocs\app\code\local\PushOn\Config\sql\pushon_config_setup\mysql4-install-0.1.0.php" - Wrong entity ID.";i:1;s:1148:"#0 C:\wamp\www\vhosts\staging.domainname.com\httpdocs\app\code\core\Mage\Core\Model\Resource\Setup.php(390): Mage::exception('Mage_Core', 'Error in file: ...')
I am just trying to load all the categories. Any idea why this is happening?

Resources