Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

Magento 2 Registry & Register: How to Use?

Vinh Jacker | 12-18-2024

Magento 2 Registry & Register

Magento 2 registry is the next topic Mageplaza wants to introduce in the series of this module development. Both Magento 1 and 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. This article also shows 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.

Hire Magento Developers

What you need to do is only describing desired features of the Magento website, we will help you to build the store that ticks all the boxes!

Get Started
hire magento developers

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

Final words

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

Happy coding!

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services