【问题标题】:assign new attribute to all attribute sets by coding in magento通过在 magento 中编码为所有属性集分配新属性
【发布时间】:2012-09-19 09:57:42
【问题描述】:

我创建了一个新属性(“例如 my_attribute”)。 Magento 的商店已经有很多属性集(大约 20 个)。 如果我手动将属性添加到集合中,这是一个非常耗时的过程,因为我的服务器非常慢。 我想以编程方式将此新属性(“my_attribute”)分配给所有属性集。

谁能帮帮我?

提前致谢。

【问题讨论】:

    标签: magento attributes magento-1.7


    【解决方案1】:

    这很简单。在自定义模块设置脚本中:

    $installer = Mage::getResourceModel('catalog/setup','default_setup');
    $installer->startSetup();
    
    $installer->addAttribute(
         'catalog_product',
         'your_attribute_code',
         array(
             'label' => 'Attribute Label',
             'group' => 'General', // this will add to all attribute sets in the General group
             // ...
         )
    )
    
    $installer->endSetup();
    

    有关其他实用程序,请参阅Mage_Eav_Model_Entity_Setup

    【讨论】:

      【解决方案2】:

      这里有一些快速、肮脏和未经测试的东西:)

      <?php
      $attributeId = ID_HERE;
      $installer = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
      $entityType = Mage::getModel('catalog/product')->getResource()->getEntityType();
      $collection = Mage::getResourceModel('eav/entity_attribute_set_collection')
      ->setEntityTypeFilter($entityType->getId());
      
      foreach ($collection as $attributeSet) {
          $attributeGroupId = $installer->getDefaultAttributeGroupId('catalog_product',     $attributeSet->getId());
          $installer->addAttributeToSet('catalog_product', $attributeSet->getId(), $attributeGroupId, $attributeId);
      }
      

      【讨论】:

      • 我刚刚对此进行了测试,但它确实有效,但请确保需要 app/Mage.php 文件。
      【解决方案3】:
      $installer = Mage::getResourceModel('catalog/setup','default_setup');
      $installer->startSetup();
      
      $attributeCode = 'my_attribute';
      $entity = Mage_Catalog_Model_Product::ENTITY;
      
      // create a new attribute if it doesn't exist
      $existingAttribute = $installer->getAttribute($entity, $attributeCode);
      if (empty($existingAttribute)) {
          $installer->addAttribute($entity, $attributeCode, array(<configure your attribute here>));
      }
      
      $attributeId = $installer->getAttributeId($entity, $attributeCode);
      
      // add it to all attribute sets' default group
      foreach ($installer->getAllAttributeSetIds($entity) as $setId) {
          $installer->addAttributeToSet(
              $entity, 
              $setId, 
              $installer->getDefaultAttributeGroupId($entity, $setId), 
              $attributeId
          );
      }
      
      $installer->endSetup();
      

      【讨论】:

        【解决方案4】:

        由于没有为属性分配任何属性集,因此您可以删除您创建的该属性,然后以编程方式创建所需的属性。

        在 Magento wiki 的 Programmatically Adding Attributes and Attribute Sets Section 中,它将 createAttribute 描述为可以解决您的问题的函数,因为它会创建属性并将它们分配给属性集。

        希望这会有所帮助!

        【讨论】:

          【解决方案5】:

          此外,从上述类扩展而来的Mage_Eav_Model_Entity_SetupMage_Catalog_Model_Resource_Setup 具有创建属性、集合和组所需的所有方法。它们相当简单,它们将帮助您了解应该如何正确执行它并防止您编写错误或冗余的代码。我发现大多数文章都在 Internet 上流传,甚至 Magento 自己的 wiki 条目的代码都写得不好。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多