Magento,sql,setup的方式添加一个select属性,add attribute selet by module sql

1.

新建一个模块

Resource里面这么定义:

 <resources>
            <wailian_setup>
                <setup>
                    <module>Lon_Wailian</module>
                     <class>Mage_Catalog_Model_Resource_Setup</class>
                  </setup>
                 <connection>
                    <use>core_setup</use>
                </connection>
            </wailian_setup>
    </resources>  

注意:在<setup>里面

<class>Mage_Catalog_Model_Resource_Setup</class>,这个一定要填写正确
Mage_Catalog_Model_Resource_Setup


2.数据库文件

<?php

$installer = $this;
$installer->startSetup();


//属性的Attribute Code,只能用字母和下划线,中间可以加入数字
$attribute_name = 'ccc';
//属性集中的group,和general等一个东西,是后台产品详细页面左侧的标签
$AttributeGroup = "sss";

if(! ($this instanceof Mage_Catalog_Model_Resource_Setup) )
{
    throw new Exception("Resource Class needs to inherit from " .
    "Mage_Catalog_Model_Resource_Setup for this to work");
}
//属性定义
$attr = array (
  'attribute_model' => NULL,
  'backend' => '',
  'type' => 'int',
  'table' => '',
  'frontend' => '',
  'input' => 'select',  //Catalog Input Type for Store Owner
  'label' => 'bbb',  //label
  'frontend_class' => '',
  'source' => '',
  'required' => '0',  //Values Required
  'user_defined' => '1',
  'default' => '',
  'unique' => '0',
  'note' => '',
  'input_renderer' => NULL,
  'global' => '1',  //Scope
  'visible' => '1',
  'searchable' => '1', //Use in Quick Search
  'filterable' => '1', //Use In Layered Navigation
  'comparable' => '1', //Comparable on Front-end
  'visible_on_front' => '0',
  'is_html_allowed_on_front' => '0',  //Allow HTML Tags on Frontend
  'is_used_for_price_rules' => '1',
  'filterable_in_search' => '1', //Use In Search Results Layered Navigation
  'used_in_product_listing' => '0', //Used in Product Listing
  'used_for_sort_by' => '0', //Used for Sorting in Product Listing
  'is_configurable' => '1', //Use To Create Configurable Product
  'apply_to' => 'simple', //Apply To *
  'visible_in_advanced_search' => '1', //Use in Advanced Search
  'position' => '1', //Position
  'wysiwyg_enabled' => '0',
  'used_for_promo_rules' => '1',
  'option' => 
  array (
    'values' => 
    array (
      0 => 'Green',
      1 => 'Silver',
      2 => 'Black',
      3 => 'Blue',
      4 => 'Red',
      5 => 'Pink',
      6 => 'Magneta',
      7 => 'Brown',
      8 => 'White',
      9 => 'Gray',
    ),
  ),
);
//新建属性
$this->addAttribute('catalog_product',$attribute_name,$attr);

//下面的部分为把新建的属性加入属性集
//得到属性的id
$attributeId = $installer->getAttributeId(
    'catalog_product',
    $attribute_name
);
 //得到属性集的id
$defaultSetId = $installer->getAttributeSetId('catalog_product', 'default');
 //在属性集里面新建group,也就是打开产品后,左边的标签,譬如general
$installer->addAttributeGroup(
    'catalog_product',
    $defaultSetId,
    $AttributeGroup
);
 
//得到新建的group的id
$groupId = $installer->getAttributeGroup(
    'catalog_product',
    $defaultSetId,
    $AttributeGroup,
    'attribute_group_id'
);
 
//把属性加入到属性集,并在上面新建的group中
if ($attributeId > 0) {
    $installer->addAttributeToSet(
        'catalog_product',
        $defaultSetId,
        $groupId,
        $attributeId
    );
}



//OK新建一个select属性到这里就完成了

	
$installer->endSetup(); 










清空缓存,运行,就会建立起来属性了


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值