I have product number which ends "_XX". XX is two digit number, ie. "_32". What do I have to change to make that invisible in the product code in product detail page? For example sku is 818833_44. I want it to show up 818833.
If you wanted to display first portion of the sku - like before underscore(_) portion needs to be display - better option is to use strpos.
Try below code :
$sku = $product->getSku(); echo echo substr($variable, 0, strpos($sku, "_"));
It will display only those characters which is before underscore (_) , so when you have large string it will still worked like same.
Thanks but where I should include that code? What file?
Just Create below file in your theme,
app/design/Companyname/Modulename/frontend/templates/Magento_Catalog/templates/product/view/attribute.phtml
Keep below content in attribute.phtml file,
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/**
* Product view template
*
* @see \Magento\Catalog\Block\Product\View\Description
*/
?>
<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_call = $block->getAtCall();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
$renderLabel = true;
// if defined as 'none' in layout, do not render
if ($_attributeLabel == 'none') {
$renderLabel = false;
}
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getStoreLabel();
}
if ($_attributeType && $_attributeType == 'text') {
$_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code)) ? $_product->getAttributeText($_code) : '';
} else {
$_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code);
}
?>
<?php if ($_attributeValue): ?>
<?php if ($_attributeLabel == 'SKU') { ?>
<div class="product attribute <?= /* @escapeNotVerified */ $_className ?>">
<?php if ($renderLabel): ?><strong class="type"><?= /* @escapeNotVerified */ $_attributeLabel ?></strong><?php endif; ?>
<div class="value" <?= /* @escapeNotVerified */ $_attributeAddAttribute ?>><?= /* @escapeNotVerified */ substr($_attributeValue, 0, -3) ?></div>
</div>
<?php } else { ?>
<div class="product attribute <?= /* @escapeNotVerified */ $_className ?>">
<?php if ($renderLabel): ?><strong class="type"><?= /* @escapeNotVerified */ $_attributeLabel ?></strong><?php endif; ?>
<div class="value" <?= /* @escapeNotVerified */ $_attributeAddAttribute ?>><?= /* @escapeNotVerified */ $_attributeValue ?></div>
</div>
<?php } ?>
<?php endif; ?>
Clear cache of system and check.
Rakesh Jesadiya solution works but ManthanDave doesn't. I cannot get sku to show at all.
By Default, In product page, SKU is coming from core catalog module's,
You can check at below path, vendor/magento/module-catalog/view/frontend/templates/product/view/attribute.phtml
There are many dynamic attributes comes from this file so if you need only SKU you have to give conditions in above file,
$_attributeLabel contains the name of the attribute.
I had already given solutions to your query by overriding above core files into the custom theme.
You just need to copy previous answer code to your attribute.phtml file.