How do you add custom attributes to Customer Groups in Magento? - php

We're on Magento CE 1.7.0.0, and we're trying to add new attributes to the Customer Group entities. We've successfully added custom attributes to Customers using the following install script:
<?php
$installer = $this;
$installer->startSetup();
$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$setup->addAttribute('customer', 'ussco_account_number', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'USSCO Account Number',
'note' => 'Leave blank for default',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => '',
'visible_on_front' => 0,
'source' => NULL,
));
Mage::getSingleton('eav/config')
->getAttribute('customer', 'ussco_account_number')
->setData('used_in_forms', array('adminhtml_customer'))
->save();
$installer->endSetup();
Has anyone had any luck doing something similar with Customer Groups, rather than customers?

If you take a look at the sql installer/update scripts you will find something like this:
$table = $installer->getConnection()
->newTable($installer->getTable('customer/customer_group'))
->addColumn('customer_group_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
'identity' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true,
), 'Customer Group Id')
->addColumn('customer_group_code', Varien_Db_Ddl_Table::TYPE_TEXT, 32, array(
'nullable' => false,
), 'Customer Group Code')
->addColumn('tax_class_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'unsigned' => true,
'nullable' => false,
'default' => '0',
), 'Tax Class Id')
->setComment('Customer Group');
As you can see its a simple mysql4 table and you simply need to add a column to the group. It is not EAV so you dont work with attributes on that one!
The new colum will not show up in the form or grid! You have to add this manually via observer or rewriting Mage_Adminhtml_Block_Customer_Group_Edit_Form or Mage_Adminhtml_Block_Customer_Group_Grid by adding something like this for e.g text field:
$fieldset->addField('your_column', 'text',
array(
'name' => 'Your_Column',
'label' => Mage::helper('customer')->__('Tax Class'),
'title' => Mage::helper('customer')->__('Tax Class'),
'class' => 'required-entry',
'required' => true
)
);

I 've had the same need to include a field in customer_group. I want to add here my sql script to add some custom fields into customer_group
$installer = $this;
$installer->startSetup();
$installer->getConnection()->addColumn($installer->getTable('customer_group'), 'my_custom_field1', 'varchar(100)');
$installer->getConnection()->addColumn($installer->getTable('customer_group'), 'my_custom_field2', 'varchar(100)');
$installer->endSetup();
Regards

Related

Add New Back Office Field In Prestashop

How can i add a new field in prestashop's back office?
Specific, i want to insert a text field in the BO: Orders->Statuses->Add New Order Status under the status name. Which files i have to modify in order to do that? Can anyone describes the full procedure?
Thanks
I am using Prestashop version 1.6.1.2 and added one text field using following steps. You need to make changes in core files. You have to add field in one table in database and do some changes in class and controller file.
Here are the steps to do the same. I have adde field 'my_custom_field'.
Add one field in order_state table
ALTER TABLE {YOUR_DB_PREFIX}order_state ADD my_custom_field VARCHAR(50) NOT NULL;
Change class file of order state. You need to define your field in file "classes/order/OrderState.php"
After code
public $deleted = 0;
add this code snipet
public $my_custom_field;
After code
'deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
add this code snipet
'my_custom_field' => array('type' => self::TYPE_STRING),
open "controllers/admin/AdminStatusesController.php" file and do following changes
in function initOrderStatutsList()
after this code
'name' => array(
'title' => $this->l('Name'),
'width' => 'auto',
'color' => 'color'
),
add this code
'my_custom_field' => array(
'title' => $this->l('My Custom Field'),
'width' => 'auto',
),
in function renderForm()
after this code
array(
'type' => 'text',
'label' => $this->l('Status name'),
'name' => 'name',
'lang' => true,
'required' => true,
'hint' => array(
$this->l('Order status (e.g. \'Pending\').'),
$this->l('Invalid characters: numbers and').' !<>,;?=+()##"{}_$%:'
)
),
add this code
array(
'type' => 'text',
'label' => $this->l('My Custom field'),
'name' => 'my_custom_field',
),
Do changes suggested here. Hope this helps you :)

How to add a WYSIWYG editor to Magento custom options

