1

In Headless magento project, there is condition written by previous developers in javascript

if there is a value assigned for attribute : is_outlet_available , than some text should display, otherwise text should not display in product view page

products(
            filter: { is_outlet_available: { in: [$key] }, sku: { in: [$sku] } }
        )

I want to change as if the value of the attribute is 3 or 5 or 6 for that product, than only text should display in product view page, so i changed code as below :

filter: { is_outlet_available: { in: [3||5||6] }, sku: { in: [$sku] } } 

but still the text is displaying.

enter image description here

here is a full code : https://pastebin.com/CrS2ERW5

3
  • can you share your schema.graphqls ?
    – Msquare
    Nov 10, 2023 at 13:11
  • please check : pastebin.com/5f2EuXq1 , here is folder structure : prnt.sc/VpuQi0glcPLh Nov 10, 2023 at 13:34
  • Yes, I have provided a solution. Please check it, and if it is not working, update me. If you encounter any issues, kindly provide more information so that I can assist you further.
    – Msquare
    Nov 10, 2023 at 15:09

1 Answer 1

1

I created an attribute following your provided example and incorporated customizations into the product query. Please add the code below to your module and check the product query.

app/code/VendorName/ModuleName/etc

schema.graphqls

interface ProductInterface {
    is_outlet_available: [String] @doc(description: "Product Custom Attribute Show")
      @resolver(
        class: "VendorName\\ModuleName\\Model\\Resolver\\Product\\AddCustomAttribute"
      )
}

app/code/VendorName/ModuleName/Model/Resolver/Product

AddCustomAttribute.php

<?php

namespace VendorName\ModuleName\Model\Resolver\Product;

use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Catalog\Api\Data\ProductAttributeInterface;

class AddCustomAttribute implements ResolverInterface
{
    protected $productFactory;

    protected $attributeOptionsManagement;

    public function __construct(
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionsManagement
    ) {
        $this->productFactory = $productFactory;
        $this->attributeOptionsManagement = $attributeOptionsManagement;
    }

    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());
        $optionData = explode(',', $_product->getData('is_outlet_available') ?? '');
        $attributeOptions = $this->attributeOptionsManagement->getItems(ProductAttributeInterface::ENTITY_TYPE_CODE, 'is_outlet_available');
        $featuredOptions = [];
        if (count($optionData)) {
            foreach ($attributeOptions as $attributeOption) {
                if (in_array($attributeOption->getValue(), $optionData)) {
                    $featuredOptions[$attributeOption->getValue()] = $attributeOption->getLabel();
                }
            }
        }
        return $featuredOptions;
    }
}

Product Query with your attribute value:

{
  products(filter: { sku: { eq: "YOUR_SKU" } }) {
    items {
      name
      sku
      url_key
      is_outlet_available
    }
  }
}

OUTPUT: enter image description here

enter image description here

enter image description here

Now, the value is available within the product data, allowing you to directly check it in your Headless React code.

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.