1

Is there a way to get the product sku within the app/design/frontend/theme/Magento_Catalog/templates/product/price/amount.phtml file?

I need to retrieve the product sku value there, because i want to insert it to

fbq('track', 'ViewContent', {
content_type: 'product',
content_ids: /* sku value here */

});

2 Answers 2

1

The following code will get the sku of current product

$_product = $block->getProduct()
$productsku = $_product->getSku()

Or u can use object manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product') 
$productsku = $product->getSku();
1
  • When using the object manager my homepage breaks up. but with the first approach you suggested it works. I found myself the solution too. The other approach on solving this is to change block and add the following code in details.phtml <?php echo $block->getProduct()->getSku(); ?> But thank you for your answer i will mark it as solved.
    – vasilisdmr
    Dec 15, 2017 at 9:50
0

This should return current product:
$this->_registry->registry('current_product'), so using $this->_registry->registry('current_product')->getSku() will return sku of current product.
I recommend to use call it in function in Block.

3
  • this breaks my homepage appearance.
    – vasilisdmr
    Dec 14, 2017 at 13:16
  • oh right. Do you need it on product page or everywhere price is showed? Dec 14, 2017 at 13:23
  • I just edited the question. I want to access the sku product value in that file because i need to added to the facebook pixel content_id.
    – vasilisdmr
    Dec 14, 2017 at 13:35

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.