Create Magento Backend Field ReadOnly - magento

I would like to create a read-only field in the backend at the client's Magento.
create fields to know (through a module) is as follows:
$installer->addAttribute("customer", "attrcode", array(
"type" => "varchar",
"backend" => "",
"label" => "label",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
));
this way it creates the field, but he's not just reading ...
Thank You

One possible solution is to use javascript to disable the button on page load
Create a js file and upload it to your admin skin/js directory (disable_button.js)
add
document.observe('dom:loaded', function(){
$("target_input_id").disabled=true;
});
Then add or update you local.xml to include the js files
<?xml version="1.0"?>
<layout version="0.1.0">
<adminhtml_customer_edit>
<reference name="head">
<action method="addItem"><type>skin_js</type><script>js/disable_button.js</script></action>
</reference>
</adminhtml_customer_edit>
</layout>

I don't think what you are trying to is possible using addAttribute(), _prepareValues($attr) method only allow specific values that are store in $data.
Take a look # app/code/core/Mage/Eav/Model/Entity/Setup.php
public function addAttribute($entityTypeId, $code, array $attr)
{
$entityTypeId = $this->getEntityTypeId($entityTypeId);
$data = array_merge(
array(
'entity_type_id' => $entityTypeId,
'attribute_code' => $code
),
$this->_prepareValues($attr);
);
.....
if ($attributeId) {
$this->updateAttribute($entityTypeId, $attributeId, $data, null, $sortOrder);
} else {
$this->_insertAttribute($data);
}
.......
}
protected function _prepareValues($attr)
{
$data = array(
'backend_model' => $this->_getValue($attr, 'backend'),
'backend_type' => $this->_getValue($attr, 'type', 'varchar'),
'backend_table' => $this->_getValue($attr, 'table'),
'frontend_model' => $this->_getValue($attr, 'frontend'),
'frontend_input' => $this->_getValue($attr, 'input', 'text'),
'frontend_label' => $this->_getValue($attr, 'label'),
'frontend_class' => $this->_getValue($attr, 'frontend_class'),
'source_model' => $this->_getValue($attr, 'source'),
'is_required' => $this->_getValue($attr, 'required', 1),
'is_user_defined' => $this->_getValue($attr, 'user_defined', 0),
'default_value' => $this->_getValue($attr, 'default'),
'is_unique' => $this->_getValue($attr, 'unique', 0),
'note' => $this->_getValue($attr, 'note'),
'is_global' => $this->_getValue($attr, 'global',
Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
),
);
return $data;
}

I have developed exactly such extension that work for products, categories and CMS pages. You just have to define some rules and choose which attributes you want to show as read-only.
Extension URL: https://www.bubbleshop.net/magento-admin-readonly.html

Related

Add multiple fields in registration form magento

