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.
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
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!!!
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…
If you are a Shopify admin, using a Shopify Balance Account for your business revenue…