0

how can I add a custom column into an order invoice table and use extension attribute to save some data in this column via REST Api.

thank you!!


Hi thank you very much for your answer, it seems to work when retrieving data but when i tried to create invoice via postman using

/rest/default/V1/order/{orderId}/invoice

{ "capture": true, "items": [ { "extension_attributes": {}, "order_item_id": 0, "qty": 0 } ], "notify": true, "appendComment": true, "comment": { "extension_attributes": {}, "comment": "string", "is_visible_on_front": 0 }, "arguments": { "extension_attributes": { "custom_column": "My Custom Value"} } }

it throws me an error

"message": ""%fieldName" is not supported. Correct the field name and try again.", "parameters": { "fieldName": "CustomColumn" },

Murtuza Zabuawala
14.8k10 gold badges47 silver badges76 bronze badges
asked Feb 3 at 17:35

1 Answer 1

0

1. Add a Custom Column to the Sales Invoice Table

Create a Setup Upgrade Schema to add a custom column.You Can use the db_schema.xml as well to create the column.

Create app/code/Vendor/Module/Setup/UpgradeSchema.php

<?php
namespace Vendor\Module\Setup;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
 public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
 {
 $setup->startSetup();
 if (version_compare($context->getVersion(), '1.0.1', '<')) {
 $connection = $setup->getConnection();
 $tableName = $setup->getTable('sales_invoice');
 if (!$connection->tableColumnExists($tableName, 'custom_column')) {
 $connection->addColumn(
 $tableName,
 'custom_column',
 [
 'type' => Table::TYPE_TEXT,
 'nullable' => true,
 'length' => 255,
 'comment' => 'Custom Column for Invoice'
 ]
 );
 }
 }
 $setup->endSetup();
 }
}

Run the Upgrade Command

php bin/magento setup:upgrade

2. Create an Extension Attribute for the Invoice

Extension attributes allow you to add custom fields to Magento's core APIs.

Create app/code/Vendor/Module/etc/extension_attributes.xml

<?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_column" type="string"/>
 </extension_attributes>
</config>

3. Create a Plugin to Handle Extension Attribute Save

We need a plugin to save the data in the custom column when an invoice is created via REST API.

Create app/code/Vendor/Module/Plugin/InvoiceRepositoryPlugin.php

<?php
namespace Vendor\Module\Plugin;
use Magento\Sales\Api\Data\InvoiceExtensionFactory;
use Magento\Sales\Api\Data\InvoiceInterface;
use Magento\Sales\Api\InvoiceRepositoryInterface;
use Magento\Framework\Api\SearchResultsInterface;
class InvoiceRepositoryPlugin
{
 protected $extensionFactory;
 public function __construct(InvoiceExtensionFactory $extensionFactory)
 {
 $this->extensionFactory = $extensionFactory;
 }
 /**
 * Load custom column into invoice extension attribute
 */
 public function afterGet(
 InvoiceRepositoryInterface $subject,
 InvoiceInterface $result
 ) {
 $extensionAttributes = $result->getExtensionAttributes() ?? $this->extensionFactory->create();
 $result->setExtensionAttributes($extensionAttributes);
 $extensionAttributes->setCustomColumn($result->getData('custom_column'));
 return $result;
 }
 /**
 * Load custom column into invoice extension attributes in getList
 */
 public function afterGetList(
 InvoiceRepositoryInterface $subject,
 SearchResultsInterface $searchResults
 ) {
 foreach ($searchResults->getItems() as $invoice) {
 $this->afterGet($subject, $invoice);
 }
 return $searchResults;
 }
 /**
 * Save custom column from extension attribute
 */
 public function beforeSave(
 InvoiceRepositoryInterface $subject,
 InvoiceInterface $invoice
 ) {
 $extensionAttributes = $invoice->getExtensionAttributes();
 if ($extensionAttributes && $extensionAttributes->getCustomColumn()) {
 $invoice->setData('custom_column', $extensionAttributes->getCustomColumn());
 }
 }
}

4. Expose the Extension Attribute in di.xml

Create app/code/Vendor/Module/etc/di.xml

<?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\Data\InvoiceInterface">
 <plugin name="add_custom_column_invoice_repository_plugin"
 type="Vendor\Module\Plugin\InvoiceRepositoryPlugin" sortOrder="10"/>
 </type>
</config>

5. Verify the REST API

Now, when you fetch an invoice via API, your custom column will be included.

GET Invoice API Example

GET /rest/V1/invoices/:invoice_id

Response Example

{
 "entity_id": 10,
 "increment_id": "100000001",
 "custom_column": "Custom Data",
 "extension_attributes": {
 "custom_column": "Custom Data"
 }
}

POST Invoice API Example (Save Custom Data)

POST /rest/V1/invoices

Request Body

{
 "entity": {
 "order_id": 1,
 "extension_attributes": {
 "custom_column": "My Custom Value"
 }
 }
}

6. Flush Cache & Test

Run:

php bin/magento cache:flush

Then test the API via Postman or cURL.

answered Feb 6 at 12:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.