1

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();
  }
}
  • show us the script – Philipp Sander Feb 15 at 10:17
  • You must not modify core database tables. Use extension attributes or additionalInformation instead. – joni jones Feb 19 at 15:14
  • 3i using addtional information – 3434 Feb 20 at 6:01
0

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 :)

  • Please try this and let me know. – Prathap Gunasekaran Feb 15 at 10:51
  • I've downvoted your answer because it's a bad approach to modify Magento core tables. The Extension Attributes or additionalInformation property should be used instead. – joni jones Feb 19 at 15:15
  • @jonijones You should downvote the question instead, given answer based on the question and there is no nightmare in modifying core tables. And please gohead and provide the "extension attribute" solution to the above question here. please so we would be glad to know how to use that. Thanks – Prathap Gunasekaran Feb 20 at 4:12
  • The given answer shows a bad approach which other people might use as accepted. "there is no nightmare in modifying core tables." - it will be a nightmare for merchants when they need to update their instances. Also, when developers use such an approach, they make Magento slower - now sales_order_payment has 55 columns and you propose to add more. Also, for modifying DB schema - declarative setup should be used. I've added a comment to the question about extension attributes - they are described in the official documentation, don't see a reason to duplicate the documentation. – joni jones Feb 20 at 17:07
1

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.

  • +1 for your effort. Even though it is not demonstrated but we get to know the other ways to doing it so. Thanks – Prathap Gunasekaran Feb 21 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.