The product of the current product page, for view models and blocks — without the deprecated core registry (Magento\Framework\Registry, Magento\Catalog\Helper\Data::getProduct()).
An observer on catalog_controller_product_init_after (storefront) keeps the product that the product page controller has just loaded. Inject Webcode\CurrentProduct\Model\Catalog\CurrentProduct and call get():
public function __construct(
private readonly \Webcode\CurrentProduct\Model\Catalog\CurrentProduct $currentProduct
) {
}
$product = $this->currentProduct->get();Outside a product page get() returns an empty product (no id), never null.
Used by other Webcode modules, for example webcode/module-breadcrumbs.
Webcode's free modules are installed from our own Composer repository — they are not published on Packagist.
composer config repositories.webcode composer https://repo.webcode.bg/public
composer require webcode/module-current-product
bin/magento module:enable Webcode_CurrentProduct
bin/magento setup:upgradeMagento Open Source and Mage-OS 2.4.7–2.4.9, PHP 8.3–8.5.
OSL-3.0 and AFL-3.0, like Magento itself.
Made by Webcode, a Magento development and support studio from Bulgaria.