Magento:为所有产品添加新属性

问题描述 投票:0回答:3

我想为所有产品添加新属性。我已经用安装脚本槽完成了

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

$this->addAttribute('catalog_product','test2',array(
    'label'     => 'test2',
    'type'      => 'varchar',
    'visible'   => true,
    'required'  => false,
    'required'  => 0
));

但是我如何通过

向此属性添加值
$entityTypeId     = $installer->getEntityTypeId('catalog_product');
$attributeSetId   = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttributeGroup($entityTypeId, 'Default', 'test2', 0);
$installer->endSetup();
magento attributes entity-attribute-value
3个回答
10
投票

这是我用来创建自己的自定义产品属性的示例代码之一:-

$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */

$installer->startSetup();

$attrCode = 'test2';
$attrGroupName = 'Test Group';
$attrLabel = 'Test 2';
$attrNote = 'Test Note';

$objCatalogEavSetup = Mage::getResourceModel('catalog/eav_mysql4_setup', 'core_setup');
$attrIdTest = $objCatalogEavSetup->getAttributeId(Mage_Catalog_Model_Product::ENTITY, $attrCode);

if ($attrIdTest === false) {
    $objCatalogEavSetup->addAttribute(Mage_Catalog_Model_Product::ENTITY, $attrCode, array(
        'group' => $attrGroupName,
        'sort_order' => 7,
        'type' => 'varchar',
        'backend' => '',
        'frontend' => '',
        'label' => $attrLabel,
        'note' => $attrNote,
        'input' => 'text',
        'class' => '',
        'source' => '',
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => '0',
        'visible_on_front' => false,
        'unique' => false,
        'is_configurable' => false,
        'used_for_promo_rules' => true
    ));
}

$installer->endSetup();

这与这两篇文章的参考文献一起使用:-

此外,您还会发现我使用了数组键“

group
”来提及属性组名称,该新的自定义属性将驻留在其中。讽刺的是,在上面的代码示例中提到这个键,会自动在该 Magento 中找到的每个属性集中创建这个属性。

因此您不需要调用任何方法(如“

addAttributeToSet()
”)将此属性添加到所有属性集。

希望有帮助。


0
投票

在您的magento根目录中运行此脚本。(更改您需要的配置)

<?php  

require_once('app/Mage.php');
 Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();                   

$installer->addAttribute('catalog_product', 'snum', array(
             'label'             => 'Serial No',
             'type'              => 'int',
             'input'             => 'text',
             'backend'           => '',
             'frontend'          => '',
             'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
             'visible'           => true,
             'required'          => false,
             'user_defined'      => false,
             'searchable'        => false,
             'filterable'        => false,
             'comparable'        => false,
             'visible_on_front'  => true,
             'visible_in_advanced_search' => false,
             'unique'            => false
));

$installer->endSetup();

?>

用于删除产品属性

<?php  

require_once('app/Mage.php');
 Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
 $installer = new Mage_Sales_Model_Mysql4_Setup;
 $installer->startSetup();
 $installer->removeAttribute('catalog_product', 'snum');
 $installer->endSetup();

?>

0
投票

您可以将自定义属性添加到 magento 后端,如图所示。如果您将产品属性创建为模块,则可以轻松从一个数据库移动到另一个数据库。

?php
$this->startSetup();
$this->addAttribute(catalog_product, 'featured_product', array(
'group'         => 'General',
'input'         => 'select',
'type'          => 'text',
'label'         => 'Featured Product',
'backend'       => '',
'visible'       => true,
'required'      => false,
'visible_on_front' => true,
'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'source' => 'eav/entity_attribute_source_boolean',
'sort_order'        => 8,
));

$this->endSetup();
© www.soinside.com 2019 - 2024. All rights reserved.