属性集合中的Magento2属性组名称

时间:2017-11-28 12:52:22

标签: attributes magento2

我需要属性集合中与属性ID关联的属性组名称。我有Aattribute Set ID。

谢谢!

1 个答案:

答案 0 :(得分:0)

/**
 * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory
 */
protected $_groupCollection;


public function __construct(
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory   
)
{     
 $this->_groupCollection = $groupCollectionFactory;     
}

 /**
 * @param $attributSetID
 * @return array
 */
public function getAttributeGroupName($attributeSetID){

    $groups = $this->_groupCollection->create();
    $groups->setAttributeSetFilter($attributeSetID);

    $groupData = [];
    /* @var $group \Magento\Eav\Model\Entity\Attribute\Group */
    foreach ($groups as $group) {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $attributeCollection = $objectManager->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection');

        $attributeCollection
            ->setAttributeGroupFilter($group->getId())
            ->setAttributeSetFilter($attributeSetID);

        foreach ($attributeCollection->getAllIds() as $attributeId) {
            $groupData[$attributeId] = $group->getAttributeGroupName();
        }

    }
            return $groupData;

}