0

I am trying to create a custom API to get all attributes of a product.

{{host}}/products/attribute-sets/17/attributes
{{host}}/products/attributes?searchCriteria[filter_groups][0][filters][0][field]=attribute_id&searchCriteria[filter_groups][0][filters][0][value]=97, 75, 125, 87, 128, 78, 104, 101, 135, 73, 76, 88, 84, 79, 106, 102, 74, 89, 85, 80, 117, 100, 122, 157, 86, 81, 116, 77, 90, 92, 91, 121, 96, 119, 134, 120, 98, 115, 124, 158, 103, 159, 129, 160, 82, 161, 107, 123, 108, 162, 109, 99, 110, 105, 111, 94, 112, 95, 113, 163, 164, 114, 167, 165, 130, 131, 132, 133, 136&searchCriteria[filter_groups][0][filters][0][condition_type]=in&searchCriteria[filter_groups][1][filters][0][field]=is_visible_on_front&searchCriteria[filter_groups][1][filters][0][value]=1&searchCriteria[filter_groups][1][filters][0][condition_type]=eq

What I want is a combined result of above mentioned API.When I call

{{host}}/products/attribute-sets/17/attributes

API, It doesn't provide all attributes value (like is_visible_on_front..).To get a detailed attribute I have to call the next API with all attribute id returned from the first API. So I wanted to know if there is a built-in API to achieve the result. Or how to create a custom API for the result. Please somebody help.

2
0

After refering to this answer https://magento.stackexchange.com/a/282395/87036

, I was able to produce the result I wanted.

public function getProductAttributes($id)
{

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $_product = $objectManager->get('Magento\Catalog\Model\Product')->load($id);
    $attributeSetId = $_product->getData('attribute_set_id');
    $groupCollection = $this->attributeGroupCollection->create()
        ->setAttributeSetFilter($attributeSetId)
        ->load(); 
    $attributeCollection = [];
    foreach ($groupCollection as $group) {
        $groupAttributesCollection = $this->productAttributeCollection->create()
            ->setAttributeGroupFilter($group->getId())
            ->addVisibleFilter()
            ->load(); 

        foreach ($groupAttributesCollection->getItems() as $attribute) {
                $attribute_code = $attribute->getData('attribute_code');

                $attribute = $this->eavConfig->getAttribute('catalog_product', $attribute_code);
                $options['options'] = $attribute->getSource()->getAllOptions();

                $attributeCollection[] = array_merge($attribute->getData(), $options);
        }
    }

    return $attributeCollection;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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