Magento 2 - How to replace a char on item title
-
06-03-2021 - |
Question
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.
La solution
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.
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange