magento Attribute Set Details

本文提供了Magento中关于属性集(Attribute Set)的实用代码片段,包括获取属性集详情、通过名称获取ID、列出所有属性集、获取属性集内的所有属性及按属性集过滤产品等关键操作。

Introduction

An attribute set is a collection of attributes which can be created/edited from Catalog -> Manage Attribute Sets in backend.

Some useful snippets regarding Attribute Set.

1> Getting Attribute Set Details

1
2
3
4
$attributeSetModel = Mage::getModel('eav/entity_attribute_set');
$attributeSetId    = 9; // or $_product->getAttributeSetId(); if you are using product model
$attributeSetModel->load($attributeSetId);
print_r($attributeSetModel->getData());

2> Getting Attribute Set Id by Name

1
2
3
4
5
6
7
8
9
10
11
$entityTypeId = Mage::getModel('eav/entity')
                ->setType('catalog_product')
                ->getTypeId();
$attributeSetName   = 'Default';
$attributeSetId     = Mage::getModel('eav/entity_attribute_set')
                    ->getCollection()
                    ->setEntityTypeFilter($entityTypeId)
                    ->addFieldToFilter('attribute_set_name', $attributeSetName)
                    ->getFirstItem()
                    ->getAttributeSetId();
echo $attributeSetId;

Note that the following code doesn’t work

1
2
3
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
->load($attributeSetName, 'attribute_set_name')
->getAttributeSetId();

Because if you refer to eav_attribute_set table then you can see there are lots of rows with same attribute set name(for example: Default)

3> Getting all Attribute Sets by Entity Type(catalog_product)

1
2
3
4
5
6
7
8
9
$entityTypeId = Mage::getModel('eav/entity')
                ->setType('catalog_product')
                ->getTypeId();
$attributeSetCollection = Mage::getModel('eav/entity_attribute_set')
                ->getCollection()
                ->setEntityTypeFilter($entityTypeId);
foreach($attributeSetCollection as $_attributeSet){
    print_r($_attributeSet->getData());
}

Alternatively, you can also use:

1
2
3
4
$attributeSets = Mage::getModel('catalog/product_attribute_set_api')->items();
foreach($attributeSets as $_attributeSet){
    print_r($_attributeSet);
}

4> Getting all attributes in an Attribute Set

1
2
3
4
$attributes = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);
foreach($attributes as $_attribute){
    print_r($_attribute);
}

5> Filtering Products by Attribute Set

1
2
3
4
5
6
7
$productCollection = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addFieldToFilter('attribute_set_id', $attributeSetId);
foreach($productCollection as $_product){
    print_r($_product->getData());
}

Hope you will find these snippets useful/helpful.
Please do share if you have any useful code snippets regarding Attribute Set.

Thanks for reading!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值