Skip to content

Modify Product Title Display

I want to prepend a text before the product name is displayed.

I’ve created a file product-title.phtml in my local template and referenced this in catalog_product_view.xml like so:

    <referenceBlock name="page.main.title">
        <arguments>
            <argument name="template" xsi:type="string">Local/argento-stripes-custom/Magento_Catalog::product/view/product-title.phtml</argument>
        </arguments>
    </referenceBlock>

Unfortunately it’s still rendering the default template title file.

Magento 2.2.7

Any suggestions?

Thanks,
Eddie

I tried implementing Ravi’s code as suggested, but that seemed to kill all titles from being displayed. But using that as a base I’ve tried the following:

Created a module Fws_Title

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="MagentoThemeBlockHtmlTitle" type="FwsTitleBlockHtmlTitle" />
</config>
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Fws_Title" setup_version="1.0.3">
    </module>
</config>

My Block code is at FwsTitleBlockHtml & as follows:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace FwsTitleBlockHtml;

use MagentoFrameworkViewElementTemplate;
use MagentoFrameworkRegistry;

/**
 * Html page title block
 *
 * @method $this setTitleId($titleId)
 * @method $this setTitleClass($titleClass)
 * @method string getTitleId()
 * @method string getTitleClass()
 * @api
 * @since 100.0.2
 */
class Title extends Template
{
    /**
     * Own page title to display on the page
     *
     * @var string
     */
    protected $pageTitle;
    protected $_registry;

    public function __construct(
        MagentoFrameworkViewElementTemplateContext $context,
        MagentoFrameworkRegistry $registry,
        array $data = []
    )
    {
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }

    /**
     * Provide own page title or pick it from Head Block
     *
     * @return string
     */
    public function getPageTitle()
    {
        if (!empty($this->pageTitle)) {
            return $this->pageTitle;
        }
        return __($this->pageConfig->getTitle()->getShort());
    }

    /**
     * Provide own page content heading
     *
     * @return string
     */
    public function getPageHeading()
    {
        if (!empty($this->pageTitle)) {
            return __($this->pageTitle);
        }
        return __($this->pageConfig->getTitle()->getShortHeading());
    }

    /**
     * Set own page title
     *
     * @param string $pageTitle
     * @return void
     */
    public function setPageTitle($pageTitle)
    {
        $this->pageTitle = $pageTitle;
    }

    public function getProduct()
    {
        if($this->registry->registry('current_product')) {
            return $this->registry->registry('current_product');
        }
        return false;
    }

}

My template is in my custom template directory at Magento_ThemeTemplateHtmlTitle.phtml and looks like this:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var $block FwsTitleBlockHtmlTitle
 */
$cssClass = $block->getCssClass() ? ' ' . $block->getCssClass() : '';
$title = '';

$_product = $block->getProduct();
if (trim($block->getPageHeading())) {
    if($_product){
        $title = '<span class="base fws-product" data-ui-id="page-title-wrapper" ' .  $block->getAddBaseAttribute() . '>'
            . '<span class="item-title">Item No:</span>' .$block->escapeHtml($block->getPageHeading()) . '</span>';
    }else{
        $title = '<span class="base fws-cms" data-ui-id="page-title-wrapper" ' .  $block->getAddBaseAttribute() . '>'
            . $block->escapeHtml($block->getPageHeading()) . '</span>';
    }

}
?>
<?php if ($title): ?>
<div class="page-title-wrapper<?= /* @escapeNotVerified */ $cssClass ?>">
    <h1 class="page-title"
        <?php if ($block->getId()): ?> id="<?= /* @escapeNotVerified */ $block->getId() ?>" <?php endif; ?>
        <?php if ($block->getAddBaseAttributeAria()): ?>
            aria-labelledby="<?= /* @escapeNotVerified */ $block->getAddBaseAttributeAria() ?>"
        <?php endif; ?>>
         <?= /* @escapeNotVerified */ $title ?>
    </h1>
    <?= $block->getChildHtml() ?>
</div>
<?php endif; ?>

I know the code is not working because I’ve added an extra class into the title span & this is not showing on the front end.

So that would suggest my code is not being picked up at all?

Any ideas,
Thanks again,
Eddie