I am trying to insert a <div> into a product page which does this.

<div class="thisdiv" id="mydiv" data-sku="<?php echo $item->getSku(); ?>">

I am ok with putting the <div> into a custom template file and then using the layout XML to display that, but how would I get the product sku of the product to appear in the tag?

I've just got this so far in my template file and just get a blank page.

    $item = $block->getProduct();
<div class="thisdiv" id="mydiv" data-sku="<?php echo $item->getSku(); ?>">



Sorted it myself but for my reference...

Template file goes here:


Template looks like this...

    $_helper = $this->helper('Magento\Catalog\Helper\Output');
    $_product = $block->getProduct();
    $_code = $block->getAtCode();
    $_className = $block->getCssClass();
    $_attributeLabel = $block->getAtLabel();
    $_attributeType = $block->getAtType();
    $_attributeAddAttribute = $block->getAddAttribute();

<div class="thisdiv" id="mydiv" data-sku="<?php echo $_product->getSku(); ?>">

catalog_product_view.xml in Magento_Catalog/layout has this...

<block class="Magento\Catalog\Block\Product\View\Description" name="mydiv" template="product/view/mydiv.phtml" after="-" />

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.