How To

How to Add Custom Product Attribute Into Existing Product GraphQL?

Hello, Magento mates.

Welcome to MageComp Magento tutorials.

Today, in this Magento 2 tutorial, we will learn to add custom product attributes to existing product GraphQL.

Product attributes refer to the specific characteristics and features that describe a product. These traits help consumers understand the product’s qualities and make informed purchasing decisions.

Examples of product attributes include size, color, material, weight, functionality, and any other distinctive features that differentiate one product from another. Businesses often highlight these attributes in marketing materials to communicate the value and benefits of their products to potential customers.

Steps to Add Custom Product Attribute

Steps 1 –

To begin with the steps, first, we need to create a “schema.graphqls” file inside the extension at the following path.

app\code\Vendor\Extension\etc\

interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\ProductInterfaceTypeResolverComposite") @doc(description:"Seller Id") {
    seller_id: String @doc(description: "Seller Id") @resolver(class: "Vendor\\Extension\\Model\\Resolver\\Product\\Getsellerid")
}

Step 2 –

After that, we need to create the “Getsellerid.php” file inside the extension at the following path.

app\code\Vendor\Extension\Model\Resolver\Product

<?php
namespace Vendor\Extension\Model\Resolver\Product;

use \Magento\Catalog\Model\Product;

class Getsellerid implements \Magento\Framework\GraphQl\Query\ResolverInterface
{
    protected $productdata;

    public function __construct(
        Product $productdata
    ) {
        $this->productdata = $productdata;
    }
    
    public function resolve(
        \Magento\Framework\GraphQl\Config\Element\Field $field,
        $context,
        \Magento\Framework\GraphQl\Schema\Type\ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        $product = $value['model'];
        $productdata = $this->productdata->load($product->getId());
        $sellerId = $productdata->getSellerId();
        return $sellerId;
    }
}

Step 3 –

Once all files are created in your Magento, you need to run Magento upgrade, compile and deploy commands as follows.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush

Step 4 –

Postman to run the below query to get configuration value on storeconfig response.

Base Url: {base_url}/graphql

{
    products(
    filter: { sku: { eq: "Simple Product" } }
    ){
        items {
          id
          sku
          name
          seller_id
        }
    }
}

Output 👇

Ending Note

Incorporating custom product attributes into an existing product GraphQL setup is a powerful way to enhance the flexibility and functionality of your Magento 2 store.

By following the steps outlined in the tutorial, you can seamlessly extend the capabilities of your product data model, providing a more tailored and comprehensive experience for both administrators and end-users.

I hope this tutorial was helpful for you. If you encounter any difficulties while following these steps, please feel free to leave a comment with your queries in the comment section below. Alternatively, you can contact us through our official Facebook page or hire an experienced Magento 2 developer to assist you with any issues.

Thank you for reading.

Happy Coding!!!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

9 hours ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

2 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

5 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

6 days ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

1 week ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

1 week ago