"magento 2"

Adding a new product type in Magento 2

- Magento 2

Magento 2 Development

Create etc/product_types.xml in your module to add a new product type in Magento 2:

The second step requires creating the modelInstance:

More tips from Magento 2 Developer’s Cookbook

Continue Reading

How to remove block from a Magento 2 layout

- Magento 2

Magento 2 Development

Remove instruction is available in Magento 2. Please note that the layout merges in order shown in a module.xml/sequence section. Here is the instruction:

More tips from Magento 2 Developer’s Cookbook

Continue Reading

How to clean LESS/CSS Theme cache in Magento 2

- Magento 2

Magento 2 Development

Right in refresh .less files, since it “collects, processes and publishes source LESS files”:

You can also use dev mode during development. According to documentation “Static view files are not cached; they are written to the Magento pub/static directory every time they’re called.”

More tips from Magento 2 Developer’s Cookbook

Continue Reading

How to get POST and GET requests in Magento 2

- Magento 2

Magento 2 Development

In a case of a controller that extends Magento\Framework\App\Action\Action, it is possible to get the request with the aid of $this->getRequest()->getPost().

For a custom class, inject the request in the constructor:

More tips from Magento 2 Developer’s Cookbook

Continue Reading

How to replace LESS CSS Preprocessor with SASS or another one in Magento 2

- Magento 2

Magento 2 Development

In Magento 2, the LESS PHP adapter is located in \lib\internal\Magento\Framework\View\Page\Config\Renderer, and you can easily replace it with another one (SASS for instance) by updating the following lines of code in the di.xml configuration:

More tips from Magento 2 Developer’s Cookbook

Continue Reading

How to change a template for a standard block

- Magento 2

Magento 2 Development

By calling setTemplate method of course!

More tips from Magento 2 Developer’s Cookbook

Continue Reading

How to defer JavaScript execution in Magento 2

- Magento 2

Magento 2 Development

Being a boolean attribute, defer specifies that the script is executed after the page parsing. The attribute is aimed at external scripts only (the src attribute should be present). Find more information about defer here.

Despite Magento 2 supports standard script deferring, it never uses defer in its core. To change the situation, you can use the following code:

More tips from Magento 2 Developer’s Cookbook

Continue Reading