1

I want to get Attribute set ID in product page
Can you help me please

1 Answer 1

0

create GetAttributes.php

<?php
namespace Vendor\Module\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Api\ProductAttributeManagementInterface;

class GetAttributes
{
    /**
     * @var ProductAttributeManagementInterface
     */
    private $productAttributeManagement;

    public function __construct(
        ProductAttributeManagementInterface $productAttributeManagement
    ) {
        $this->productAttributeManagement = $productAttributeManagement;
    }

    /**
     * Get Product Attributes from the attribute set.
     *
     * @return ProductAttributeInterface[]
     */
    public function getAttributeListBySetId()
    {
        $attributeSetId = 4; // Default Attribute set
        try {
            $getAttributes = $this->productAttributeManagement
                ->getAttributes($attributeSetId);
        } catch (NoSuchEntityException $exception) {
            throw new NoSuchEntityException(__($exception->getMessage()));
        }

        return $getAttributes;
    }
}

Now just call that collection where ever you want Hope this helps

Update:-

this is the way you can call that methods

$getAttributes = $this->getAttributeListBySetId();
foreach ($getAttribute as $attribute){
    var_dump($attribute->debug());
}
12
  • I edited my question, Can you take a look please! Jul 7, 2020 at 16:35
  • product listing page??
    – Pramod
    Jul 8, 2020 at 3:33
  • product page as I mentionned plz Jul 8, 2020 at 8:45
  • check updated ans
    – Pramod
    Jul 8, 2020 at 8:53
  • Where can I add this file plz Jul 8, 2020 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.