How to use custom customer variables in transactional email templates in magento? - 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.

Related

Magento Custom Order Attribute

I am trying to add a custom attribute field to the sales_flat_order table in the database for Magento 1.8+. Do I need to create a New Admin theme?
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute("order", "my_custom_input_field", array("type"=>"varchar"));
$installer->addAttribute("quote", "my_custom_input_field", array("type"=>"varchar"));
$installer->endSetup();
Try this:
$installer->addAttribute(Mage_Sales_Model_Order::ENTITY, 'THIS_IS_THE_CODE', array(
'group' => 'General',
'type' => 'varchar',
'default' => '0',
'input' => 'text',
'label' => 'My pretty pretty label',
'source' => '',
'visible' => true,
'required' => false,
'visible_on_front' => false,
'user_defined' => false
));
$installer->addAttribute(Mage_Sales_Model_Quote::ENTITY, 'THIS_IS_THE_CODE', array(
'group' => 'General',
'type' => 'varchar',
'default' => '0',
'input' => 'text',
'label' => 'My pretty pretty label',
'source' => '',
'visible' => true,
'required' => false,
'visible_on_front' => false,
'user_defined' => false
));

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.

Add category attribute with WYSIWYG enabled

I'm following this tutorial: http://www.atwix.com/magento/add-category-attribute/
All is working well, attributes are added to categories, but without the WYSIWYG button below the field. WYSIWYG is enabled in System > Config > Content Management.
$this->startSetup();
$this->addAttribute('catalog_category', 'custom_att', array(
'group' => 'General',
'input' => 'textarea',
'type' => 'text',
'label' => 'My attribute',
'backend' => '',
'visible' => true,
'required' => false,
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$this->endSetup();
Whatever I try, WYSIWYG is not enabled for my attributes. Can anyone help? Or maybe there is a workaround for this?
EDIT: I searched other posts but all say that this code should add the WYSIWYG:
'wysiwyg_enabled' => true,
but it doesn't.
Tried to accomplish same task today and searching through magento code managed to complete my task with this code:
$productEntityTypeId = $installer->getEntityTypeId('catalog_product');
$installer->addAttribute($productEntityTypeId, 'some_text', array(
'group' => 'General',
'input' => 'textarea',
'type' => 'text',
'label' => 'Some Text',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$installer->updateAttribute($productEntityTypeId, 'some_text', 'is_wysiwyg_enabled', 1);
$installer->updateAttribute($productEntityTypeId, 'some_text', 'is_html_allowed_on_front', 1);
This works:
$installer->updateAttribute('catalog_category', 'certifications', 'is_wysiwyg_enabled', 1);
$installer->updateAttribute('catalog_category', 'certifications', 'is_html_allowed_on_front', 1);
This worked for me:
$installer->addAttribute('catalog_category', 'short_description', array(
'type' => 'varchar',
'label' => 'Short Description',
'input' => 'textarea',
'default' => '',
'sort_order' => 1,
'required' => false,
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'group' => 'General Information',
));
Notice the following three entries:
'wysiwyg_enabled' => true,
'visible_on_front' => true,
'is_html_allowed_on_front' => true,
Using Magento CE 1.9.2.0.
Create php file in magento root directory and paste below code and run it from browser:-
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);

Drop down attribute in category - Magento

I need a dropdown attribute in category. I tried to do this in installer:
$installer = $this;
$installer->startSetup();
$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttribute('catalog_category', 'priority', array(
'type' => 'varchar',
'label' => 'Priority2',
'input' => 'select',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'option' => array(
'value' => array(
0 => array(''),
1 => array('normal'),
2 => array('grouping'),
3 => array('highlighted'),
4 => array('trendy'),
),
),
'default' => array(0),
));
$installer->addAttributeToGroup(
$entityTypeId, $attributeSetId, $attributeGroupId, 'priority', '11'
);
$attributeId = $installer->getAttributeId($entityTypeId, 'priority');
$installer->run("
INSERT INTO `{$installer->getTable('catalog_category_entity_int')}`
(`entity_type_id`, `attribute_id`, `entity_id`, `value`)
SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
FROM `{$installer->getTable('catalog_category_entity')}`;
");
Mage::getModel('catalog/category')
->load(1)
->setImportedCatId(0)
->setInitialSetupFlag(true)
->save();
Mage::getModel('catalog/category')
->load(2)
->setImportedCatId(0)
->setInitialSetupFlag(true)
->save();
$installer->endSetup();
but it doesn't work. Nothing shows up in Catalog->categories->Manage Categories. I can create only textfield attribute like this:
$installer->addAttribute('catalog_category', 'priority', array(
'type' => 'varchar',
'label' => 'Priority2',
'input' => 'text',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
));
Do you have any ideas?
Thx for help.
EDIT.
I also tied SQL query like this:
INSERT INTO eav_attribute (entity_type_id, attribute_code, attribute_model, backend_model, backend_type, backend_table, frontend_model, frontend_input, frontend_label, frontend_class, source_model, is_required, is_user_defined, default_value, is_unique, note) VALUES
(9, 'priority', NULL, 'NULL', 'int', 'NULL', 'NULL', 'select', 'Priority', NULL, 'eav/entity_attribute_source_table', 1, 0, 0, 0, '');
INSERT INTO eav_attribute_option (option_id,attribute_id,sort_order) VALUES
(1500,282,0),
(1501,282,1),
(1502,282,2),
(1503,282,3),
(1504,282,4);
INSERT INTO eav_attribute_option_value (value_id,option_id,store_id,value) VALUES
(201,1500,0,''),
(202,1501,0,'normal'),
(203,1502,0,'grouping'),
(204,1503,0,'highlighted'),
(205,1504,0,'trendy');
INSERT INTO eav_entity_attribute (entity_type_id, attribute_set_id, attribute_group_id, attribute_id, sort_order) VALUES
(9, 12, 7, (SELECT atribute_id FROM eav_attribute WHERE attribute_code='priority'), 2);
It's possible using module installer:
$installer->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'myattribute', array(
'type' => 'varchar',
'label' => 'My attribute',
'source' => 'module/category_attribute_myattribute',
'backend' => 'module/category_attribute_backend_myattribute',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'input' => 'select',
'visible' => true,
'required' => true,
'user_defined' => false,
'used_in_product_listing' => false,
'group' => 'Group name',
'default' => Module_Catalog_Model_Category_Attribute_Myattribute::DEFAULT_Groupname,
));
Hope it helps someone ;)
Please refer the tutorial for file structure and step by step explanation.
http://www.pearlbells.co.uk/how-to-add-custom-dropdown-attribute-to-magento-category-section/
<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'custom_dropdown', array(
'group' => 'General Information',
'input' => 'select',
'type' => 'text',
'label' => 'Custom Dropdown',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'source' => 'customfeaturedattribute/source_custom',
));
$this->endSetup();
And in the model class add the options.
public function getAllOptions()
{
$options = array(
1 => 'One',
2 => 'Two',
3 => 'Three'
);
return $options;
}

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/

Resources