Reading list Switch to dark mode

    Create Order And Quote_address Attribute by Installer Magento 2

    Updated 29 July 2016

    Create Order And Quote_address Attribute by Installer Magento 2 – There is a simple way if you want to create attribute of order and quote address by installer.
    Just check the below code snippet then i’ll describe the points

    <?php
    namespace Namespace\Module\Setup;
    
    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;
    use Magento\Sales\Setup\SalesSetupFactory;
    use Magento\Quote\Setup\QuoteSetupFactory;
    
    class InstallData implements InstallDataInterface
    {
        /**
         * @var Magento\Sales\Setup\SalesSetupFactory
         */
        protected $_salesSetupFactory;
    
        /**
         * @var Magento\Quote\Setup\QuoteSetupFactory
         */
        protected $_quoteSetupFactory;
    
        /**
         * @param SalesSetupFactory $salesSetupFactory
         * @param QuoteSetupFactory $quoteSetupFactory
         */
        public function __construct(
            SalesSetupFactory $salesSetupFactory,
            QuoteSetupFactory $quoteSetupFactory
        ) {
            $this->_salesSetupFactory = $salesSetupFactory;
            $this->_quoteSetupFactory = $quoteSetupFactory;
        }
    
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
    
            /** @var \Magento\Sales\Setup\SalesSetup $salesInstaller */
            $salesInstaller = $this->_salesSetupFactory
                            ->create(
                                [
                                    'resourceName' => 'sales_setup',
                                    'setup' => $setup
                                ]
                            );
            /** @var \Magento\Quote\Setup\QuoteSetup $quoteInstaller */
            $quoteInstaller = $this->_quoteSetupFactory
                            ->create(
                                [
                                    'resourceName' => 'quote_setup',
                                    'setup' => $setup
                                ]
                            );
    
            $this->addQuoteAttributes($quoteInstaller);
            $this->addOrderAttributes($salesInstaller);
        }
    
        /**
         * add attribute in quote address
         * @param object $installer
         */
        public function addQuoteAttributes($installer) {
            $installer->addAttribute('quote_address', 'attribute_name', ['type' => 'text']);
        }
    
        /**
         * add attribute in sales_order
         * @param object $installer
         */
        public function addOrderAttributes($installer) {
            $installer->addAttribute('order', 'attribute_name', ['type' => 'text']);
        }
    }

    here, salesInstaller and quoteInstaller is the instance of SalesSetup and QuoteSetup.
    After that i have passed them on the function to create the attribute.
    here, addQuoteAttributes function is responsible for create quote_address attribute and addOrderAttributes function is responsible for create order attribute.
    here addAttribute function contains three parameters
    first parameter is entity name.
    second parameter is for name of your attribute.it can be anything according to need.
    third parameter is for datatype of the attribute like here my attribute datatype is text.
    Hope so it’ll help you.

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home