创建Attribute 和 Attribute Set

目录

自从上次总结后,俺是忙得团团转,忙着找工作,忙着打代码,忙着忙着,一晃一个月就过去了。清明放假除了祭奠亲人外,顺便总结下最近的成果。最近一直在忙的项目是数据导入。从在我看来后台"很原始"的Zencart 平台迁移到扩展性强,性能很弱的Magentlo上。在迁移数据的过程中,遇到了需要创建Attibute Set的问题,在这里记录下我的解决方案。btw,在我看来,Mqgento的开发文挡匮乏得令人发指的程度,还有这么多优秀插件出现,这不得不得让人相信这是个奇迹!好了,废话不多说,上代码儿。

获取默认的Attribute Set 和Gourp Id

setType('catalog_product')->getTypeId();
//get default type id
//$entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();

//get Default Attribute Set id
$defaultAttributeSetID = Mage::getModel('eav/entity_attribute_set')->getCollection()
->setEntityTypeFilter($entityTypeId)
->addFiledToFilter('attribute_set_name','Default')
->getFirstItem()
->getAttributeSetId();
?>

创建自定义的Attribute Set

//create Attribute Set Name: Configurable;
//refer app/code/core/Mage/Adminhtml/controllers/Catalog/Product/SetController.php
// line 142
$model = Mage::getModel('eav/entity_attribute_set');
$entityTypeID = Mage::getModel('catalog/product')->getResource()->getTypeId();
$model->setEntityTypeId($entityTypeID);
$model->setAttributeSetName('Configurable');
$model->validate();
$model->save();
$model->initFromSkeleton($defaultAttributeSetID);
$model->save();

//get the Create Attribute Set ID and Group ID
$attributeSet = array();
$attributeSet['id'] = $model->getId();
$attributeSet['group_id'] = $model->load($attributeSet['id'])->getDeafultGroupId();
</pre>

### 创建自定义属性

<pre lang="php">//create attribute function
// refer app/code/core/Mage/Adminhtml/controllers/Ctalog/Product/AttribteController.php
// line 186
function createAttribute($code, $label, $attribute_type, $product_type ,
$deafultValue = array() , $attributeSet , $isRequired = '0' , $isConfigurable = 1){
        $data = array(
            'attribute_code' => $code,
            'is_global' => '1',
            'frontend_input' => $attribute_type,
            'default_value_text' => '',
            'default_value_yesno' => '0',
            'default_value_date' => '',
            'default_value_textarea' => '',
            'is_unique' => '0',
            'is_required' => $isRequired,
            'apply_to' => array($product_type),
            'is_configurable' => $isConfigurable,
            'is_searchable' => '0',
            'is_visible_in_advanced_search' => '0',
            'is_comparable' => '0',
            'is_used_for_price_rules' => '0',
            'is_wysiwyg_enabled' => '0',
            'is_html_allowed_on_front' => '1',
            'is_visible_on_front' => '0',
            'used_in_product_listing' => '0',
            'used_for_sort_by' => '0',
            'frontend_label' => array($label)
        );

        $model = Mage::getModel('catalog/resource_eav_attribute');

        if (!isset($data['is_configurable'])) {
            $data['is_configurable'] = 0;
        }
        if (!isset($data['is_filterable'])) {
            $data['is_filterable'] = 0;
        }
        if (!isset($data['is_filterable_in_search'])) {
            $data['is_filterable_in_search'] = 0;
        }

        if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
            $data['backend_type'] =
            $model->getBackendTypeByInput($data['frontend_input']);
        }

        $data['option']['value'] = $deafultValue;

        $model->addData($data);
    $typeId = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId();
        $model->setEntityTypeId($typeId);
        $model->setIsUserDefined(1);
    if($attributeSet && isset($attributeSet['id']) && isset($attributeSet['group_id'])){
            //set attibute to the Attribute Set
        $model->setAttributeSetId($attributeSet['id']);
            $model->setAttributeGroupId($attributeSet['group_id']);
    }

        try {
            $model->save();
            return $model->getId();
        }
        catch (Exception $e) {
            echo '

Sorry, error occured while trying to save the attribute. Error: '.$e->getMessage().'

';
        }
    }
// create select attribute
createAttribute('xbc_select','Xbc Select','select','simple');
//create text attribute
createAttribute('xbc_text','Xbc Text','text','grouped');
//create boolean attribute
createAttribute('xbc_bool','Xbc Bool','bollean','');
?>

以上代码在Magento Community 1.6.2测试通过

参考资料

  1. Programatically create attribute in Magento, useful for the “on the fly” import system
  2. Programmatically Adding Attributes and Attribute Sets
  3. Playing with Attribute Set in Magento