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

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

1 hour ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

2 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

5 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

6 days ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

7 days ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

7 days ago