5

I would like to add some additional fields to the newsletter table. How do I go about rewriting the Newsletter subscriber table to add those fields?

I am expecting to add things like firstname, last name etc.

closed as too broad by Keyur Shah, Qaisar Satti, Adarsh Khatri, saravanavelu, Sander Mangel Dec 21 '15 at 7:19

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

0
  1. Add custom setup script which will add proper column to core table or better to separate table (see Magento\Newsletter\Setup\InstallSchema as an example)
  2. Declare your new field as extension attribute, see details in this answer
  3. Use new as an extension attribute whenever necessary
  • I've managed to create my custom module with custom database table newsletter_subscriber_detail. But is extensions only for API? I am trying to get the fields usable with the subscriber model – LuckyKnight Jan 8 '16 at 14:28
  • Extension attributes are only for service contracts API. If you don't want to define/use API, it should be possible to use plugins/events to load additional data during model load. If new fields are defined in the main table, they will most likely be loaded automatically (assuming SELECT * was used). – Alex Paliarush Jan 8 '16 at 20:18
  • If I look at Magento\Newsletter it does not have a API folder directly underneath so I would assume it doesn't. The issue I have is that I would normally need to extend the controller to get the additional form fields, then extend the model to save those attributes. Is this still the case because no API is defined? I am still reading up on plugins. – LuckyKnight Jan 12 '16 at 9:54
  • So If write a plugin to intercept the subscribe function in the newsletter module, I can do this but I don't see how you then get the extra params which are normally handled in the controller, then the subscribe function is called with those extra params – LuckyKnight Jan 12 '16 at 10:27
  • I suppose, you would have to modify controller/forms anyway (even if API were defined). How about asking separate question with concrete problem description? – Alex Paliarush Jan 12 '16 at 14:40
0

below is code which will help you i gave example of adding field to orders table

in app\code\Sugarcode\Test\Setup\UpgradeSchema.php

if (version_compare($context->getVersion(), '2.0.5', '<')) {
                $connection = $setup->getConnection();
                $tableNames=['sales_order_item','sales_invoice_item','quote_item'];

                  // Declare data
                $columns = [
                    'custom_sku' => [
                        'type' => Table::TYPE_TEXT,
                        'nullable' => false,
                        'LENGTH' =>255,
                        'comment' => 'custom sku',
                    ],
                ];
                $connection = $setup->getConnection();
                foreach ($tableNames as $tableName) {
                    foreach ($columns as $name => $definition) {
                        $connection->addColumn($tableName, $name, $definition);
                    }
                }


                $tableNames=['sales_order','sales_invoice','quote'];

                  // Declare data
                $columns = [
                    'custom_field' => [
                        'type' => Table::TYPE_TEXT,
                        'nullable' => false,
                        'LENGTH' =>255,
                        'comment' => 'custom Field',
                    ],
                ];
                $connection = $setup->getConnection();
                foreach ($tableNames as $tableName) {
                    foreach ($columns as $name => $definition) {
                        $connection->addColumn($tableName, $name, $definition);
                    }
                }


                // Changes here.
        }

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