How to add timestamp column in sales_order_item table using db_schema.xml Magento 2?

You can add a new column, a field in sales_order_item table using db_schema.xml under your module, etc folder from Magento 2.3 and higher version.

We try to add a new column called item_expiry_date in sales_order_item table.

To add item_expiry_date field in table you need to only add one row inside the db_schema.xml file in your module.

Here, the Main table for Sales Item Operation is “sales_order_item” and you can add a new field in the table using below simple code snippet,

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="sales_order_item" resource="sales" engine="innodb" comment="Sales Order Item">
        <column xsi:type="timestamp" name="item_expiry_date" on_update="false" nullable="true" comment="Item expiry date"/>
    </table>
</schema>

Here New Column type is a timestamp. You can add timestamp at item level using adding a new column in sales_order_item table.

Run command from Magento root instance,
php bin/magento setup:upgrade

Check the sales_order_item table in Database and one new column is added to the table and you can see new column.

Get Country name by Country id Magento 2.

Retrieve Country full name from Country Id Using Magento 2 by Country Class of Directory Module.

You need to instantiate CountryFactory in your class __construct() method to get the country full name.

The class responsible to get name is Magento\Directory\Model\Country. Continue reading “Get Country name by Country id Magento 2.”

How to add column in sales_order table using db_schema.xml Magento 2?

You can add a new column in sales_order table using db_schema.xml under your module etc folder from Magento 2.3 and Upper version.

We try to add a new column called order_country in sales_order table.

To add order_country field in sales_order table, you need to only add one row inside the db_schema.xml file structure.

Here, the Main table for Sales Operation is sales_order and you can add a new field in sales_order table by the below way, Continue reading “How to add column in sales_order table using db_schema.xml Magento 2?”