In mini cart i am trying to put a custom/formatted text but it reflecting raw html over there, required for magento 2.
Using Magento 2.2.4.
I expect html formatted text but the actual output raw text on minicart
Try following way:
app/code/SR/MagentoCommunity/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">
<type name="Magento\Checkout\CustomerData\DefaultItem">
<plugin name="sr_DefaultItem"
type="SR\MagentoCommunity\Plugin\Checkout\CustomerData\DefaultItem" sortOrder="1"/>
</type>
</config>
app/code/SR/MagentoCommunity/Plugin/Checkout/CustomerData/DefaultItem.php
<?php
namespace SR\MagentoCommunity\Plugin\Checkout\CustomerData;
class DefaultItem
{
public function afterGetItemData(
\Magento\Checkout\CustomerData\DefaultItem $subject,
$item
) {
if (isset($item['options'])) {
$options = $item['options'];
foreach ($options as $key => $option) {
$item['options'][$key]['value'] = html_entity_decode($option['value']);
}
}
return $item;
}
}
Overwrite following template for change render type:
vendor/magento/module-checkout/view/frontend/web/template/minicart/item/default.html
Now modify
<span data-bind="text: option.value"></span>
to
<span data-bind="html: option.value"></span>
[Update]
Add following code in di.xml
<type name="Magento\Checkout\Model\DefaultConfigProvider">
<plugin name="st_config_provider"
type="SR\MagentoCommunity\Plugin\Checkout\Model\DefaultConfigProvider" sortOrder="1"/>
</type>
app/code/SR/MagentoCommunity/Plugin/Checkout/Model/DefaultConfigProvider.php
<?php
namespace SR\MagentoCommunity\Plugin\Checkout\Model;
use Magento\Framework\Serialize\Serializer\Json;
class DefaultConfigProvider
{
/**
* @var Json
*/
private $json;
/**
* DefaultConfigProvider constructor.
* @param Json $json
*/
public function __construct(
Json $json
) {
$this->json = $json;
}
public function afterGetConfig(
\Magento\Checkout\Model\DefaultConfigProvider $subject,
array $result
) {
$items = $result['totalsData']['items'];
foreach ($items as $index => $item) {
if (isset($item['options'])) {
$options = $this->json->unserialize($item['options']);
foreach ($options as $key => $option) {
if (isset($option['full_view'])) {
$options[$key]['full_view'] = html_entity_decode($option['full_view']);
}
}
$item['options'] = $this->json->serialize($options);
$result['totalsData']['items'][$index] = $item;
}
}
return $result;
}
}
OR
Add following code in di.xml
<type name="Magento\Quote\Model\Cart\CartTotalRepository">
<plugin name="checkout_item_sidebar"
type="SR\MagentoCommunity\Plugin\Quote\Model\Cart\CartTotalRepository" sortOrder="1"/>
</type>
app/code/SR/MagentoCommunity/Plugin/Quote/Model/Cart/CartTotalRepository.php
<?php
namespace SR\MagentoCommunity\Plugin\Quote\Model\Cart;
use Magento\Framework\Serialize\Serializer\Json;
class CartTotalRepository
{
/**
* @var Json
*/
private $json;
/**
* DefaultConfigProvider constructor.
* @param Json $json
*/
public function __construct(
Json $json
) {
$this->json = $json;
}
public function afterGet(
\Magento\Quote\Model\Cart\CartTotalRepository $subject,
$quoteTotals
) {
$quoteItems = $quoteTotals->getItems();
$items = [];
/** @var \Magento\Quote\Model\Cart\Totals\Item $item */
foreach ($quoteItems as $index => $item) {
if ($options = $item->getOptions()) {
$options = $this->json->unserialize($options);
foreach ($options as $key => $option) {
if (isset($option['full_view'])) {
$options[$key]['full_view'] = html_entity_decode($option['full_view']);
}
}
$item->setOptions($this->json->serialize($options));
}
$items[$index] = $item;
}
$quoteTotals->setItems($items);
return $quoteTotals;
}
}