0

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

Detail Page

minicart

1 Answer 1

1

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>

Output: enter image description here

[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;
    }
}
5
  • Now it's working on minicart, but still not working checkout page. Commented May 28, 2019 at 19:34
  • Your question is about minicart, why you test in checkout page. For other page like checkout, cart page etc you need to do same things.
    – Sohel Rana
    Commented May 28, 2019 at 21:49
  • Please mention checkout page method for plugin. Commented May 29, 2019 at 5:47
  • Check updated answer
    – Sohel Rana
    Commented May 29, 2019 at 7:36
  • Is it working for you? If yes, accept this answer that will help other.
    – Sohel Rana
    Commented May 30, 2019 at 6:35

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.