Index

Api


Create your own api. Test is with the build in api tester in your magento 2 installation. http://<yourmagento2website>/swagger

WARNING. This api is public. Acl will be added soon.

Back to top

Cache


Custom Cache snippet

Back to top

Categoryattribute


Install Magento 2 category attributes programmatically.

Back to top

Console


Console commands are listed and executed by bin/magento command line tool.

  • action_name: Console action name. Example: Backup, Import
  • short_description: Console action description. Example: Backups the Magento environment, Starts product import

Snippet generation

When you generate a module with an action_name (backup) and the module is named (MageGen/Module). The generated command used by bin/magento is:

bin/magento mage2gen_module:backup
Back to top

Controller


Controller is used to serve a request path. A request path look like this:

www.yourmagentoinstallation.com/frontname/section/action
  • frontname: Configured in the router.xml and must be unique.
  • section: Is a subfolder or folders to the action class.
  • action: An action class that will execute the reqeust.

This snippet will also create a layout.xml, Block and phtml for the action.

Back to top

Cronjob


With this snippet you can create a class with a 'excute' method wich will be executed by the magento cron schedule according to the cron schedule you have configured.

The class will be created within the "Cron" folder of the module.

To build up the cron schedule manually use php bin/magento cron:run

You should find a log in the var/log/system.log after the cronjob has runned.

In the Magento 2 Adminpanel under Stores > Configuration > Advanched > System you change scheduler settings per cron group.

You can create your own groups if you wish. In that case be sure to add extra system settings.

Instead of the <schedule> tag in the crontab.xml you can set a system config path

Example

<config_path>crontab/default/jobs/catalog_product_alert/schedule/cron_expr</config_path>

This way a admin user can configure the cronschedule for this task.

Back to top

Customerattribute


With this snippet you can create customer and customer address attribute programmatically thru a Magento 2 InstallData setup script. You can asign them to the forms where the should appear.

Warning. Not all template files are setup to load customer or customer address attributes dynamically.

Magento 2 create customer attribute programmatically

Back to top

Install


Install is used for creating database tables and adding data to Magento 2. It uses Schema and Data install and upgrade classes.

  • from_version: Add sample upgrade from version statement

Snippet generation

When you generate a module, the following classes will be created:

Install scripts

  • Setup/InstallSchema
  • Setup/InstallData

Upgrade scripts (With the sample from_version statement)

  • Setup/UpgradeSchema
  • Setup/UpgradeData
Back to top

Language


Magento 2 uses csv files for translations per language. This snippet will generate a translation csv file for selected language with one example translation.

  • language: Language for translation file.

Snippet generation

When you generate a module for the language English (United States), it will create a csv translation file in i18n/en_US.csv

Back to top

Model


Model is used to create a easie CRUD interface to the database

  • Model ame: The name of the model, the table name wil be <module_name>_<model_name>.
  • Field name: The name of the database table field.
  • Field type: The type of database field.
  • Adminhtml grid: Add this field to the adminhtml grid layout

Model ID field: The snippet will auto add the model id field to the database table, the field name is <model_name>_id.

Back to top

Observer


With observers you can hook in on events fired by Magento or other third party modules. For creating an observer we need the event name and the scope:

  • Event name: Name of event you want to observe, example: catalog_product_save_after

  • Scope: For which scope the observer is active.
    • All: Observer is always active
    • Frontend: Observer is only active in the frontend of Magento.
    • Backend: Observer is only active in the admin panel of Magento.

Some events:

  • sales_order_place_before
  • sales_order_place_after
  • checkout_cart_product_add_after
  • checkout_cart_update_items_before
  • checkout_cart_save_before
  • catalog_product_get_final_price
Back to top

Payment


Creates a payment method

Generated Payment methods can be found in Magento Adminpanel > Stores > Settings > Configuration > Sales > Payment Methods

It allows you to add extra payment methods to Magento. For example if you need to have a payment method which can only be used in the backend or if you need a payment that directly creates an invoice.

Back to top

Plugin


Creates a Plugin

Plugins are designed to overwrite core Magento methods or methods from other 3rd party modules.

You can choose to change it before, after, or around the original method is called.

Example

Change the product name to show pipes before and after the name.

Input for the plugin form

  • classname: MagentoCatalogModelProduct
  • methodname: getName
  • plugintype: After
public function afterGetName(
        Magento\Catalog\Model\Product $subject,
        $result
){
        return '|'.$result.'|';
}
Back to top

Productattribute


Install Magento 2 product attributes programmatically.

The attribute is automatically added to all the attribute sets.

Back to top

Shipping


Creates a basic Magento 2 shipping method.

Generated Shipping methods can be found in Magento Adminpanel > Stores > Settings > Configuration > Sales > Shipping Methods

It allows you to write your own price logic to calculate the shipping cost. It is written in /Model/Carries/Generatedshippingmethod.php

Example:

if you want to calculate the shipping cost based on the shipping cost per product in the customers basket.

$items = $request->getAllItems();
$cost = 0;

foreach($items as $item){
        $cost += $item->getProduct()->getShippingCost();
}
Back to top

System


System config is used in Magento for storing settings to use in your module.

For example an option to enable and disable your module.

Generated configuration can be found in Magento Adminpanel > Stores > Settings > Configuration

To retrieve the value you can use the xml path yourmodulename/general/enabled

Example:

$this->_scopeConfig->getValue('yourmodulename/general/enabled', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

(Depends on MagentoFrameworkAppConfigScopeConfigInterface)

Field Types:

  • Select
  • Multiselect
  • Text
  • Textarea

For Select and Multiselect you will need to define a source model. By default this will be this will be the core Magento yes/no.

Back to top

Unittest


Unit tests are runned with magento dev:tests:run <test> and is used to test your code in development.

  • Test suite: A class with a collection of tests
  • Test name: The name of a test
Back to top

Widget


Back to top