i am adding a phtml with after_body_start reference

how do I display the SKU of a product if the phtml is being added to a product page?


There may be a more efficient way depending on how you added this phtml, but if it only shows on a product page, try getting the product like this:

$current_product = Mage::registry('current_product');
if($current_product) {
    $sku = $current_product->getSku();
    // output sku
    echo $sku;
  • i am a new user so i could not accept for ten minutes did it now thanks – Yehuda Schwartz Nov 18 '15 at 17:54
  • Good Edit Abdul – Amit Bera Nov 19 '15 at 7:17
  • actually the get Data('sku') format was better for me as they can choose any product attribute as the output for this space <?php $att = Mage::getStoreConfig('options/messages/attribute'); ?> <?php echo Mage::registry('current_product')->getData($att); ?>"]); – Yehuda Schwartz Nov 19 '15 at 14:23
  • Yeah, I agree on getData, but he added the if($current_product) check that I didn't have. That check was needed – TJ Gamble at Jamersan Nov 19 '15 at 14:27

You can try this is code below:

$_product = $this->getProduct();
$sku = $_product->getSku(); 
  • Whether or not this works depends on what block he is using when he adds it to the page. – TJ Gamble at Jamersan Nov 18 '15 at 17:40
  • tried that does not work – Yehuda Schwartz Nov 18 '15 at 17:44
  • I edit this code, because uppercase $sku = $_product->getSku(); it not work. If you in product detail page, it using block Mage_Catalog_Block_Product_View – Thao Pham Nov 18 '15 at 17:56

This code is working for me in magento 2.

    if($_product) {
      $sku = $_product->getSku();
      echo $sku;

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.