0

If we have to display product name with superscript tag like "®","™", Magento 2 doesn't support and it display as it is without parsing tag.

For example, if we have to display product name "Magento Allowed<sup>®</sup> Superscript" :-

enter image description here,

then from backend product section if we have directly add "Magento Allowed<sup>®</sup> Superscript" as product name:-

Then it will display as it is without parsing on browser :-

enter image description here

0

This is the Magento issue and to fixed this, please follow following four steps :-

1- Define global plugin in di.xml file :-

<type name="\Magento\Framework\Escaper">
        <plugin name="Escaper-Plugin"
                type="Waqar\Catalog\Plugin\Product\EscaperPlugin" disabled="false" sortOrder="0" />
    </type> 

2- EscaperPlugin.php :-

<?php

namespace Waqar\Catalog\Plugin\Product;

use Magento\Framework\Escaper;
use Waqar\Catalog\Helper\Config as ConfigHelper;

class EscaperPlugin
{
    const ALLOWED_SUPERSCRIPT_TAG = ['sup'];
    const SUPERSCRIPT_TAG = '<sup>';

     /**
     * @var ConfigHelper
     */
    protected $configHelper;

    /**
     * @param ConfigHelper $configHelper
     */
    public function __construct(
        ConfigHelper $configHelper
    ) {
        $this->configHelper = $configHelper;
    }

    public function beforeEscapeHtml(
        Escaper $subject,
        $data,
        $allowedTags = null
    ) {
        if ($this->configHelper->superScriptConfigAllowed() && $this->strDataContainsSuperScript($data)) {
            $allowedTags = $this->getAllowedTags($allowedTags);
        }

        return [$data, $allowedTags];
    }

    /**
     * Function return if data type is string and it contains superscipt tag
     *
     * @return boolean
     */

    public function strDataContainsSuperScript($data): bool
    {
        if (is_string($data) && strpos($data, self::SUPERSCRIPT_TAG) !== false) {
            return true;
        }

        return false;
    }

    /**
     * return allowed tags
     *
     * @return array
     */

    public function getAllowedTags($allowedTags)
    {
        if ($allowedTags == null) {
            $allowed_tags = self::ALLOWED_SUPERSCRIPT_TAG;
        } else if (is_array($allowedTags)) {
            $allowed_tags = array_merge($allowedTags, self::ALLOWED_SUPERSCRIPT_TAG);
        }

        return $allowed_tags;
    }
}

3-Config.php :-

<?php

namespace Waqar\Catalog\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * Class Config
 * @package Waqar\Catalog\Helper
 */
class Config extends AbstractHelper
{
    /** @var string  */
    const ALLOWED_SUPERSCRIPT_TAG_CONFIG_PATH = 'catalog/catalog_superscript_group/allowed_superscript_with_product_name';

    /** @var StoreManagerInterface  */
    protected $storeManager;

    /**
     * Config constructor.
     * @param Context $context
     * @param StoreManagerInterface $storeManager
     */
    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager
    ) {
        parent::__construct($context);

        $this->storeManager = $storeManager;
    }

    /**
     * Function return if current website allowed superscript with product name
     *
     * @return boolean
     */

    public function superScriptConfigAllowed(): bool
    {
        $website_id = $this->getWebsiteId();

        $result = $this->scopeConfig->getValue(
            self::ALLOWED_SUPERSCRIPT_TAG_CONFIG_PATH,
            ScopeInterface::SCOPE_WEBSITE,
            $website_id
        );

        return (bool) $result;
    }

    /**
     * return current website id
     *
     * @return int
     */
    protected function getWebsiteId()
    {
        $store = $this->storeManager->getStore();

        return $store->getWebsiteId();
    }
}

4-adminhtml/system.xml :-

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="catalog" translate="label" type="text" sortOrder="40   " showInDefault="1" showInWebsite="1"
                 showInStore="1">
            <group id="catalog_superscript_group" translate="label" type="text" sortOrder="2000" showInDefault="1" showInWebsite="1" showInStore="1">
                <label><![CDATA[Allow Superscript with Product Name]]></label>
                <field id="allowed_superscript_with_product_name" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Allow Superscript with Product Name</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    <comment>Allow Superscript like ®,™ with Product Name</comment>
                </field>
            </group>
    </system>
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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