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 code.

  • 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 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
{
    /**
     * @var AttributeOptionManagementInterface
     */
    private $attributeOptionManagement;

    public function __construct(
        AttributeOptionManagementInterface $attributeOptionManagement
    ) {
        $this->attributeOptionManagement = $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 condition attribute code.
We have to pass attribute code to fetch all the attribute options of 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.