Question

i want to add new column in Quote payment table i tried script but not working

<?php
namespace Ibnab\Additional\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();   
        $setup->getConnection()->addColumn(
            $setup->getTable('quote_payment'),
            'bankowner',
            [
                'type' => 'text',
                'nullable' => true  ,
                'comment' => 'Bank',
            ]
        );
        $setup->getConnection()->addColumn(
            $setup->getTable('sales_order_payment'),
            'bankowner',
            [
                'type' => 'text',
                'nullable' => true  ,
                'comment' => 'Bank',
            ]
        );
        $setup->endSetup();
  }
}
Was it helpful?

Solution

Try this,

This will add a bankowner column to quote_payment table with index name called bankowner. Similarly you can add column into the sales_order_payment table as well.

<?php                                                                 
namespace Vendor\Madule\Setup;                                              
use Magento\Framework\Setup\ModuleContextInterface;                         
use Magento\Framework\Setup\SchemaSetupInterface;                           
use Magento\Framework\Setup\UpgradeSchemaInterface;                       
class UpgradeSchema implements UpgradeSchemaInterface{
protected $installer;
/**
 * {@inheritdoc}
 */
public function upgrade(
    SchemaSetupInterface $setup,
    ModuleContextInterface $context
) {
    $this->installer = $setup;
    $this->installer->startSetup();
    if (version_compare($context->getVersion(), "1.0.1", "<")) {
        //Add columns to existing quote table
        $this->addQuoteColumn($setup);
    }
    $this->installer->endSetup();
}

protected function addQuoteColumn($setup)
{
    $installer = $setup;
    $table_columns = [
        'bankowner' => [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            'nullable' => true,
            'length' => 255,
            'comment' => 'bankowner'
        ]
    ];
    $connection = $installer->getConnection();

    foreach ($table_columns as $name => $definition) {
        $connection->addColumn($installer->getTable('quote_payment'), $name, $definition);
        $connection->addIndex(
            $installer->getTable('quote_payment', 'bankowner'),
            $installer->getIdxName('quote_payment', ['bankowner'], '', 'bankowner'),
            ['bankowner']
        );
    }
}}

Hope this helps. Peace :)

OTHER TIPS

Do not modify Magento core tables, at first, more columns make reading from tables slower and upgrade process for merchants might be more complicated.

If you want to add additional properties to the quote or sales payment entities you can use the next following solutions.

  1. The first one is much simpler and doesn't require additional columns but it should be used only to store simple values like numbers and strings:

Use setAdditionalInformation and getAdditionalInformaiton methods of Magento\Payment\Model\InfoInterface (Magento\Sales\Model\Order\Payment and Magento\Quote\Model\Quote\Payment implement it) to store any details which will be stored in additional_information column as JSON encoded string in sales_order_payment and quote_payment tables. Also, if you set additional information properties to the quote payment entity before placing an order, this data will be transferred to the order payment entity by default. As an example, to set additionalInformation and to get additionalInformation.

  1. The second approach is more complicated but allows storing complicated entities with many fields - it's Extension Attributes - https://devdocs.magento.com/guides/v2.3/extension-dev-guide/extension_attributes/adding-attributes.html.

In that case, you will need to store and load extension attributes manually. For example, you have defined the extension attributes for quote and payment entities (extension_attributes.xml):

<config>
    <extension_attributes for="Magento\Sales\Api\Data\OrderPaymentInterface">
        <attribute code="bankowner" type="Custom\Api\Data\CustomAttribute" />
</extension_attributes>
    <extension_attributes for="Magento\Quote\Api\Data\PaymentInterface">
        <attribute code="bankowner" type="Custom\Api\Data\CustomAttribute" />
    </extension_attributes>

When you need to define your interface with custom attributes:

interface CustomAttribute
{
    public function getBankOwner(): string;

    public function setBankOwner(string $title): void;
}

And create a default implementation for the interface.

Also, you need to create a custom table to store your attributes, repository, resource model, etc. You need to define custom handlers to store extension attributes to the database like in the example for Vault - https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Vault/Observer/AfterPaymentSaveObserver.php#L53 and to load attributes - https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Vault/Plugin/PaymentVaultAttributesLoad.php#L50.

As I said, this approach more complicated but doesn't have an overhead for existing tables, allows using complicated and fine-grained entities.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top