Add a 2 character prefix to the SKU on the product detail page
-
14-03-2021 - |
سؤال
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
المحلول
if you will check vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml
file 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 ?>