How to Develop a Custom Sample Data Module for Magento 2

Mar 6, 2018451Alex Simonchik
How to Develop a Custom Sample Data Module for Magento 2

Today we’re going to discuss designing Sample Data module to create data for Belvg_WelcomeAgreement demo module. The case is that we use Gitlab CI to automatically create a demo for the module. Additionally, we wanted database data to be created simultaneously, so we decided to design Sample Data module using Magento 2 Way.

You might have seen Sample Data modules designed by Magento team, for example, Magento_CmsSampleData, Magento_ConfigurableSampleData, Magento_MsrpSampleData etc. Similar to these examples, we’ve designed our own module, the code of which you can find on github.

Let’s consider each file separately:

  • Belvg/WelcomeAgreementSampleData/registration.php – file which records a new module in the system.
  • Belvg/WelcomeAgreementSampleData/module.xml – one more file every module for Magento 2 must have. Here we specify Belvg_WelcomeAgreementSampleData dependence on Belvg_WelcomeAgreement in order to set the order of module installation.

  • Belvg/WelcomeAgreementSampleData/Setup/InstallData.php –  file which with the help of Dependency Injection (DI) creates the object installing interface Setup\SampleData\InstallerInterface.
  • Belvg/WelcomeAgreementSampleData/Setup/Installer.php – class which installs the interface and и triggers data import for \Belvg\WelcomeAgreementSampleData\Model\Agreement by using fixtures/belvg_welcomeagreement_agreement.csv.
  • Belvg/WelcomeAgreementSampleData/Model/Agreement.php – class which represents install method responsible for data import:

Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

We used $this->agreementFactory object delivered on via DA while being the sample of the original Belvg_WelcomeAgreement module. Agreement entity has the following structure:

Besides,  belvg_welcomeagreement_agreement_store table is used for keeping agreements <-> stores.

In order to install the module, you need to run the commands (having Belvg_WelcomeAgreement module):

  • php bin/magento module:enable Belvg_WelcomeAgreementSampleData
  • php bin/magento setup:upgrade

Partner With Us Looking for a partner that will help you to grow your business? We are the right company to develop your webstore. Feel free to get in touch with us. We will be happy to discuss your business opportunities and provide you with a Free Quote. Talk to Andrey

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *