Besides the improvements, Magento 2 still has the old features like allow you to have the registry to register global variable using stable registry method.
However, instead of Mage::registry, in Magento 2, it has become \Magento\Framework\Registry. There are two main methods to set registry variable such as register for installing and registry for restoring data.
In this post, we are going to show you the way to create or use your own custom registry & retrieve global Magento 2 registry objects like category, cms page, current product, cms block, et cetera.
You just need to follow code snippet below in order to work with registry objects in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
/** * @var \Magento\Framework\Registry */ protected $_registry; /** * ... * ... * @param \Magento\Framework\Registry $registry, */ public function __construct( ..., ..., \Magento\Framework\Registry $registry, ... ) { $this->_registry = $registry; ... ... } /** * Setting custom variable in registry to be used * */ public function setCustomVariable() { $this->registry->register('custom_var', 'Added Value'); } /** * Retrieving custom variable from registry * @return string */ public function getCustomVariable() { return $this->registry->registry('custom_var'); } /** * Return catalog product object * * @return \Magento\Catalog\Model\Product */ public function getProduct() { return $this->_registry->registry('product'); } /** * Return catalog current category object * * @return \Magento\Catalog\Model\Category */ public function getCurrentCategory() { return $this->_registry->registry('current_category'); } |
It is quite a simple tutorial. Believe that you can apply it in the same way.
[crayon-63d3d3953cacd510971594/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.