Magento 1.7添加属性到类别

时间:2012-11-11 22:25:10

标签: attributes categories magento-1.7

我正在尝试将类别的字段列表添加到名为“website_url”的新文本字段中。

我找到了许多教程,但没有一部分适用于magento 1.7。

我创建了一个新模块如下:

-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 \本地\ SaponeWebConcept \ CategoriesAttributes \等\ 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>

我的模块由此正确激活:

<SaponeWebConcept_CategoriesAttributes>
    <active>true</active>
    <codePool>local</codePool>
</SaponeWebConcept_CategoriesAttributes>

但是当我访问我的类别编辑时,我没有看到任何新字段。 我究竟做错了什么 ?如果缓存关闭,模块应该立即工作,对吧? 提前谢谢。

2 个答案:

答案 0 :(得分:1)

我有同样的问题。这就是我解决它的方法:

应用程序/代码/本地/ yourmodulename / SQL / yourcustomattribute_setup / mysql4升级-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();

应用程序/代码/本地/ 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> 

应用程序的/ etc /模块/ 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>

检查数据库以查看是否已在“Eav属性”

下安装了sql

您的自定义类别属性现在应该显示在前端!!!!

现在您需要做的就是使用以下方法显示前端的属性:

<?php echo $_product->getYourattributeid(); ?>

答案 1 :(得分:0)

对于那些遇到此类问题的人,我在MAGENTO 1.8中的解决方案只能确保设置VERSION标签..如果你不放它,因为mysql4-install-0.0.1.php永远不会被调用。

<config>
    <modules>
        <Yournamespace_Yourmodulename>
            <active>true</active>
            <codePool>local</codePool>
            <version>0.0.1</version>
        </Yournamespace_Yourmodulename>
    </modules>
</config>