6

In magento 2 I want to add column in quote_item table.

How can i add that column,i am new in magento

Please help me

4 Answers 4

18

You can add column like this

namespace [Name]\[Modulename]\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class InstallSchema implements InstallSchemaInterface
{

/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
public function install(SchemaSetupInterface $setup, ModuleContextInterface    $context)
  {
    $installer = $setup;

    $installer->startSetup();

    $eavTable = $installer->getTable('quote_item');

    $columns = [
        'customname' => [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            'nullable' => false,
            'comment' => 'customname',
        ],

    ];

    $connection = $installer->getConnection();
    foreach ($columns as $name => $definition) {
        $connection->addColumn($eavTable, $name, $definition);
    }

    $installer->endSetup();
}
}

Using this code you can add column in quote_item table

3
  • I tried the same code but it didn't work for me. Could you please help? Jul 20, 2016 at 13:53
  • @IpsitaRout can you create new question with your code?
    – ND17
    Jul 21, 2016 at 7:11
  • This code worked for me after changing version in etc/module.xml. Tx Jul 21, 2016 at 9:24
1
<?php


namespace Test\TestAgain\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;
use Magento\Framework\DB\Ddl\Table;
use Magento\Quote\Setup\QuoteSetup;
use Magento\Sales\Setup\SalesSetup;

class UpgradeSchema implements UpgradeSchemaInterface
{


    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {


         $installer = $setup;
        $installer->startSetup();
        $connection = $installer->getConnection();
        $connection->addColumn($installer->getTable('quote'), 'custom_column', [
            'type'     => Table::TYPE_TEXT,
            'nullable' => true,
            'comment'  => 'Custom Column Name'
        ]);
        $connection->addColumn($installer->getTable('sales_order'), 'custom_column', [
            'type'     => Table::TYPE_TEXT,
            'nullable' => true,
            'comment'  => 'Custom Column Name'
        ]);

        $installer->endSetup();


        }
}

Note: If you face any issue, it may be due to the module that you have already installed. As you know, if the module is already installed then setup:upgrade command does not install schema. You will need to look into your setup_module table, delete your module from the table and re-run the php bin/magento setup:upgrade command.

1

I have some complains with accepted answer. We should avoid operating over tables, especially core tables. The better way is to use QuoteSetup. The other issue that all answers implements is usage of SchemaSetup script when we should use DataSetup. That's because quote_item columns are considered as attributes in core Magento.

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Quote\Setup\QuoteSetupFactory;

class InstallData implements InstallDataInterface
{

    /**
     * Quote setup factory
     *
     * @var QuoteSetupFactory
     */
    private $quoteSetupFactory;

    /**
     * Init
     *
     * @param QuoteSetupFactory $quoteSetupFactory
     */
    public function __construct(
        QuoteSetupFactory $quoteSetupFactory
    ) {
        $this->quoteSetupFactory = $quoteSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        /* @var $quoteSetup \Magento\Quote\Setup\QuoteSetup */
        $quoteSetup = $this->quoteSetupFactory->create(['setup' => $setup]);
        /* Price type custom column */
        $quoteSetup->addAttribute("quote_item", "new_price", ['type' => 'decimal']);
        /* Text type custom column */
        $quoteSetup->addAttribute("quote_item", "label", ['type' => 'text']);

        $setup->endSetup();
    }
}
0

I took this project file as an example: https://github.com/sohelrana09/magento2-module-delivery-date/blob/master/SR/DeliveryDate/Setup/InstallSchema.php

<?php
namespace SR\DeliveryDate\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        $installer->getConnection()->addColumn(
            $installer->getTable('quote'),
            'delivery_date',
            [
                'type' => 'datetime',
                'nullable' => false,
                'comment' => 'Delivery Date',
            ]
        );

        $setup->endSetup();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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