问题
I'm trying to add to the field list of the categories a new text field called "website_url".
I found many tutorials but none works on magento 1.7.
I created a new module as following :
-code\local\SaponeWebConcept\CategoriesAttributes\sql\categoriesattributes_setup\mysql4-install-0.1.0.php :
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_category', 'website_url', array(
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'Lien du site',
'input' => 'text',
'class' => '',
'source' => '',
'global' => 0,
'visible' => 1,
'required' => 0,
'user_defined' => 0,
'default' => '',
'searchable' => 0,
'filterable' => 0,
'comparable' => 0,
'visible_on_front' => 0,
'unique' => 0,
'position' => 1,
));
$installer->endSetup();
-code\local\SaponeWebConcept\CategoriesAttributes\etc\config.xml
<?xml version="1.0"?>
<config>
<modules>
<SaponeWebConcept_CategoriesAttributes>
<version>0.1.0</version>
</SaponeWebConcept_CategoriesAttributes>
</modules>
<global>
<resources>
<categoriesattributes_setup>
<setup>
<module>SaponeWebConcept_CategoriesAttributes</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</categoriesattributes_setup>
</resources>
</global>
</config>
And my module is correctly activated by this :
<SaponeWebConcept_CategoriesAttributes>
<active>true</active>
<codePool>local</codePool>
</SaponeWebConcept_CategoriesAttributes>
But when I access my category editing, I don't see any new field. What am I doing wrong ? The module is supposed to work immediatly if the cache is off, right ? Thanks in advance.
回答1:
I had the same issue. This is how I solved it:
app/code/local/yourmodulename/sql/yourcustomattribute_setup/mysql4-upgrade-0.0.9-0.1.3.php
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute("catalog_category", "your_attribute", array(
"type" => "varchar",
"backend" => "",
"frontend" => true,
"label" => "Your Attribute",
"input" => "text",
"class" => "",
"source" => "",
"global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
"visible" => true,
"required" => false,
"user_defined" => false,
"default" => "",
"searchable" => false,
"filterable" => false,
"comparable" => false,
"visible_on_front" => true,
"unique" => true,
"note" => ""
));
$installer->endSetup();
app/code/local/yourmodulename/etc/config.xml
<config>
<modules>
<Yournamespace_Yourmodulename>
<version>0.1.3</version>
</Yournamespace_Yourmodulename>
</modules>
<frontend>
<routers>
<yourmodulename>
<use>standard</use>
<args>
<module>Yournamespace_Yourmodulename</module>
<frontName>categoryattribute</frontName>
</args>
</yourmodulename>
</routers>
<category>
<collection>
<attributes>
<yourmodulename />
</attributes>
</collection>
</category>
<layout>
<updates>
<yourmodulename>
<file>categoryattribute.xml</file>
</yourmodulename>
</updates>
</layout>
</frontend>
<global>
<helpers>
<yourmodulename>
<class>Yournamespace_Yourmodulename_Helper</class>
</yourmodulename>
</helpers>
<blocks>
<yourmodulename>
<class>Yournamespace_Yourmodulename_Block</class>
</yourmodulename>
</blocks>
<models>
<yourmodulename>
<class>Yournamespace_Yourmodulename_Model</class>
<resourceModel>categoryattribute_mysql4</resourceModel>
</yourmodulename>
</models>
<resources>
<yourmodulenamee_setup>
<setup>
<module>Yournamespace_Yourmodulename</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</yourmodulename_setup>
<yourmodulename_write>
<connection>
<use>core_write</use>
</connection>
</yourmodulename_write>
<yourmodulename_read>
<connection>
<use>core_read</use>
</connection>
</yourmodulename_read>
</resources>
</global>
</config>
app/etc/modules/Your_modulename.xml
<?xml version="1.0"?>
<config>
<modules>
<Yournamespace_Yourmodulename>
<active>true</active>
<codePool>local</codePool>
<version>0.1.3</version>
</Yournamespace_Yourmodulename>
</modules>
</config>
Check you database to see if the sql has been installed under "Eav Attribute"
You custom category attribute should now be showing in the frontend!!!!
Now all you need to do is show the attribute on the frontened using:
<?php echo $_product->getYourattributeid(); ?>
回答2:
For those who have this kind of problems my solution in MAGENTO 1.8 whas only make sure that VERSION tag is setted.. if you dont put it cause mysql4-install-0.0.1.php never is called .
<config>
<modules>
<Yournamespace_Yourmodulename>
<active>true</active>
<codePool>local</codePool>
<version>0.0.1</version>
</Yournamespace_Yourmodulename>
</modules>
</config>
来源:https://stackoverflow.com/questions/13336112/magento-1-7-add-attribute-to-category