How To

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.

Happy Coding!!!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago