Magento获取可用属性集id';s和外部脚本中的名称


Magento get available attribute set id's and names from external script

我尝试了各种各样的东西,但没有取得任何进展。有人能告诉我如何获得所有可用产品属性集的名称和id吗?其中一个将是"默认"。。。

我正在构建一个自定义报价系统,需要引入属性集,以便用户可以首先选择该属性集,然后加载分配给该属性集的产品。

非常感谢你的帮助。

您可以使用以下属性加载属性集:

$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection') ->load();

迭代:

foreach ($attributeSetCollection as $id=>$attributeSet) {
  $entityTypeId = $attributeSet->getEntityTypeId();
  $name = $attributeSet->getAttributeSetName();
  Mage::log("ATTRIBUTE SET :".$name." - ".$id);
}

然后可以按属性集加载集合。

因此,当您试图获取显示在管理属性集部分的属性集时,您可以按照以下编码进行操作:

<?php
      require_once('app/Mage.php');
      umask(0);
      Mage::app();//->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
      $entityType = Mage::getModel('catalog/product')->getResource()->getTypeId();
      $collection = Mage::getResourceModel('eav/entity_attribute_set_collection')->setEntityTypeFilter($entityType);
      $allSet = array();
      foreach($collection as $coll){
         $attributeSet['name'] = $coll->getAttributeSetName();
         $attributeSet['id'] = $coll->getAttributeSetId();
         $allSet[] = $attributeSet;
      }
      echo "<pre>";
      print_r($allSet);
      echo "</pre>";
?>

点击这里!供更多参考。

如果你想在Magento管理系统>配置中获得属性集选择器,这个类会很有用:

class CompanyName_ModuleName_Model_System_Config_Source_Catalog_Product_Attributeset
{
    protected $_options = array();
    public function toOptionArray()
    {
        if (!count($this->_options)) {
            $entityTypeId           = Mage::getResourceModel('catalog/product')->getTypeId();
            $attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection')
                    ->setEntityTypeFilter($entityTypeId);
            foreach ($attributeSetCollection as $_attributeSet) {
                $this->_options[] = array(
                    'value' => $_attributeSet->getId(),
                    'label' => $_attributeSet->getAttributeSetName()
                );
            }
        }
        return $this->_options;
    }
}

这些属性集受到catalog_product实体类型的限制。

实际上,您需要system.xml中的字段,如下所示:

<select_attribute_set translate="label">
    <label>Default Attribute Set for new importing products</label>
    <frontend_type>select</frontend_type>
    <source_model>companyname_modulename/system_config_source_catalog_product_attributeset</source_model>
    <sort_order>30</sort_order>
    <show_in_default>1</show_in_default>
</select_attribute_set>