How To

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

Hello, Magento mates.

Welcome to MageComp’s Magento tutorials.

Today, this tutorial is about getting a custom attribute value of an invoice for your Magento 2 store using REST API.

A custom attribute value of an invoice refers to additional, user-defined information that can be associated with an invoice. This feature allows you to extend the default set of attributes associated with an invoice by adding custom fields or values that meet specific business requirements.

Steps to Get Custom Attribute Value

Step 1 – 

First, we need to create an extension_attributes.xml file inside the etc folder 👇

app\code\Vendor\Extension\etc\

Using the code given below 👇

<?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\InvoiceInterface">
        <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\InvoiceRepositoryInterface">
        <plugin name="invoiceInformationUpdate" type="Vendor\Extension\Plugin\Api\InvoiceRepository" />
    </type>
</config>

Step 3 – 

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

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

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

use Magento\Sales\Api\Data\InvoiceExtensionFactory;
use Magento\Sales\Api\Data\InvoiceInterface;
use Magento\Sales\Api\Data\InvoiceSearchResultInterface;

class InvoiceRepository
{
    /**
     * @var InvoiceExtensionFactory
     */
     private $feeDataFields = [
        'custom_attribute',
        'secondcustom_attribute'
     
    ];

    protected $extensionFactory;

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

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

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

    public function afterGetList(\Magento\Sales\Api\InvoiceRepositoryInterface $subject, InvoiceSearchResultInterface $searchResult)
    {
        $invoices = $searchResult->getItems();

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

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

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

        return $searchResult;
    }
}

Step 4 –

Now, we need to run

php bin/magento setup:upgrade

You can see the values ​​of the custom attributes from the URL below.

BASE_URL/rest/all/V1/invoices/increment_id

Conclusion

Custom attribute values of invoices can be beneficial for Magento 2 store admins to display extra information related to an invoice, providing a flexible added feature for multiple Magento store needs and business scenarios.

These attributes can be managed and configured through the Magento 2 admin panel, allowing merchants to tailor their invoicing system to suit their unique needs.

This was it. If you have any queries related to this tutorial or any other Magento feature, kindly contact us, or hire our Magento developers to solve your queries.

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 day ago

React Native or Flutter in 2024

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

3 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…

6 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…

1 week ago

Five Essential Payroll Compliance Tips for eCommerce Startups

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

1 week ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

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

1 week ago