Im trying to implement a wysiwyg editor under text-option in Magentos custom options, but am failing there. I have already searched for several components, but can't get it together though.
I want that editor to appear in the field where the WYSIWYG Value-textarea is right now.
Other source either don't go into detail or aren't working for 1.9.1.
What I have so far: [WR is companyname and EPO is my module]
I've found this snippet, which is used to place a wysiwyg editor on a cms page within a _prepareForm function:
<?php
if (Mage::getSingleton('cms/wysiwyg_config')->isEnabled() && ($block = $this->getLayout()->getBlock('head'))) {
$block->setCanLoadTinyMce(true);
}
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save'),
'method' => 'post'
));
$fieldset = $form->addFieldset('base_fieldset', array(
'legend' => Mage::helper('wr_epo')->__("Some Information"),
'class' => 'fieldset-wide',
));
$wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config');
$fieldset->addField('description', 'editor', array(
'name' => 'description',
'label' => Mage::helper('wr_epo')->__('Description'),
'title' => Mage::helper('wr_epo')->__('Description'),
'style' => 'height: 600px;',
'wysiwyg' => true,
'required' => false,
'config' => $wysiwygConfig
));?>
I had an issue like this no to long ago (client wanted custom stuff in an option).
I dont know if this is exactly what you want. But heres what i did:
I made a php file in the root of my magento.
And then i added this code. I found it on stackoverflow. If anyone knows where it comes from be even better.
ini_set('display_errors',0);
require_once 'app/Mage.php';
Mage::app();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
function createNewAttributeSet($name) {
Mage::app('default');
$modelSet = Mage::getModel('eav/entity_attribute_set')
->setEntityTypeId(4) // 4 == "catalog/product"
->setAttributeSetName($name);
$modelSet->save();
$modelSet->initFromSkeleton(4)->save(); // same thing
}
// Replace your attribute name with "extra_info"
$setup->addAttribute('catalog_category', 'extra_info', array(
'group' => 'General Information',
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Extra Information',
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
'input' => 'textarea',
'class' => '',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => '',
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'unique' => 0,
'position' => 1,
));
$setup->updateAttribute('catalog_category', 'extra_info', 'is_wysiwyg_enabled', 1);
$setup->updateAttribute('catalog_category', 'extra_info', 'is_html_allowed_on_front', 1);
I do however Think you want to include
'wysiwyg_enabled' => true,
Rather than
'wysiwyg' => true,
(this is refering to your code you pasted earlier)
Other reading:
Things i found that helped with my issues:
https://www.atwix.com/magento/add-category-attribute/
https://docs.magento.com/m1/ce/user_guide/catalog/product-options-custom.html

Move a product attribute to a new group in magento admin

I created an attribute like so...
$installer = $this;
$installer->startSetup();
/* $installer Services_Issue_Model_Mysql4_Setup */
$installer->addAttribute('catalog_product', 'alice_id', array(
'backend' => '',
'frontend' => '',
'type' => 'text',
'visible' => true,
'label' => 'Alice Id',
'note' => 'Alice Id.',
'input' => 'text',
'unique' => true,
'source' => '',
'global' => true,
'visible' => true,
'required' => true,
'user_defined' => true,
'default' => '',
'visible_on_front' => true,
'apply_to' => 'simple,configurable,default',
'group' => 'Special Attributes',
'used_in_product_listing' => true,
'frontend_class' => '',
'class' => '',
'is_html_allowed_on_front' => true,
'searchable' => true
));
$installer->endSetup();
Now I need to move it to another group within the product information page. So I've tried this without any success.
$installer = $this;
$installer->startSetup();
/* $installer Services_Issue_Model_Mysql4_Setup */
$installer->updateAttribute('catalog_product', 'alice_id', 'note', 'Product SKU for Alice.com third-party cart & checkout.');
/* - move between groups not possible with updateAttribute - */
$installer->updateAttribute('catalog_product', 'alice_id', 'group', 'Additional Attributes');
$installer->endSetup();
Can anyone tell me how I can accomplish this?
You can use the addAttributeToGroup($entityType, $attributeSetId, $attributeGroupId, $attributeId, $sortOrder) method in Mage_Eav_Model_Entity_Setup to move an attribute to a different group. First, you'll need to get the set ID and group ID.
// ... start setup
// get default set id
$setId = $installer->getDefaultAttributeSetId('catalog_product');
// get group id by name "Additional Attributes"
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_group_collection');
foreach ($attributeSetCollection->getData() as $attributeGroupIndex) {
foreach ($attributeGroupIndex as $key => $value) {
if ($key === "attribute_group_name" and $value === "Additional Attributes") {
$groupId = $attributeGroupIndex["attribute_group_id"];
break 2;
}
}
}
// move attribute 'alice_id' to group 'Additional Attributes'
if (isset($setId) and isset($groupId)) {
$installer->addAttributeToGroup('catalog_product', $setId, $groupId, 'alice_id', 1000);
}
// ... end setup
If you know the name/code of your group, you can get it's group ID directly using getAttributeGroupID(). This example updates the group of attribute_code to be "Group Name" within the default attribute set.
// $installer->startSetup()
// ...
$eavSetup = new Mage_Eav_Model_Entity_Setup('core_setup');
$iDefaultAttrSetID = $eavSetup->getDefaultAttributeSetId('catalog_product');
$iAttributeID = $eavSetup->getAttributeId('catalog_product', 'attribute_code');
$iGroupID = $eavSetup->getAttributeGroupId('catalog_product', $iDefaultAttrSetID, 'Group Name');
$eavSetup->addAttributeToGroup('catalog_product', $iDefaultAttrSetID, $iGroupID, $iAttributeID);

How to set the source for a new select type customer address attribute in magento?

