I am trying to get current product SKU in controller using the below code:

namespace Test\ProductsToCart\Controller\Product;

class Styles extends \Magento\Framework\App\Action\Action
    protected $data;
    protected $_registry;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Framework\Registry $registry,
        array $data = []
    ) {
        $this->resultJsonFactory = $resultJsonFactory;
        $this->_registry = $registry;
        parent::__construct($context, $data);

    public function execute()
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $prid = $this->_registry->registry('current_product');
        $pr1id = $prid->getSku();
        echo $pr1id;

but couldn't get the result.

Is there any problem with this line of code?

 $prid = $this->_registry->registry('current_product'); 

Also tried:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
echo $product->getId();

Getting error like:

Call to a member function getId() on null

1 Answer 1


Your code will work fine, if the current product has already been registered in some other part of the code.

However, if you are sending a query to your Controller, and the product hasn't been registered yet, you'll get 'null' instead of the SKU value.

If you are sending an AJAX query from a product page, you can send a product ID in the parameters and get a product from the repo the following way:

 * @var $this->productRepository \Magento\Catalog\Model\ProductRepository
 * @var $productId int
$product = $this->productRepository->getById($productId);
if ($product) {
    $sku = $product->getSku();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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