2

I have succesfully used this trick to replace dash char "-" with "br" on CATEGORY VIEW:

<?php echo str_replace(' - ', '<br />', $_helper->productAttribute($_product, $_product->getName() , 'name')); ?>

result --> https://dev.push96.com/uomo.html

But now how can I use the same trick on the PRODUCT PAGE title? --> https://dev.push96.com/uomo/tommy-jeans-uomo-t-shirt-in-puro-cotone-giallo-con-logo-dm0dm05125094.html

I need the same replace "-" with line break but I can't find where to put the code.

1
  • use "Template hints", you will know the location of file where product title is located. Then edit title Commented Jul 2, 2019 at 12:08

1 Answer 1

3

You can add your code here:

/app/code/Custom/module/etc/di.xml

<?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="Magento\Theme\Block\Html\Title" type="Custom\Module\Block\Html\Title" />
</config>

/app/code/Custom/Module/Block/Html/Title.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Custom\Module\Block\Html;

use Magento\Framework\View\Element\Template;

/**
 * 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;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry
    )
    {
        parent::__construct($context);
        $this->registry = $registry;    
    }

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

Now you can update your prodcut name here

/app/design/frontend/Custom/Theme/Magento_Theme/templates/html/title.phtml

$product = $block->getCurrentProduct();

And then apply your logic there.

3
  • I have updated my answer, please check
    – Ravi Soni
    Commented Jul 2, 2019 at 12:31
  • Your reply opened my mind and I got an illlumination :) I just overrided the file "/app/design/frontend/vendor/theme/mytemplate/Magento_Theme/templates/html/title.phtml" and it works!
    – Silver
    Commented Jul 2, 2019 at 13:55
  • Welcome :). And +1 for accepting as answer
    – Ravi Soni
    Commented Jul 2, 2019 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.