1

I want to put an additional sku in the product page.

I have this code:

<?php
$product = $block->getCurrentProduct();
$sku = $product->getData('sku');
if (trim($sku) !== "") : ?>
<div class="product-info-sku">
    <?php echo $sku; ?>
</div>
<?php endif; ?>

but I got this error

Fatal error: Uncaught Error: Call to a member function getData() on null in /var/www/magento/app/design/frontend/Mytheme/custom/Magento_Catalog/templates/product/view/attribute/additional_sku.phtml:3 S

product xml

<referenceContainer name="content">
            <referenceContainer name="product.info.main">
                 <block class="Magento\Catalog\Block\Product\View" name="product.info.additional.sku" template="Magento_Catalog::product/view/attribute/additional_sku.phtml" before="product.info.price"/>
            </referenceContainer>
</referenceContainer>

What did I do wrong? I'm new to Magento

4
  • $product = $block->getData('product'); this $product doesnt have anything, means its an empty object, thats wht its saying call to a member function on null Commented Aug 31, 2022 at 18:02
  • I used the same code with my custom attribute "brand" and it's working fine Commented Aug 31, 2022 at 19:22
  • print_r($product) see, is there sku in it or not? Commented Sep 1, 2022 at 5:12
  • Try $block->getProduct() Commented Sep 1, 2022 at 12:39

1 Answer 1

0

Update your code like this

<?php
$product = $block->getData('product');
$sku = $product->getSku();
if (trim($sku) !== "") : ?>
<div class="product-info-sku">
    <?php echo $sku; ?>
</div>
<?php endif; ?
1
  • it's still showing me the same error Commented Aug 31, 2022 at 17:24

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.