0

I have a product custom attribute, want to write in session when then product detail page was load. So I can get use the session value on the other block on the same page. Anyone can give me an advise?

0

You can set the custom value in registry instead. On product detail page, the product object is already present in the registry.

/**
 * @var \Magento\Framework\Registry
 */
protected $_registry;
 
/**
 * @param \Magento\Framework\Registry $registry,
 */

public function __construct(
    \Magento\Framework\Registry $registry,
) {
    $this->_registry = $registry;
}

/**
 * Set custom variable in registry
 *
 */
public function setCustomVariable()
{
    $this->registry->register('custom_variable', 'Test Value');
}
 
/**
 * Get custom variable from registry
 * @return string
 */
public function getCustomVariable()
{
     return $this->registry->registry('custom_variable');
}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.