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

How to Integrate and Use MongoDB with Laravel?

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

15 hours ago

NodeJS | Callback Function

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

1 day 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…

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

5 days 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…

6 days ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

6 days ago