2

I've been trying without success to prepend the the SKU on the product detail page with 2 characters - R9.

I've already added some customisations to the page using a catalog_product_view.xml but I can't seem to target the block product.info.sku

I think something like this in the theme catalog_product_view.xml should work:

<referenceBlock name="product.info.sku">
    <block class="Magento\Cms\Block\Block" name="skuPrefix">
        <arguments>
            <argument name="skuPrefix" xsi:type="string">skuPrefix</argument>
        </arguments>
    </block>
</referenceBlock>

but it doesn't. I can get the block to appear with this:

<referenceContainer name="product.info.type">
        <container name="custom.container3" before="-">
        <block class="Magento\Catalog\Block\Product\View" name="skuprefix" template="Magento_Catalog::skuprefix.phtml" />
        </container>
</referenceContainer>

but that doesn't put the block where I want it. We're using Luma and we're on 2.3.2. Ideally I'd like the SKU to look like this: SKU#: R912345

0

if you will check vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xmlfile then you will see magento use Magento_Catalog::product/view/attribute.phtml template for sku so you can override this template to your theme and can add prefix from that file.

Add below line after $_code = $block->getAtCode();

$prefix = ($_code && $_code == 'sku') ? 'R9' : '';

and echo that $prefix variable before <?= /* @escapeNotVerified */ $_attributeValue ?> so it will look like :

<?= /* @escapeNotVerified */ $prefix ?><?= /* @escapeNotVerified */ $_attributeValue ?>
| improve this answer | |
  • Thanks Satish, this is a great solution. I'd hoped to figure our how to update the prefix from the admin panel but it only changes once a year so it's not a major issue. – Mark Sep 27 '19 at 9:21

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.