你是否曾经需要创建magento自定义属性源类型?下面的小示例可能会对你有所帮助。 首先你需要创建sql安装文件,这个文件将把源类型的属性加入到你的系统中。源类型定义在数组: ‘source’ => ‘sourcetype/attribute_source_type’。 startSetup(); $installer->addAttribute('catalog_product', 'product_type', array( 'group' => 'Product Options', 'label' => 'Product Type', 'note' => '', 'type' => 'int', //backend_type 'input' => 'select', //frontend_input 'frontend_class' => '', 'source' => 'sourcetype/attribute_source_type', 'backend' => '', 'frontend' => '', 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE, 'required' => true, 'visible_on_front' => false, 'apply_to' => 'simple', 'is_configurable' => false, 'used_in_product_listing' => false, 'sort_order' => 5, )); $installer->endSetup(); 接着你需要创建自定义源类型或者名为Alwayly_Sourcetype_Model_Attribute_Source_Type的类,下面是我的例子: class Alwayly_Sourcetype_Model_Attribute_Source_Type extends Mage_Eav_Model_Entity_Attribute_Source_Abstract { const MAIN = 1; const OTHER = 2; public function getAllOptions() { if (is_null($this->_options)) { $this->_options = array( array( 'label' => Mage::helper('sourcetype')->__('Main Product'), 'value' => self::MAIN ), array( 'label' => Mage::helper('sourcetype')->__('Other Product'), 'value' => self::OTHER ), ); } return $this->_options; } public function toOptionArray() { return $this->getAllOptions(); } public function addValueSortToCollection($collection, $dir = 'asc') { $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID; $valueTable1 = $this->getAttribute()->getAttributeCode() . '_t1'; $valueTable2 = $this->getAttribute()->getAttributeCode() . '_t2'; $collection->getSelect()->joinLeft( array($valueTable1 => $this->getAttribute()->getBackend()->getTable()), "`e`.`entity_id`=`{$valueTable1}`.`entity_id`" . " AND `{$valueTable1}`.`attribute_id`='{$this->getAttribute()->getId()}'" . " AND `{$valueTable1}`.`store_id`='{$adminStore}'", array() ); if ($collection->getStoreId() != $adminStore) { $collection->getSelect()->joinLeft( array($valueTable2 => $this->getAttribute()->getBackend()->getTable()), "`e`.`entity_id`=`{$valueTable2}`.`entity_id`" . " AND `{$valueTable2}`.`attribute_id`='{$this->getAttribute()->getId()}'" . " AND `{$valueTable2}`.`store_id`='{$collection->getStoreId()}'", array() ); $valueExpr = new Zend_Db_Expr("IF(`{$valueTable2}`.`value_id`>0, `{$valueTable2}`.`value`, `{$valueTable1}`.`value`)"); } else { $valueExpr = new Zend_Db_Expr("`{$valueTable1}`.`value`"); } $collection->getSelect() ->order($valueExpr, $dir); return $this; } public function getFlatColums() { $columns = array( $this->getAttribute()->getAttributeCode() => array( 'type' => 'int', 'unsigned' => false, 'is_null' => true, 'default' => null, 'extra' => null ) ); return $columns; } public function getFlatUpdateSelect($store) { return Mage::getResourceModel('eav/entity_attribute') ->getFlatUpdateSelect($this->getAttribute(), $store); } } 如果安装正确地完成,进入Magento后台admin -> Catalog -> Product,你会看到一个名为“Product Options”的选项。 (责任编辑:最模板) |