Magento 2 Registry & Register

Magento 2 registry is the next topic Mageplaza wants to introduce in the series of this module development. Magento 1, as well as Magento 2, authorize you to register global variable that supports the static registry method.

To implement that, maybe you used to work with Mage::register() and Mage::registry() in Magento 1, but now in Magento 2 platform, there is a difference in running the registry. You will be required to apply \Magento\Framework\Registry, that accepts the settings and the registry of the restored data. However, first of all, you need to learn how to create or use the own custom registry and also show you how to retrieve global Magento 2 registry objects like current product, category, cms page, cms block, etc.

And that is lucky because all of them will be referred here. The topic today will help you be familiar with Magento 2 registry objects.

Please follow the following code snippet:

How to get and set custom attribute in registry / register

/**
  * @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');
}

How to get registry of current Product, Category, CMS Page


/**
  * @var \Magento\Framework\Registry
  */
 
 protected $_registry;
 
 /**
 * ...
 * ...
 * @param \Magento\Framework\Registry $registry,
 */
public function __construct(
    ...,
    ...,
    \Magento\Framework\Registry $registry,
    ...
) {
    $this->_registry = $registry;
    ...
    ...
}

/**
 * Return catalog product object
 *
 * @return \Magento\Catalog\Model\Product
 */
 
public function getProduct()
{
    return $this->_registry->registry('current_product');
}
 
/**
 * Return catalog current category object
 *
 * @return \Magento\Catalog\Model\Category
 */
 
public function getCurrentCategory()
{
    return $this->_registry->registry('current_category');
}


/**
 * Return catalog current cms page object
 *
 */
public function getCurrentCategory()
{
    return $this->_registry->registry('current_cms_page');
}

With this instruction, you can achieve the best results of Magento 2 registry. Thanks for your watching and hope you are happy with it.

Happy coding!





Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
registry
register

Comments for Magento 2 Registry & Register

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

People also searched for:

  • magento 2 registry
  • magento 2 register
  • magento 2 registry not working
  • magento 2 registry key already exists
  • magento 2 registry current category
  • magento 2 registry current product
  • magento 2 registry is undefined
  • magento 2 register variable
  • magento 2 registry product
  • magento 2 registry set get
  • magento 2 registry get current product
  • magento 2 registry
  • magento 2 change registry value
  • magento 2 mage registry current category
  • magento 2 registry database
  • magento 2 registry.get(eventname)
  • magento 2 wedding registry extension
  • magento 2 registry functions
  • magento 2 gift registry free
  • magento 2 get registry value
  • magento type error registry is undefined
  • mage registry in magento 2
  • magento 2 prototype registry is undefined
  • magento 2 check if registry key exists
  • magento 2 prototype js registry is undefined
  • magento 2 prototype typeerror registry is undefined