How to get product collection in custom template block |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection'); /** Apply filters here */ $productCollection->load();
|
How to use Mage::log method in Magento 2 |
protected $logger; public function __construct(\Psr\Log\LoggerInterface $logger) {$this->logger = $logger; }
|
You use debug, exception, system for psr logger for example |
$this->logger->info($message); $this->logger->debug($message);
|
How to retrieve product information in Magento 2 |
In Magento 2 proposed to use service layer for this. Try use \Magento\Catalog\Model\ProductRepository::getById
method to get product by id |
How to get a store in Magento 2 programmatically? |
Magento 1.x |
Mage::app()->getStore()
|
function rm_store($id = null) { /** @var \Magento\Framework\ObjectManagerInterface $om */ $om = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Store\Model\StoreManagerInterface $manager */ $manager = $om->get('Magento\Store\Model\StoreManagerInterface'); return $manager->getStore($id); }
|
How to make JavaScript strings translatable (localizable) |
To make JavaScript strings translatable or localizable in Magento 2, use : $.mage.__
|
Example : $.mage.__('Select type of option.') |
Some Magento 2 core modules use another way to translate JavaScript strings: they define/require the mage/translate library |
How to call theme images from a static block |
You call theme images from static block as follows: {{view url=”images/demo.jpg”}} |
How to instantiate a model in Magento 2 |
Magento strictly discourages the use of ObjectManager directly. It provides service classes that abstract it away for all scenarios. For all non-injectables (models) you have to use factory: |
protected $pageFactory; public function __construct(\Magento\Cms\Model\PageFactory $pageFactory) { $this->pageFactory = $pageFactory; } public function someFunc() { ... $page = $this->pageFactory->create(); ... }
|
How to get the сollection of custom Magento 2 modules |
protected $mymodulemodelFactory; public function __construct( .... \[Namespace]\[Module]\Model\ResourceModel\[Entity]\CollectionFactory $mymodulemodelFactory, ... ) { ... $this->mymodulemodelFactory = $mymodulemodelFactory; ... }
|
and you can use in any one of the class methods: |
$collection = $this->mymodulemodelFactory->create(); |
How to get POST and GET requests in Magento 2 |
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: |
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(); } }
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets