1

I am using Magento 2.3.3 and GraphQL to get a list of products.

I want to query products by a custom attribute that I created in the admin. Couldn't find in the documentation how to.

It is possible..?

Thanks!

3
  • what is your purpose? is it for layered navigation or other ?
    – Mujahidh
    Commented Oct 28, 2019 at 5:08
  • @Mujahidh the user should be able to filter products by custom attributes such as brand, color, etc Commented Oct 28, 2019 at 6:50
  • @AlanSteiman try this mujahidh.com/… Commented May 26, 2020 at 4:16

2 Answers 2

3

Magento 2.4

Filter, sort, and get product custom attribute

Create Custom Attribute with below options values.

enter image description here

enter image description here

app/code/VendoreName/ModuleName/etc

schema.graphqls

interface ProductInterface {
    sample_attribute: String @doc(description: "Product Custom Attribute Show")
      @resolver(
        class: "VendoreName\\ModuleName\\Model\\Resolver\\Product\\AddCustomAttribute"
      )
}

input ProductAttributeFilterInput {
  sample_attribute: FilterEqualTypeInput @doc(description: "Product Data filter with Custom Attribute Value")
}

input ProductAttributeSortInput {
  sample_attribute: SortEnum @doc(description: "Sort by the Product Custom Attribute.")
}

app/code/VendoreName/ModuleName/Model/Resolver/Product

AddCustomAttribute.php

<?php

namespace VendoreName\ModuleName\Model\Resolver\Product;

use Magento\Framework\GraphQl\Query\ResolverInterface;

/**
 * Product custom attribute field resolver
 */
class AddCustomAttribute implements ResolverInterface
{

    /**
     * @var Magento\Catalog\Model\ProductFactory
     */
    protected $productFactory;

    /**
     * @param Magento\Catalog\Model\ProductFactory $productFactory
     */
    public function __construct(
        \Magento\Catalog\Model\ProductFactory $productFactory
    ) {
        $this->productFactory = $productFactory;
    }

    /**
     * @inheritdoc
     */
    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'];
        $_product = $this->productFactory->create()->load($product->getId());
        return $_product->getData('sample_attribute');
    }
}

Run Magento Commands:

php bin/magento s:up
php bin/magento in:rein
php bin/magento s:s:d -f
php bin/magento c:c

For show product custom attribute value

{
  products(filter: { sku: { eq: "24-MB01" } }) {
    items {
      name
      sku
      sample_attribute
    }
  }
}

For filter product data using custom attribute

{
  products(filter: { sample_attribute: { eq: "test" } }) {
    items {
      name
      sku
      sample_attribute
    }
  }
}

For sort product data using custom attribute

{
  products(search: "bag", sort: {sample_attribute: DESC}) {
    items {
      name
      sku
      sample_attribute
    }
  }
}
8
  • $args will give NULL in the above case. Any solution would be appreciated Commented Feb 20, 2023 at 14:25
  • Yes, it's returns null, because it does not have any query param.
    – Msquare
    Commented Feb 20, 2023 at 15:14
  • Why not ? For e.g products(search: "bag", sort: {sample_attribute: DESC}) , you are passing arguments right like search, sort, filter ? How to get those values in our custom resolver ? Commented Feb 20, 2023 at 18:55
  • @biplabrout You can not get that values in the custom resolver. If you want to get above mention arguments you must use this resolver vendor/magento/module-catalog-graph-ql/Model/Resolver/Products.php. Because you can check products query has already defined resolver.
    – Msquare
    Commented Feb 21, 2023 at 9:11
  • Hi @msquare! Thanks but how to achieve this accessing the arguments of some other resolver class in my custom resolver ? Any help would be appreciated. Commented Feb 23, 2023 at 3:59
0

Okay got the solution..

  1. created before plugin for products resolver
  2. set the arguments values in session
  3. in my custom resolver fetch the arguments using the session

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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