Here I'm trying to add a select type customer address attribute. Can I use an array to set the option values and labels?
$newAttributeName = "adt";
$attribute = array(
'type' => 'int',
'input' => 'select',
'label' => 'Ad TYPE',
'visible' => true,
'required' => true,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'visible_on_front' => true,
'source' =>
);
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer_address', $newAttributeName, $attribute);
Can someone give me a help with this?
This page has some information that might help answer your question. It's not about customer attributes, but it does discuss adding dropdown attributes to products and should point you in the right direction.
Set Dropdown Option Value of a Product

Adding Custom attribute in forms

I am using magento V1.5. I am working on Customer EAV & have tried to create another EAV module.
I have added few attributes in customer entity. Now MY requirement is those attributes must be editable from frontend as well as backend.
Please tell me how to add those attributes in forntend form (customer edit form). & Tell me what to do in backend to have those options to be editable.
I am thinking if there is a way in admin like in our Form.php we prepare form with adding elements to it. then we not need to write a code to create actual html. Magento automatically does it. SO idea is it must also load the new attributes in that was just added. (like they appear in product edit.)
Second Issue is, Can u guys tell me what should I write in my Grid.php >> prepareCollection (for other EAV module ). so that it must get all the attributes with their values ( or may be few )
here is something that I have in my Grid.php but its not working
protected function _prepareCollection()
{
$collection = Mage::getModel('pincodes/eavpincodes')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
& this is my collection file
class Namespace_Pincodes_Model_Resource_Eav_Mysql4_Eavpincodes_Collection extends Mage_Eav_Model_Entity_Collection_Abstract
{
protected function _construct()
{
$this->_init('pincodes/eavpincodes');
}
}
But its not returning anything in my grid
& here is my Attribute collection file
class Inkfruit_Pincodes_Model_Resource_Eav_Mysql4_Attribute_Collection extends Mage_Eav_Model_Mysql4_Entity_Attribute_Collection
{
public function _construct()
{
$this->_init('pincodes/resource_eav_attribute', 'eav/entity_attribute');
}
protected function _initSelect()
{
$this->getSelect()->from(array('main_table' => $this->getResource()->getMainTable()))
->where('main_table.entity_type_id=?', Mage::getModel('eav/entity')->setType('pincodes_eavpincodes')->getTypeId())
->join(
array('additional_table' => $this->getTable('pincodes/eavpincodes')),
'additional_table.attribute_set_id=main_table.attribute_id' // I think this sql need to be changed but I have no idea what it'll be
);
return $this;
}
}
Guys thank you so much. This forum has been specially very helpful to me
Regards
SAM
Ok guys I have created a separate module for customers that has one mysql4_install file & it goes as follows
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'profile_image', array(
'label' => 'Profile Image',
'type' => 'varchar',
'input' => 'file',
'visible' => true,
'required' => false,
'user_defined' => true,
));
$setup->addAttribute('customer', 'mobile', array(
'label' => 'Mobile Number',
'type' => 'int',
'input' => 'text',
'visible' => true,
'required' => false,
'user_defined' => true,
));
$installer->endSetup();
$installer->installEntities();
But when I hit the url for this module I see no error but these attributes are not in my database.
I also have created Entity_Setup file & it goes as follows I think its incorrect but I gave it a try
<?php
class Namespace_Customer_Entity_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',
'attributes' => array(
'profile_image' => array(
//the EAV attribute type, NOT a mysql varchar
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Profile Image',
'input' => 'file',
'class' => '',
'source' => '',
// store scope == 0
// global scope == 1
// website scope == 2
'global' => 0,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => false,
'unique' => false
),
'mobile' => array(
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Mobile Number',
'input' => 'text',
'class' => '',
'source' => '',
'global' => 0,
'visible' => true,
'required' => true,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'comparable' => false,
'visible_on_front' => false,
'unique' => false
),
),
)
);
}
}
But I see nothing, not any new attribute in database.
Can u guys help here whats wrong??
Thanks
Guys here is My solution. the above answers worked for adding attribute to database only
My question has three parts.
1. Adding attributes to entity in database.
Ans. the above install scripts worked. using a Setup.php & mysql_install/ mysql_upgrade script.
2. Attribute should be editable from Frontend
Ans. We need to modify the file app/design/fontend/default//template/customer/form/edit.phtml
3. Attribute must be editable from Backend
Ans. for that we need to add this snippet in our config.xml
<global>
<fieldsets>
<customer_account>
<create>1</create><update>1</update>
</customer_account>
</fieldsets>
</global>
this will do everything
Hope this will help somebody
This worked for me, notice the additional lines:
<?php
class Millena_CustomerExportAdditions_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' => 'millena_customerExportAdditions/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
)
)
)
);
}
}
Install script is simply:
<?php
$installer = $this;
$installer->installEntities();
Having same issue here. Using addAttribute in 1.5.0.1 seems to add it into database but customer admin won't render it.
This method works fine with product and category attributes. And in 1.4.0.0 it works for customers too.
In Magento chagelog there is bullet point under improvements about customer attribute rendering. I'll start checking it next.

Categories