4

I want to get the values for custom product attribute in product details page. I am using below GrapgQl query to get the values. i am passing the name as the argument. How can i get the products attributes.

{
products(filter:{name: {eq: "Joust Duffle Bag"}}
                    ){
items{
  id
  sku
  special_price
  description{ html}
  short_description{ html}
  meta_title
  meta_keyword
  meta_description
  options_container
  type_id
  image{ url label }
  small_image{ url label }
  thumbnail{ url label }
  price{  
    regularPrice{
      amount{ 
        value
        currency
      }
    }
  }
}
}
}

2 Answers 2

6

For product attributes to be available in GraphQL, is_user_defined has to be true AND at least one of the following:

  • is_comparable
  • is_filterable
  • is_filterable_in_search
  • is_visible_on_front
  • used_in_product_listing
  • used_for_sort_by

Reference: \Magento\CatalogGraphQl\Model\Resolver\Products\Attributes\Collection::getAttributes()


Additionally, if the attributes should be filterable as well, at least one of the following must be true:

  • (Attribute is searchable AND visible_in_advanced_search)
  • attribute is "Used in Layered Navigation" (is_filterable)

And if the type is "Select", it must have options.

Reference: \Magento\CatalogGraphQl\Model\Config\FilterAttributeReader::getFilterAttributes()

2

After some searches we dont need any special query methods to retriew custom attributes. We can just pass the custom attribute code to the query.

EX - our custom attribute code is publisher just pass it in the query.

{
products(filter:{name: {eq: "Joust Duffle Bag"}}
                ){
items{
id
sku
publisher
price{  
regularPrice{
  amount{ 
    value
    currency
  }
}
}
}
}
}

to get the custom attribute values and label if it was the multiple select or select type refer How to get product attribute value, label using GraphQl in Magento 2.3?

8
  • but what about option and drop down attributes... that's all just printing id only... Dec 10, 2019 at 4:22
  • @prabhakaran7 for that you want to write a custom module.I explained this in this article mujahidh.com/…
    – Mujahidh
    Dec 10, 2019 at 5:50
  • this one is ok but what if i want in product details page -(Product query) Dec 10, 2019 at 5:58
  • custom attribute is already there but it's showing id in product query .... Dec 10, 2019 at 5:59
  • 1
    Thanks a lot, man! Sep 8, 2021 at 12:38

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.