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

3

6

I have tried all sorts of things and I'm not getting anywhere. Please would someone show me how to get the name and id of all available product attribute sets? One will be 'Default'...

I'm building a custom quoting system and need to pull in the attribute sets so that users can select that first then load up the products that are assigned to that set.

Many thanks for your help.

Conrad answered 29/8, 2012 at 10:52 Comment(0)
T
12

You can load the attribute sets with:

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

Iterate:

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

You can then load your collection by attribute set.

Tricornered answered 29/8, 2012 at 11:24 Comment(1)
The code returns all attribute sets. I only wanted those that are seen in 'Admin > Attributes > Manage Attribute Sets'. Add this to the collection: // We are only interested in sets that can be used for products, add a filter. // Based on Mage_Adminhtml_Block_Catalog_Product_Attribute_Set_Grid->_prepareCollection() and // Mage_Adminhtml_Catalog_Product_SetController->_setTypeId() $entity_type = Mage::getModel('catalog/product')->getResource()->getTypeId(); $attributeSetCollection->setEntityTypeFilter($entity_type); NB For this to work, remove the '->load()' part.Opine
P
2

So As you are trying to get the attributeSets which are showing in manage attribute set section of admin you can follow the below coding:

<?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>";
?>

Click here! For more reference.

Prosecution answered 10/11, 2013 at 7:22 Comment(0)
H
0

If you want to get an Attribute set selector in your Magento Admin System > Configuration this class will be useful:

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;
    }

}

These attribute sets are limited by catalog_product entity type.

Indeed you will need the field in your system.xml like this:

<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>
Hemichordate answered 23/7, 2015 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.