Get Rma Attribute Options By Attribute Code Magento 2.

You can get all the RMA attribute options by RMA attribute code using Magento 2.

Native Magento Commerce RMA contains the four attribute codes.

                  • resolution
                  • condition
                  • reason
                  • reason_other

You need to fetch all the attribute options of specific RMA attribute code, You can get the ID and value of the option using the below code snippet,

You need to use the Magento\Eav\Api\AttributeOptionManagementInterface interface to fetch all the options.

<?php declare(strict_types=1);

namespace Rbj\RmaOptions\Model;

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\StateException;
use Magento\Eav\Api\AttributeOptionManagementInterface;
use Magento\Rma\Api\RmaAttributesManagementInterface;

/**
 * Class RmaAttributeOption.
 */
class RmaAttributeOption
{
    public function __construct(
        private readonly AttributeOptionManagementInterface $attributeOptionManagement
    ) {
    }

    /**
     * Get all attribute options by attribute code RMA.
     *
     * @param string $attributeCode
     *
     * @return string[]
     * @throws InputException
     * @throws StateException
     */
    public function getRmaAttributeOptions($attributeCode): array
    {
        $options = $this->attributeOptionManagement->getItems(
            RmaAttributesManagementInterface::ENTITY_TYPE,
            $attributeCode
        );

        $rmaOptions = [];

        foreach ($options as $option) {
            $rmaOptions[$option->getValue()] = strtolower($option->getLabel());
        }

        return $rmaOptions;
    }
}

We will fetch all the options of the condition attribute code.
We have to pass the attribute code to fetch all the attribute options of the RMA attribute code.

Call function using the below way,

$rmaAttributeCode = 'condition';
$this->getRmaAttributeOptions($rmaAttributeCode);

Output:

Array
(
    [] =>
    [7] => unopened
    [9] => damaged
    [8] => opened
)

Where 7 is the option_id and unopened is the option’s value.