How to get custom product attributes values using graphQl queries in magento 2.3?
-
06-05-2021 - |
문제
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
}
}
}
}
}
}
해결책
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?
다른 팁
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
ANDvisible_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()