【问题标题】:Add new values to a attribute option in magento将新值添加到 magento 中的属性选项
【发布时间】:2011-06-30 13:58:06
【问题描述】:

我正在尝试使用脚本向 magento 中的属性选项添加新值以加快流程,因为我有 2,000 多家制造商

【问题讨论】:

    标签: php magento attributes options entity-attribute-value


    【解决方案1】:

    这是我用来执行此任务的一段代码。创建一个自定义模块(使用ModuleCreator 作为工具),然后在 sql/modulename_setup 文件夹下创建一个mysql4-install-0.1.0.php。它应该包含以下内容(当然要适应您自己的数据!)。

    $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    
    $aManufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');
    $iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
    $aOption = array();
    $aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, 'manufacturer');
    
    for($iCount=0;$iCount<sizeof($aManufacturers);$iCount++){
       $aOption['value']['option'.$iCount][0] = $aManufacturers[$iCount];
    }
    $installer->addAttributeOption($aOption);
    
    $installer->endSetup();    
    

    如果需要,请在 Magento wiki 上查看更多文档。

    如果您不想在自定义模块中执行此操作,您可以创建一个以以下开头的 php 文件:

    require_once 'app/Mage.php';
    umask(0);
    Mage::app('default');
    

    【讨论】:

    • 如何在没有安装程序的情况下执行此操作?它是一个生产网站。
    • 错误信息?我可以向您保证它确实有效,我已经使用了该代码。使用安装程序是生产站点的首选方法。
    • @Tony 顺便说一句,该变量被称为installer 但这并不意味着它正在重新安装整个 Magento 堆栈。有一个方便类用于进行安装、升级和更改。这是您的任务的正确 Magento 方法。
    【解决方案2】:

    乔纳森的答案是正确的。但是如果你想在没有安装程序的情况下执行它,即在任何其他代码中,那么你可能会发现这很有帮助:

    $arg_attribute = 'manufacturer';
    $manufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo');
    
    $attr_model = Mage::getModel('catalog/resource_eav_attribute');
    $attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
    $attr_id = $attr->getAttributeId();
    
    $option['attribute_id'] = $attr_id;
    foreach ($manufacturers as $key=>$manufacturer) {
        $option['value'][$key.'_'.$manufacturer][0] = $manufacturer;
    }
    
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $setup->addAttributeOption($option);
    

    更多信息请见here

    【讨论】:

      【解决方案3】:

      重要!(希望这对某人有所帮助,因为我被这个问题困住了 2 小时)

      如果您使用特殊字符(例如 ä、ö、ü、ß、×、...),请务必正确编码!

      array_walk($manufacturers , create_function('&$val', '$val = utf8_encode($val);'));
      

      【讨论】:

      • 既然我们都有 PHP/5.3,一个更易读、更快的版本是$manufacturers = array_map('utf8_encode',$manufacturers);
      【解决方案4】:

      这里有一个简单快速的方法....

      删除一个选项

      /* My option value */
      $value = 'A value';
      /* Delete an option */
      $options = array();
      $entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
      $attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
      if ($attribute && $attribute->usesSource()) {
          $option_id = $attribute->getSource()->getOptionId($value); // Check Option ID from value...
          if ($option_id) {
              $options['delete'][$option_id] = true; 
              $attribute->setOption($options)->save();
          }
      }
      /* END ! */
      

      添加或更新选项

      /* Add/Update an option */
      $options = array();
      $entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID
      $attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code
      if ($attribute && $attribute->usesSource()) {
          $option_id = $attribute->getSource()->getOptionId($value); // Check Option ID...
          $options['order'][$option_id] = 10; // Can be removed... Set option order...
          $options['value'][$option_id] = array(
              0 => $value, // Admin Store - Required !
              1 => $value, // Store id 1 - If U want ! Can be removed
          );
          $attribute->setDefault(array($option_id)); /* If you want set option as default value */
          $attribute->setOption($options)->save(); /* That's all */
      }
      /* END ! */  
      

      【讨论】:

        【解决方案5】:

        我已经创建了一个函数来动态添加属性选项

        public function addAttributeOptions($attributeCode, $argValue)
        {
            $attribute = Mage::getModel('eav/config')
                ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
        
        
            if ($attribute->usesSource()) {
        
                $id = $attribute->getSource()->getOptionId($argValue);
                if ($id) 
                    return $id;
            }
        
            $value = array('value' => array(
                    'option' => array(
                            ucfirst($argValue),
                            ucfirst($argValue)
                        )
                )
            );
        
            $attribute->setData('option', $value);
            $attribute->save();
        
            //return newly created option id
            $attribute = Mage::getModel('eav/config')
                ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
            if ($attribute->usesSource()) {
                return $attribute->getSource()->getOptionId($argValue);
            }
        }
        

        您可以通过提供代码和选项值来为属性添加选项

        $this->addAttributeOptions('unfiorm_type', 'leotartd')
        

        【讨论】:

        • 感谢分享!
        【解决方案6】:

        在我的教程中,我将解释如何从 CSV 读取选项并以编程方式创建选项。

        http://www.pearlbells.co.uk/add-attribute-options-magento-scripts/ 详细说明请点击教程

        function createAttribute($options) {
        $option = array('attribute_id' => 
        Mage::getModel('eav/entity_attribute')->getIdByCode(
             Mage_Catalog_Model_Product::ENTITY, 
             'color'
            )
        );
        
        for ($i = 0; $i < count($options); $i++) {
            $option['value']['option'.$i][0] = $options[ $i ]; // Store View
            $option['value']['option'.$i][1] = $options[ $i ]; // Default store view
            $option['order']['option'.$i] = $i; // Sort Order
        
        }
        
        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
        $setup->addAttributeOption($option);
        }
        

        【讨论】:

          【解决方案7】:

          Arvind Bhardwaj 在此处输入代码的答案是正确的。但是如果你想在没有安装程序的情况下执行它,即在任何其他代码中,那么你可能会发现这很有帮助:

          同意 Arvind,但它仅适用于插入单个选项值,如果您想执行插入多个选项值,则只需替换 "$option['value'][$key.''.$manufacturer] = $manufacturer;"到“$option['values'][$key.''.$manufacturer] = $manufacturer;”到这个。

          下面是最终代码

                      require_once 'app/Mage.php';
                      umask(0);
                      Mage::app('default');
          
                      $arg_attribute = 'manufacturer';
                      $manufacturers = array('Sony', 'Philips', 'Samsung', 'LG', 'Panasonic', 'Fujitsu', 'Daewoo', 'Grundig', 'Hitachi', 'JVC', 'Pioneer', 'Teac', 'Bose', 'Toshiba', 'Denon', 'Onkyo', 'Sharp', 'Yamaha', 'Jamo');
          
                      $attr_model = Mage::getModel('catalog/resource_eav_attribute');
                      $attr = $attr_model->loadByCode('catalog_product', $arg_attribute);
                      $attr_id = $attr->getAttributeId();
          
                      $option['attribute_id'] = $attr_id;
                      foreach ($manufacturers as $key => $manufacturer) {
                          $option['values'][$key . '_' . $manufacturer] = $manufacturer;
                      }
          
                      $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
                      $setup->addAttributeOption($option);
          

          我希望它适用于插入多个选项。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-05-29
            • 2013-12-02
            • 2015-08-30
            • 1970-01-01
            • 1970-01-01
            • 2016-07-30
            • 1970-01-01
            相关资源
            最近更新 更多