Is there a way from a phtml file to get the attributes of an attribute set given the set id?
You will have to create a module for this.
So basically, you first need to find out which block renders your phtml file.
Once you've found out, you need to create app/code/Vendor/Module/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\<Module>\Block\<Path>\<To>\<Block>"
type="Vendor\Module\Block\<Path>\<To>\<Block>"/>
</config>
In this file you need to replace Magento\<Module>\Block\<Path>\<To>\<Block>
with the path to the block rendering the template. Don't forget to replace <Path>\<To>\<Block>
in the type
attribute too.
Then in app/code/Vendor/Module/Block/<Path>/<To>/<Block>
:
<?php
namespace Vendor\Module\Block\<Path>/<To>;
class <Block> extends \Magento\<Module>\Block\<Path>\<To>\<Block>
{
protected $_productAttributeManagementInterface;
public __construct(
...
\Magento\Catalog\Api\ProductAttributeManagementInterface $productAttributeManagementInterface
...
) {
...
$this->_productAttributeManagementInterface= $productAttributeManagementInterface;
...
}
public function getAttributes($id)
{
$attributes = $this->_productAttributeManagementInterface->getAttributes($id);
return $attributes ;
}
}
Please note that you will have to adapt the constructor with the original block constructor.
Don't forget the usual app/code/Vendor/Module/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="0.0.1" />
</config>
As well as the app/code/Vendor/Module/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Finally run the following commands:
php bin/magento module:enable Vendor_Module
php bin/magento setup:upgrade
And now you should be able to call the following in your template:
$attributes = $block->getAttributes($attributeSetId);
-
Thanks for this. It seems to get the attribute set information (like name, sort order and id), but what about the attributes assigned to that set? Thanks again! – Jim May 26 '16 at 14:32
-
@Jim sorry I missed that. I've updated the answer so you can retrieve the attributes ;) – Raphael at Digital Pianism May 26 '16 at 14:36
-
Works perfectly. Do you happen to know a way to look up the label for the attribute? I thought it would be part of the attribute itself but I'm not seeing a function to getLabel or anything close to that. – Jim May 26 '16 at 16:07
-
@Jim hmmm I think eav doesn't let u do that. Feel free to post a new question I'll check it out – Raphael at Digital Pianism May 26 '16 at 16:52
-
1