我试图通过运行此代码按照magento的顺序添加新字段:
<?php
/* @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(
'district_id' => array(
'label' => 'ID District',
'type' => 'varchar',
'input' => 'text',
'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' => 255,
'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 `district_id` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `district_id` VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
");
$installer->endSetup();
?>
但是当我登录管理员并点击查看客户详细信息时。 它有错误:在布尔值上调用成员函数setAttribute() 这里有什么问题?
答案 0 :(得分:0)
您需要更改安装程序脚本
$installer = $this;
$installer->startSetup();
$this->addAttribute('customer_address', 'district_id', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'District Id',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'visible_on_front' => 1
));
Mage::getSingleton('eav/config')
->getAttribute('customer_address', 'district_id')
->setData('used_in_forms', array('customer_register_address',
'customer_address_edit',
'adminhtml_customer_address'))
->save();
$installer->endSetup();
答案 1 :(得分:0)
Assuming that you want to add my_custom_input_field to your order and add a field to your admin create order page (to add the field to the frontend you just need to add the input field to the front template and double check the observer)
In /app/code/local/MageIgniter/CustomOrderStatus/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<MageIgniter_CustomOrderStatus>
<version>1.1</version>
</MageIgniter_CustomOrderStatus>
</modules>
<global>
<fieldsets>
<sales_convert_quote>
<my_custom_input_field><to_order>*</to_order></my_custom_input_field>
</sales_convert_quote>
<sales_convert_order>
<my_custom_input_field><to_quote>*</to_quote></my_custom_input_field>
</sales_convert_order>
</fieldsets>
<helpers>
<customorderstatus>
<class>MageIgniter_CustomOrderStatus_Helper</class>
</customorderstatus>
</helpers>
<models>
<customorderstatus>
<class>MageIgniter_CustomOrderStatus_Model</class>
<resourceModel>customorderstatus_mysql4</resourceModel>
</customorderstatus>
</models>
<resources>
<customorderstatus_setup>
<setup>
<module>MageIgniter_CustomOrderStatus</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customorderstatus_setup>
<customorderstatus_write>
<connection>
<use>core_write</use>
</connection>
</customorderstatus_write>
<customorderstatus_read>
<connection>
<use>core_read</use>
</connection>
</customorderstatus_read>
</resources>
<events>
<adminhtml_sales_order_create_process_data_before>
<observers>
<customorderstatus>
<type>singleton</type>
<class>customorderstatus/observer</class>
<method>saveCustomData</method>
</customorderstatus>
</observers>
</adminhtml_sales_order_create_process_data_before>
</events>
<blocks>
<customorderstatus>
<class>MageIgniter_CustomOrderStatus_Block</class>
</customorderstatus>
</blocks>
</global>
</config>
In
/app/code/local/MageIgniter/CustomOrderStatus/sql/customorderstatus_setup/mysql4-install-1.1.php
<?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();
In /app/code/local/MageIgniter/CustomOrderStatus/Model/Observer.php
class MageIgniter_CustomOrderStatus_Model_Observer
{
public function saveCustomData($event)
{
$quote = $event->getSession()->getQuote();
$quote->setData('my_custom_input_field', $event->getRequestModel()->getPost('my_custom_input_field'));
return $this;
}
}
(You should avoid make changes to core default - you should do some research on way around this)
In /app/design/adminhtml/default/default/template/sales/order/view/info.phtml
<?php if($_order->getMyCustomInputField()): ?>
<tr>
<td class="label"><label><?php echo Mage::helper('sales')->__('My Custom Input Field') ?></label></td>
<td class="value"><strong><?php echo $_order->getMyCustomInputField() ?></strong></td>
</tr>
<?php endif; ?>
In /app/design/adminhtml/default/default/template/sales/order/create/form/account.phtml
(to add to frontend change the value="<?php echo Mage...>")
<input id="my_custom_input_field" name="my_custom_input_field" value="<?php echo Mage::getSingleton('adminhtml/session_quote')->getQuote()->getMyCustomInputField() ?>" class="input-text" type="text">