I want to add multiple fields in registration form.
installation file is:-
<?php
$installer = $this;
$installer->startSetup();
$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$setup->addAttribute('customer', 'accounttype', array(
'type' => 'int',
'input' => 'select',
'label' => 'Accounttype',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => '0',
'visible_on_front' => 1,
'source' => 'profile/entity_accounttype',
));
$setup->addAttribute('customer', 'tva', array(
'type' => 'int',
'input' => 'select',
'label' => 'Tva',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => '0',
'visible_on_front' => 2,
'source' => 'profile/entity_tva',
));
$setup->addAttribute('customer', 'companycountry', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Companycountry',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => '0',
'visible_on_front' => 4,
'source' => 'profile/entity_companycountry',
));
$setup->addAttribute('customer', 'companycomment', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Companycomment',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => '0',
'visible_on_front' => 3,
'source' => 'profile/entity_companycomment',
));
if (version_compare(Mage::getVersion(), '1.6.0', '<='))
{
$customer = Mage::getModel('customer/customer');
$attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId();
$setup->addAttributeToSet('customer', $attrSetId, 'General', 'accounttype','tva','companycountry','companycomment');
}
if (version_compare(Mage::getVersion(), '1.4.2', '>='))
{
Mage::getSingleton('eav/config')
->getAttribute('customer', 'accounttype', 'tva','companycountry','companycomment' )
->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register'))
->save();
}
$tablequote = $this->getTable('sales/quote');
$installer->run("
ALTER TABLE $tablequote ADD `customer_accounttype` INT NOT NULL,
ADD `customer_tva` INT NOT NULL,
ADD `customer_companycountry` varchar(100) NOT NULL,
ADD `customer_companycomment` varchar(1000) NOT NULL
");
$installer->endSetup();
I made 4 source model for each field. only one field us made & also data is not saving in database. Please help me where I am wrong.
you can go through this link Magento Module Creater
and create module with Need Add Customer Attribute : yes and it will give the field for your customer attribute. and create field as much as you.after creating field just click Create Magento Module (download) it will give you a ready module with your custom customer attribute. Note:- Selecte **Forms to Use In** in which your field will be populated for user input
Hope this will help you.
Try this code :
$attributeArray = [
[
'code' => 'is_privacy_statement',
'label' => 'Privacy Statement',
'position' => '1020'
],[
'code' => 'is_general_terms',
'label' => 'General Terms',
'position' => '1021'
]
];
foreach($attributeArray as $attributeCreate)
{
$installer = new Mage_Customer_Model_Entity_Setup('core_setup');
$installer->startSetup();
$installer->addAttribute(
"customer", $attributeCreate['code'], array(
"type" => "int",
"label" => $attributeCreate['label'],
"input" => "text",
"visible" => true,
"required" => true,
"default" => "1",
)
);
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", $attributeCreate['code']);
$used_in_forms = array();
$used_in_forms[] = "adminhtml_customer";
$used_in_forms[] = "checkout_register";
$used_in_forms[] = "customer_account_create";
$used_in_forms[] = "customer_account_edit";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", $attributeCreate['position']);
$attribute->save();
$installer->endSetup();
}
Note: input type are changed, as per "On customer register got an error message: “Cannot save the customer”" because got errors with checkbox and/or boolean type, and the code a little bit optimised.

Magento how to add new attribute to sales/order model?

I want to add one attribute to sales/order, that is the Mage_Sales_Model_Order, to store some extra data.
I found the resource of sales/order uses normal table. And when I save an order, it only saves the fields that match the columns in the table.
What is the right way to add this attribute?
I would recommend a hybrid of a few of the answers below. You definitly want to put this code in a install script via config xml but your going to want to use the Mage_Eav_Model_Entity_Setup as your setup class if you want to leverage the addAttribute function. So your config xml will look something like this...
<config>
...
<resources>
<modulename_setup>
<setup>
<module>Your_Module</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</setup>
</modulename_setup>
</resources>
...
</config>
and your install script should look something like this
$installer = $this;
/* #var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */
$installer->startSetup();
$installer->addAttribute('sales_order', 'attributename', array(
'group' => 'General',
'label' => 'Label frontend',
'note' => '',
'type' => 'string', //backend_type
'input' => 'text', //frontend_input
'frontend_class' => '',
'source' => '',
'backend' => '',
'frontend' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
'required' => false,
'visible_on_front' => false,
'apply_to' => 'simple',
'is_configurable' => false,
'used_in_product_listing' => false,
'sort_order' => 5,
));
$installer->endSetup();
Keep in mind this is untested code, and you might need to tweak some of the attribute options to get it to function the way you need it to.
To add new attribute on order you use this code, but just one time. Get any file and put the code there, execute one time and can remove the code.
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('sales_order', 'attributename', array(
'group' => 'General',
'label' => 'Label frontend',
'note' => '',
'type' => 'string', //backend_type
'input' => 'text', //frontend_input
'frontend_class' => '',
'source' => '',
'backend' => '',
'frontend' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
'required' => false,
'visible_on_front' => false,
'apply_to' => 'simple',
'is_configurable' => false,
'used_in_product_listing' => false,
'sort_order' => 5,
));

How to use custom customer variables in transactional email templates in magento?

I have added some custom variables to customer and I want to use them in email template.
Here's example how I have added a variable to customer in my installer:
$setup->addAttribute('customer', 'companyname', array(
'input' => 'text',
'type' => 'varchar',
'label' => 'Company name',
'visible' => 1,
'required' => 1,
'user_defined' => 1,
));
$setup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'companyname',
'999' //sort_order
);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'companyname');
$oAttribute->setData('used_in_forms', array('adminhtml_customer','customer_account_create'));
$oAttribute->save();
I try to use this variable in email template:
{{var order.getCustomer().getCompanyname()}}
But it's not showing. How to make it work?
I done similar task with code like below:
$setup->addAttribute('customer', 'attr_name', array(
'type' => 'int',
'input' => 'select',
'label' => 'Attr label',
'global' => 1,
'visible' => 1,
'required' => 1,
'default' => '0',
'user_defined' => 1,
'visible_on_front' => 1,
));
if (version_compare(Mage::getVersion(), '1.6.0', '<='))
{
$customer = Mage::getModel('customer/customer');
$attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId();
$setup->addAttributeToSet('customer', $attrSetId, 'General', 'attr_name');
}
if (version_compare(Mage::getVersion(), '1.4.2', '>='))
{
Mage::getSingleton('eav/config')
->getAttribute('customer', 'attr_name')
->setData('used_in_forms', array('customer_account_create'))
->save();
}
Next have tested it (right now) within "Order new" email {{var order.getCustomer().getAttrName()}} and have got correct value.
Try it, might it helps to you.

