Magento 1.4.2 : errors when saving custom customer Attributes - magento

I can't save my custom customer attributes:
My Magento backend is blocked and I get this errors :
2011-08-03T12:27:36+00:00 ERR (3): Warning: include(Mage\Customer\Model\Attribute\Data\.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\xampp\htdocs\magento_test142_3\lib\Varien\Autoload.php on line 93
2011-08-03T12:27:36+00:00 ERR (3): Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'Mage\Customer\Model\Attribute\Data\.php' for inclusion (include_path='C:\xampp\htdocs\magento_test142_3\app\code\local;C:\xampp\htdocs\magento_test142_3\app\code\community;C:\xampp\htdocs\magento_test142_3\app\code\core;C:\xampp\htdocs\magento_test142_3\lib;.;C:\xampp\php\PEAR') in C:\xampp\htdocs\magento_test142_3\lib\Varien\Autoload.php on line 93
This is my module code:
in file mysql4-install-0.1.0.php:
<?php
$installer=new Mage_Customer_Model_Entity_Setup ('core_setup');
$installer->startSetup();
$installer->addAttribute('customer', 'kd_nr', array(
'label' => 'Kundennummer',
'visible' => true,
'required' => false,
'position' => 20,
));
Mage::getSingleton( 'eav/config' )
->getAttribute( 'customer', 'kd_nr' )
->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
->save();
$installer->endSetup();
and here is my config.xml file:
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Newattcustomer>
<version>0.1.1</version>
</Mycompany_Newattcustomer>
</modules>
<global>
<resources>
<newattcustomer_setup>
<setup>
<module>Mycompany_Newattcustomer</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</newattcustomer_setup>
</resources>
<fieldsets>
<customer_account>
<kd_nr><create>1</create><update>1</update></kd_nr>
</customer_account>
</fieldsets>
</global>
</config>
I use Magento 1.4.2
Please help to solve this problem.
Thanks a lot.
[edit]
Thanks a lot for help, the code of Nasaralla seems to be working, but I discover that my problem come from a YES/NO select: Here is the code for the select box:
$installer->addAttribute('customer', 'in_search', array(
'label' => 'Appear in search',
'type' => 'int',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
'visible' => true,
'required' => false,
));
Mage::getSingleton( 'eav/config' )
->getAttribute( 'customer', 'in_search' )
->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
->save();
It will be very helpful if anyone help me to solve the really problem
[/edit]

Get rid of this part in install script
Mage::getSingleton( 'eav/config' )
->getAttribute( 'customer', 'kd_nr' )
->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
->save();
In order to call eav_setup you do
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
and then you can updateAttribute like:
$setup->updateAttribute ($entityTypeId,$id,$field,$value = null,$sortOrder=null)
You can get the list of methods you can call in here:
Freegento-Eav-Model-entity-setup
Therefore your code, I think, will be replaced by:
$setup->updateAttribute('customer', 'kd_nr', 'used_in_forms', 'adminhtml_customer');

Ok if the above didn't work at all ... I think lets revert back to your original script and I think I can spot some fixes:
<?php
$installer=new Mage_Customer_Model_Entity_Setup ('core_setup');
$installer->startSetup();
$installer->addAttribute('customer', 'kd_nr', array(
'label' => 'Kundennummer',
'is_visible' => 1,
'is_required' => 0,
'sort_order' => 20,
));
$attr = Mage::getSingleton( 'eav/config' )->getAttribute( 'customer', 'kd_nr' );
$attr->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
$attr->save();
$installer->endSetup();
For details check the upgrade script mysql4-data-upgrade-1.4.0.0.7-1.4.0.0.8.php at core/Mage/Customer

Related

Magento Module DB Setup Script not running

Running Magento 1.9.0.2, I'm trying to add 2 new checkbox fields into the Address form via Module.
Problem: The database script is NOT executed (note the die() function on the first line)
Symptoms:
My module does not exist in core_resources table
database does not get updated
module is listed & enabled in System > Configuration > Advanced
log enabled, folder {base_dir}/var/log/ is 777, no log produced
I'm installing the module; is uploading the files to the folder & refresh the cache & frontend once triggers the install?
Question: What did I miss?
Here is the {base_dir}/app/code/local/Tdg/Check/etc/config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Tdg_Check>
<version>1.0.1</version>
</Tdg_Check>
</modules>
<admin>
<fieldsets>
<customer_dataflow>
<chk_commercial><billing>1</billing><shipping>1</shipping></chk_commercial>
<chk_residential><billing>1</billing><shipping>1</shipping></chk_residential>
</customer_dataflow>
</fieldsets>
</admin>
<global>
<models>
<check>
<class>Tdg_Check_Model</class>
</check>
</models>
<resources>
<check_setup>
<setup>
<module>Tdg_Check</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</check_setup>
<check_write>
<connection>
<use>core_write</use>
</connection>
</check_write>
<check_read>
<connection>
<use>core_read</use>
</connection>
</check_read>
</resources>
<fieldsets>
<sales_copy_order_billing_address>
<chk_commercial><to_order>*</to_order></chk_commercial>
<chk_residential><to_order>*</to_order></chk_residential>
</sales_copy_order_billing_address>
<sales_copy_order_shipping_address>
<chk_commercial><to_order>*</to_order></chk_commercial>
<chk_residential><to_order>*</to_order></chk_residential>
</sales_copy_order_shipping_address>
<sales_convert_quote_address>
<chk_commercial><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></chk_commercial>
<chk_residential><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></chk_residential>
</sales_convert_quote_address>
<sales_convert_order_address>
<chk_commercial><to_quote_address>*</to_quote_address></chk_commercial>
<chk_residential><to_quote_address>*</to_quote_address></chk_residential>
</sales_convert_order_address>
<customer_address>
<chk_commercial><to_quote_address>*</to_quote_address></chk_commercial>
<chk_residential><to_quote_address>*</to_quote_address></chk_residential>
</customer_address>
<checkout_onepage_billing>
<chk_commercial><to_customer>*</to_customer></chk_commercial>
<chk_residential><to_customer>*</to_customer></chk_residential>
</checkout_onepage_billing>
</fieldsets>
</global>
</config>
And the SQL Update file {base_dir}/app/code/Tdg/Check/sql/check_setup/mysql4-install-1.0.1.php:
<?php
die('Installing Module');
/* #var $installer Mage_Customer_Model_Entity_Setup */
$installer = $this;
$installer->startSetup();
/* #var $addressHelper Mage_Customer_Helper_Address */
$addressHelper = Mage::helper('customer/address');
$store = Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID);
/* #var $eavConfig Mage_Eav_Model_Config */
$eavConfig = Mage::getSingleton('eav/config');
// update customer address user defined attributes data
$attributes = array(
'chk_commercial' => array(
'label' => 'Chk Commercial',
'type' => 'int',
'input' => 'checkbox',
'default' => 0,
'is_user_defined' => 1,
'is_system' => 0,
'is_visible' => 1,
'sort_order' => 140,
'is_required' => 1,
'multiline_count' => 0,
'validate_rules' => array(
'max_text_length' => 1,
'min_text_length' => 1
),
),
'chk_residential' => array(
'label' => 'Chk Residential',
'type' => 'int',
'input' => 'checkbox',
'default' => 0,
'is_user_defined' => 1,
'is_system' => 0,
'is_visible' => 1,
'sort_order' => 141,
'is_required' => 1,
'multiline_count' => 0,
'validate_rules' => array(
'max_text_length' => 1,
'min_text_length' => 1
),
),
);
foreach ($attributes as $attributeCode => $data) {
$attribute = $eavConfig->getAttribute('customer_address', $attributeCode);
$attribute->setWebsite($store->getWebsite());
$attribute->addData($data);
$usedInForms = array(
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address'
);
$attribute->setData('used_in_forms', $usedInForms);
$attribute->save();
}
$installer->run("
ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `chk_commercial` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `chk_commercial` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `chk_residential` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `chk_residential` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
");
$installer->endSetup();
?>
Last, it's the Module definition file Tdg_Check.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Tdg_Check>
<active>true</active>
<codePool>local</codePool>
</Tdg_Check>
</modules>
</config>
In Magento module entry stored in code field like below:
yourmodulename_setup
Check for record like check_setup in code field of core_resource table.
So for run install script again you have to find module record in module and remove that record.
It will automatically run the script again.

Magento - Add field to actions in Catalog Price Rules

I want to add a custom field to the actions tab in the Catalog Price Rules section.
This is what I did:
I added these lines to the file app\code\core\Mage\Adminhtml\Block\Promo\Catalog\Edit\Tab\Actions.php
$fieldset->addField('custom_field', 'select', array(
'label' => 'Custom Field',
'title' => 'Custom Field',
'name' => 'custom_field',
'options' => array(
'1' => Mage::helper('catalogrule')->__('Yes'),
'0' => Mage::helper('catalogrule')->__('No'),
),
));
I changed the version to 1.6.0.4 in this file: app\code\core\Mage\CatalogRule\etc\config.xml
<Mage_CatalogRule>
<version>1.6.0.4</version>
</Mage_CatalogRule>
I created new file with the name app\code\core\Mage\CatalogRule\sql\catalogrule_setup\upgrade-1.6.0.3-1.6.0.4.php
$installer->startSetup();
$ruleProductTable = $installer->getTable('catalogrule/rule');
$columnOptions = array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_SMALLINT,
'UNSIGNED' => true,
'NULLABLE' => false,
'DEFAULT' => 0,
'COMMENT' => 'Custom Field',
);
$installer->getConnection()->addColumn($ruleProductTable, 'custom_field', $columnOptions);
$installer->endSetup();
Then, I logged in to the admin panel and tried to edit one of the catalog price rules.
I saw my new field in the actions tab - great!
But when I click "Save", the changes that I made in that field, was not saved.
I logged in to the phpmyadmin and went to catalogrule table.
In this table I can see the new field custom_field, with the value 0 (default) - so It really didn't save the changed.
My question is what I did wrong?
Why the changed in the custom_field didn't saved in the DB?
Thanks
I'm not sure why it isn't working for you my guess is your installer isn't being called. I managed the modification through a module and local edits to future proof the change.
Directory structure:
/app/code/local/custom/promo
/app/code/local/custom/promo/etc/
/app/code/local/custom/promo/sql
/app/code/local/custom/promo/sql/promo_setup
/app/code/core/local/Adminhtml/Block/Promo/Catalog/Edit/Tab
add file /app/code/local/custom/promo/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Custom_Promo>
<version>1.0</version>
</Custom_Promo>
</modules>
<global>
<resources>
<promo_setup>
<setup>
<module>Custom_Promo</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
</promo_setup>
</resources>
</global>
</config>
add file /app/code/local/custom/promo/sql/promo_setup/install-1-0.php
/* #var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();
$ruleProductTable = $installer->getTable('catalogrule/rule');
$columnOptions = array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_BOOLEAN,
'NULLABLE' => false,
'COMMENT' => 'Custom Promo',
);
$installer->getConnection()->addColumn($ruleProductTable, 'custom_promo', $columnOptions);
$installer->endSetup();
Clear caches and verify the column is added by looking at the catalog_rule table
SELECT * FROM catalogrule;
Finally take a copy of /app/code/core/Mage/Adminhtml/Block/Promo/Catalog/Edit/Tab/Main.php and create the same file /app/code/core/local/Adminhtml/Block/Promo/Catalog/Edit/Tab/Main.php
Edit the file and add the field before $form->setValues($model->getData());
//Custom Promo
$fieldset->addField('custom_promo', 'select', array(
'label' => 'Custom Promo',
'title' => 'Custom Promo',
'name' => 'custom_promo',
'options' => array(
'1' => Mage::helper('catalogrule')->__('Yes'),
'0' => Mage::helper('catalogrule')->__('No'),
),
));

Magento - Add custom attribute to order

I'm trying to add a custom field to my orders. At this moment, I found the post bellow that helped me to create such attribute in my database:
http://fabrizioballiano.net/2011/11/15/create-a-custom-order-attribute-in-magento/
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;
$attribute = array(
'type' => 'int',
'backend_type' => 'text',
'frontend_input' => 'text',
'is_user_defined' => true,
'label' => 'My Label',
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => true,
'default' => 0
);
$installer->addAttribute('order', 'special_attribute', $attribute);
$installer->endSetup();
After executing the code above and creating several orders, I'm able to loop through all orders and see the default value to the every order.
The question is, how can I store the data I want in this field? How can I retrieve such data?
Thanks!
Add this to the gobal scope in config.xml. Then simply set the attribute in the quote - it gets automagically transferred to the order in the quote to order conversion process.
<global>
...
<fieldsets>
<sales_convert_quote>
<your_special_attribute>
<to_order>*</to_order>
</your_special_attribute>
</sales_convert_quote>
</fieldsets>
...
</global>
You can retrieve/set the attribute at any time via the magic getter/setter e.g.
$quote->getYourSpecialAttribute()
$order->getYourSpecialAttribute()
$quote->setYourSpecialAttribute()

magento: add attibute to product, but not show up when editing the product

As per bens comment and answer I updated my script, comments indicate changes
{Magento 1.4.0.1} currently i have an installer script:
$installer = $this;
$installer->startSetup();
//commented out to use factory method
//$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$setup = Mage::getResourceModel('catalog/setup','core_setup');
if(!$setup->getAttribute('catalog_product','attribute_code')){
$newFields = array(
'attribute_code' => array(
'type' => 'text',
'label' => 'Attribute Label',
//added visible option
'visible' => false,
),
);
$entities = array(
'catalog_product',
);
foreach($newFields as $attributeName => $attributeDefs) {
foreach ($entities as $entity) {
$setup->addAttribute($entity, $attributeName, array(
'type' => $attributeDefs['type'],
'label' => $attributeDefs['label'],
//added visible option
'visible' => $attributeDefs['visible'],
'class' => '',
'required' => false,
));
}
}
}
$installer->endSetup();
It works wonderfully! Except the attribute shows up in the General attribute group when editing the product and I don't want it to show up at all (its a secret ninja attribute) is there something I'm doing wrong? or perhaps something I should be doing to let Magento know not its not supposed to show up?
Using addAttribute() you can set the 'visibleindex tofalse. UsingupdateAttribute()` you should do the following:
$setup->updateAttribute('catalog_product','attr_code','is_visible',false);
Let me know if I'm wrong.

Magento: can't set the default value of custom order attribute using installation script

I've added custom attribute to orders using mysql4-install-1.0.0.php in my module:
$installer = $this;
$installer->startSetup();
$installer->addAttribute('order', 'custom_status', array(
'type' => 'varchar',
'label' => 'My Status',
'note' => '',
'default' => "my_default_value",
'visible' => false,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'unique' => false
));
It works - when I look at sales_flat_order table, I see new varchar field *custom_status* in the table. But the default value is NULL instead of "my_default_value" as expected there. Any ideas, why?
PS. Installation script is really executed, I reset all to initial state each time.
UPD. config.xml
<resources>
<mymodule_setup>
<setup>
<module>Company_Mymodule</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</mymodule_setup>
***
Magento Sales module has pseudo emulation of old EAV functionality. It takes only type property from your array to create a column in the database. Also it uses "grid" property for determining is it required to make the same column in grid representation table.
BTW, previous sales module that was based on EAV, was not using default, label, required and other properties as well. If you want to set a default value for this attribute you need create an observer for order before save event and set this data in it if field is empty.
Make sure you have it like this in your config.xml:
<resources>
<customattr_setup>
<setup>
<module>Yourcompany_ModuleName</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customattr_setup>
</resources>
The <class>Mage_Sales_Model_Mysql4_Setup</class> is extremely important!
Then in the install script mysql4-install-0.1.0.php:
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute(
'order',
'customattr',
array(
'type' => 'float', // or whatever you want here...
'grid' => false
)
);
$installer->endSetup();
?>

Resources