Magento – Get product attribute value in GraphQL api

apigraphqlmagento2

I'm having a problem with the GraphQL API for Magento 2. I cannot retrieve the attribute value of multiselect or dropdown attributes when I retrieve my products.

In this example my attribute "material" returns an integer of the value ID instead of the textual value. I'm currently using the endpoint https://devdocs.magento.com/guides/v2.3/graphql/reference/custom-attribute-metadata.html to retrieve all the option values, but this is not very efficient.

{
  products(
    search: "Chair"
    pageSize: 10
  )
  {
    total_count
    items {
      name
      sku
      material
      price {
        regularPrice {
          amount {
            value
            currency
          }
        }
      }
    }
    page_info {
      page_size
      current_page
    }
  }
}

returns:

{
  "data": {
    "products": {
      "total_count": 48,
      "items": [
        {
          "name": "Brown Chair",
          "sku": "4203894",
          "material": 209,
          "price": {
            "regularPrice": {
              "amount": {
                "value": 89,
                "currency": "EUR"
              }
            }
          }
        },
    .......

Is there a way to get the textual value of the "material" attribute ("Leather") in this call?

Best Answer

From Native GraqpQl we cannot get the expected result.But we can write a custom module with the graphql and achieve it.What my suggestion is get the product detail by sku using _productRepository->get($sku);. This will return the entire product details.So you can get the attribute data by using $attributes = $_product->getAttributes(); Here is my data provider file

/**
 * @params string $sku
 * this function return all the product data by product sku
 **/
public function getProductBySku($sku)
{
    return $this->_productRepository->get($sku);
}
/**
 * @params int $id
 * this function return all the word of the day by id
 **/
public function getAttributesBySku( $sku ){
    $_product = $this->getProductBySku($sku);
    $attributes = $_product->getAttributes();// All Product Attributes

    $attributes_data = [];
    $x=0;
    foreach ($attributes as $attribute) {
        if($attribute->getIsUserDefined()){ // Removed the system product attribute by checking the current attribute is user created
            $attributeLabel = $attribute->getFrontend()->getLabel();
            $attributeValue = $attribute->getFrontend()->getValue($_product);

            if($attribute->getAttributeCode()=="language"){
                $attributeLabelAndValue = $attributeLabel." - ".$attributeValue;
                $attributes_data[$x]['atr_data'] = $attributeLabelAndValue;
            }
        }
        $x++;
    }
    return $attributes_data;
}

this code will return the need

$attribute->getFrontend()->getLabel(); this will return the label and 
$attribute->getFrontend()->getValue($_product); this will return the value.

To check the entire graphQl query and resolver file kindly view How to get product attribute value, label using GraphQl in Magento 2.3?

Hope this will help you.

Related Topic