Magento: add new attribute to all products

I want to add a new attribute to all products. I have done it with a install script trough
$installer = $this;
$installer->startSetup();
$this->addAttribute('catalog_product','test2',array(
'label' => 'test2',
'type' => 'varchar',
'visible' => true,
'required' => false,
'required' => 0
));
But how can I add values to this attribute by
$entityTypeId = $installer->getEntityTypeId('catalog_product');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttributeGroup($entityTypeId, 'Default', 'test2', 0);
$installer->endSetup();
This is one of the sample code which I had used to create my own custom Product Attribute:-
$installer = $this;
/* #var $installer Mage_Core_Model_Resource_Setup */
$installer->startSetup();
$attrCode = 'test2';
$attrGroupName = 'Test Group';
$attrLabel = 'Test 2';
$attrNote = 'Test Note';
$objCatalogEavSetup = Mage::getResourceModel('catalog/eav_mysql4_setup', 'core_setup');
$attrIdTest = $objCatalogEavSetup->getAttributeId(Mage_Catalog_Model_Product::ENTITY, $attrCode);
if ($attrIdTest === false) {
$objCatalogEavSetup->addAttribute(Mage_Catalog_Model_Product::ENTITY, $attrCode, array(
'group' => $attrGroupName,
'sort_order' => 7,
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => $attrLabel,
'note' => $attrNote,
'input' => 'text',
'class' => '',
'source' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '0',
'visible_on_front' => false,
'unique' => false,
'is_configurable' => false,
'used_for_promo_rules' => true
));
}
$installer->endSetup();
This is used with the references of these two articles:-
Magento Wiki: Installing Custom Attributes with Your Module
Mukesh Chapagain Blog: Magento Adding attribute from MySQL setup file
Also, you will find that I have used the array key "group" to mention the Attribute Group Name, where this new custom Attribute will reside. The irony is that mentioning of this key, in the above code sample, automatically creates this Attribute in every Attribute Set found in this Magento.
So you do not need to call any method (like "addAttributeToSet()") to add this Attribute to all Attribute Sets.
Hope it helps.
Run this script in your magento root directory.(Change Configuration you need)
<?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();
$installer->addAttribute('catalog_product', 'snum', array(
'label' => 'Serial No',
'type' => 'int',
'input' => 'text',
'backend' => '',
'frontend' => '',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => true,
'visible_in_advanced_search' => false,
'unique' => false
));
$installer->endSetup();
?>
For Remove Product 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_product', 'snum');
$installer->endSetup();
?>
you can add custom attribute to the magento backend as shown.If you create the product attribute as module it is easy to move from one database to another.
?php
$this->startSetup();
$this->addAttribute(catalog_product, 'featured_product', array(
'group' => 'General',
'input' => 'select',
'type' => 'text',
'label' => 'Featured Product',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'source' => 'eav/entity_attribute_source_boolean',
'sort_order' => 8,
));
$this->endSetup();
please refer my tutorial for step by step explanation and file structure.
http://www.pearlbells.co.uk/adding-custom-product-attributes-in-magento/

Magento EAV: What is the 'source' setting used for?

I'm curious what this is used for? I defined the following source model for a custom attribute I added with an entity script, yet I have no idea how to make use of the source attribute. Maybe I can use it a Form Widget? The attribute I added was exportStatus to customer eav.
<?php
class Company_Customer_Model_Customer_Attribute_Source_ExportStatus
extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = array(
array(
'value' => '0',
'label' => 'Pending Export',
),
array(
'value' => '1',
'label' => 'Exported to Mainframe',
),
array(
'value' => '2',
'label' => 'Acknowledged by Mainframe',
)
);
}
return $this->_options;
}
}
and
<?php
class Company_Customer_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
public function getDefaultEntities()
{
return array(
'customer' => array(
'entity_model' =>'customer/customer',
'attribute_model' => 'customer/attribute',
'table' => 'customer/entity',
'additional_attribute_table' => 'customer/eav_attribute',
'entity_attribute_collection' => 'customer/attribute_collection',
'attributes' => array(
'export_status' => array(
//'group' => 'Group/Tab',
'label' => 'Customer Export Status',
'type' => 'int',
'input' => 'select',
'default' => '0',
'class' => '',
'backend' => '',
'frontend' => '',
'source' => 'company_customer/customer_attribute_source_exportStatus',
'global' => 2, //global scope
'visible' => true,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'visible_in_advanced_search' => false,
'unique' => false
)
)
)
);
}
}
It allows you to create drop down menus.
See this article:
http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/installing_custom_attributes_with_your_module
Specifically Appendix A: Dropdown Options

Resources