How to Get Custom Attribute Value Of Credit Memo Using Rest API?

Hello, Magento mates. We meet again.

Welcome to MageComp’s Magento tutorials.

Today, in this Magento tutorial, we will learn to get the custom attribute value of credit memos in the Magento 2 store using REST API.

Custom attribute values in credit memos refer to additional customer information that is associated with a credit memo in the Magento platform. Custom attributes allow Magento admins to collect and store specific data points relevant to their business processes or customer interactions.

Magento, by default, does not provide any predefined attributes for credit memos, but Magento admins can create custom attribute values and configure them as per their specific business needs.

Also Read – How to Get Custom Attribute Value Of Invoice Using Rest API?

Steps to Get Custom Attribute Value

Step 1 –

To begin with the process, we need to create an extension_attributes.xml file inside the etc folder ?

app\code\Vendor\Extension\etc\

Using the below code ?

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Sales\Api\Data\CreditmemoInterface">
        <attribute code="custom_attribute" type="string" />
         <attribute code="secondcustom_attribute" type="string" />
    </extension_attributes>
</config>

Step 2 –

Now, we need to create a di.xml file inside the etc folder.

app\code\Vendor\Extension\etc\

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Api\CreditmemoRepositoryInterface">
        <plugin name="creditmemoInformationUpdate" type="Vendor\Extension\Plugin\Api\CreditmemoRepository" />
    </type>
</config>

Step 3 –

Then, we need to create a CreditmemoRepository.php file inside the Plugin folder.

app/code/Vendor/Extension/Plugin/Api/

<?php
namespace Vendor\Extension\Plugin\Api;

use Magento\Sales\Api\Data\CreditmemoExtensionFactory;
use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Api\Data\CreditmemoSearchResultInterface;

class CreditmemoRepository
{
     private $feeDataFields = [
        'custom_attribute',
        'secondcustom_attribute'
     
    ];

    protected $extensionFactory;

    public function __construct(CreditmemoExtensionFactory $extensionFactory)
    {
        $this->extensionFactory = $extensionFactory;
    }

    public function afterGet(
        \Magento\Sales\Api\CreditmemoRepositoryInterface $subject,
        CreditmemoInterface $creditmemo
    ) {
        $extensionAttributes = $creditmemo->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();

        foreach ($this->feeDataFields as $key) {
            $data = $creditmemo->getData($key) ? $creditmemo->getData($key) : '0.0000';
            $extensionAttributes->setData($key, $data);
        }
            $creditmemo->setExtensionAttributes($extensionAttributes);
            
           
        return $creditmemo;
    }

    public function afterGetList(\Magento\Sales\Api\CreditmemoRepositoryInterface $subject, CreditmemoSearchResultInterface $searchResult)
    {
        $creditmemos = $searchResult->getItems();

        foreach ($creditmemos as &$creditmemo) {
            $extensionAttributes = $creditmemo->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();

        foreach ($this->feeDataFields as $key) {

            $data = $creditmemo->getData($key) ? $creditmemo->getData($key) : '0.0000';
            $extensionAttributes->setData($key, $data);
        }
        $creditmemo->setExtensionAttributes($extensionAttributes);
        }

        return $searchResult;
    }
}

Step 4 –

After completing all the above steps, you need to run

php bin/magento setup:upgrade

Then you can see the values ​​of the custom attributes from the URL below. ?

BASE_URL/rest/all/V1/creditmemo/increment_id

Ending Phrase

A credit memo is a document issued by a seller to refund a customer for returned goods or overpaid amounts. These custom attributes can be configured and managed through the Magento 2 admin panel or programmatically in code. They provide a way to store and retrieve extra details related to credit memos, enabling merchants to tailor the system to their specific needs.

Custom attributes can be used for various purposes, such as tracking additional order information, recording specific refund details, or accommodating unique business requirements.

Hope you found this tutorial helpful. If you have any queries regarding this tutorial or any other Magento service, feel free to contact us or get in touch with us on our official Facebook page.

hire magento developers

Happy Coding!!!

Previous Article

React Native | FlatList Component

Next Article

How to Disable the PageBuilder Module for a Specific Field in Magento 2

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