Adding custom CSS/JS to your Magento 2 modules
There are several options for adding custom CSS/JS to your Magento 2 modules, and the following one is the easiest. Continue Reading

There are several options for adding custom CSS/JS to your Magento 2 modules, and the following one is the easiest. Continue Reading

You can easily flush the cache storage of Magento 2 from the command line. Note that the following script must be executed from the Magento 2 root folder:
|
1 2 |
find var/* -type f -or -type d | grep -v 'session' | xargs rm -rf && \ rm -rf pub/static/* |
It deletes all content from the var subfolder excluding the.htaccess file and the var/session subfolder. Besides, the script also deletes all content from the pub/static subfolder excluding the.htaccess file.

To create a message to a system log in Magento 2.0, use the code listed below:
|
1 2 3 4 |
\Magento\Framework\App\ObjectManager::getInstance() ->get('Psr\Log\LoggerInterface') ->debug('message') ; |
In Magento 1, for instance, this code was more simple:
|
1 |
Mage::log('message'); |
but you can also utilize another approach in case of Magento 2. Since there is a _logger property related to many objects, you can perform the same task by calling inside such objects. Just use the following code:
|
1 |
$this->_logger->debug('message'); |
Please note that not every object has the _logger property. Thus, this method will not work with all your objects.

Create etc/product_types.xml in your module to add a new product type in Magento 2:
|
1 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Catalog/etc/product_types.xsd"> <type name="demoproduct" label="Demo Product" modelInstance="Genmato\DemoProduct\Model\Product\Type\Demo" indexPriority="25" sortOrder="25"> <customAttributes> <attribute name="refundable" value="true"/> </customAttributes> </type> </config> |
The second step requires creating the modelInstance:
|
1 |
/** * @category Genmato * @package Genmato_MageStackProduct * @copyright Copyright (c) 2015 Genmato BV (https://genmato.com) */ namespace Genmato\DemoProduct\Model\Product\Type; class Demo extends \Magento\Catalog\Model\Product\Type\AbstractType { /** * Delete data specific for Simple product type * * @param \Magento\Catalog\Model\Product $product * @return void */ public function deleteTypeSpecificData(\Magento\Catalog\Model\Product $product) { } } |

To set a Magento 2 mode, use the following command:
|
1 |
magento setup:mode:set {mode} [-s|--skip-compilation] |

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:
|
1 |
<?xml version="1.0"?> <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/layout_generic.xsd"> <remove name="dashboard"/> </layout> |

You can learn about Magento 2 Request Flow from this presentation by Anton Kril:

Right in refresh .less files, since it “collects, processes and publishes source LESS files”:
|
1 |
php bin/magento dev:css:deploy |
You can also use dev mode during development. According to “Static view files are not cached; they are written to the Magento pub/static directory every time they’re called.”

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:
|
1 |
<?php namespace Namespace\Module\Something; class ClassName { protected $request; public function __construct( \Magento\Framework\App\Request\Http $request, ....//rest of parameters here ) { $this->request = $request; ...//rest of constructor here } public function getPost() { return $this->request->getPost(); } } |

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:
|
1 |
<item name=”server_side_compilation” xsi:type=”string”>Magento\Framework\View\Page\Config\Renderer</item> |