We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

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 MagentoFrameworkRegistry. 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:

/**
  * @var MagentoFrameworkRegistry
  */
 
 protected $_registry;
 
 /**
 * ...
 * ...
 * @param MagentoFrameworkRegistry $registry,
 */
public function __construct(
    ...,
    ...,
    MagentoFrameworkRegistry $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 MagentoCatalogModelProduct
 */
 
public function getProduct()
{
    return $this->_registry->registry('product');
}
 
/**
 * Return catalog current category object
 *
 * @return MagentoCatalogModelCategory
 */
 
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.

Fill the below form if you need any Magento relate help/advise/consulting.

With Only Agency that provides a 24/7 emergency support.

    Recent Articles
    Get a Free Quote