1

I need to change all product zero prices to custom text - free and enable/disable this function from admin?

  • Do you want to text instead of 0 in frontend PDP? – Sohel Rana Apr 25 at 14:33
  • Yes, i want to show them in frontend in all pages where is product. – Nata Apr 25 at 14:36
0

This is quite big. But you can go with following way:

For listing and PDP:

app/code/SR/MagentoCommunity/etc/frontend/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\Framework\Pricing\Render\PriceBox">
        <plugin name="change_text_for_free_product" type="SR\MagentoCommunity\Plugin\Pricing\Render\PriceBox" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/Pricing/Render/PriceBox.php

<?php
namespace SR\MagentoCommunity\Plugin\Pricing\Render;

use Magento\Framework\Pricing\Amount\AmountInterface;

class PriceBox
{
    public function aroundRenderAmount(
        \Magento\Framework\Pricing\Render\PriceBox $subject,
        \Closure $proceed,
        AmountInterface $amount,
        array $arguments = []
    ) {
        if ($subject->getPrice()->getValue() <= 0) {
            return 'Free';
        }

        return $proceed($amount, $arguments);
    }
}

For Minicart overwrite following template and add logic:

vendor/magento/module-weee/view/frontend/templates/checkout/cart/item/price/sidebar.phtml

For checkout sidebar:

app/code/SR/MagentoCommunity/view/frontend/layout/checkout_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="sidebar" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="summary" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="cart_items" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="details" xsi:type="array">
                                                            <item name="children" xsi:type="array">
                                                                <item name="subtotal" xsi:type="array">
                                                                    <item name="children" xsi:type="array">
                                                                        <item name="weee_row_incl_tax" xsi:type="array">
                                                                            <item name="config" xsi:type="array">
                                                                                <item name="template" xsi:type="string">SR_MagentoCommunity/checkout/summary/item/price/row_incl_tax</item>
                                                                            </item>
                                                                        </item>
                                                                        <item name="weee_row_excl_tax" xsi:type="array">
                                                                            <item name="config" xsi:type="array">
                                                                                <item name="template" xsi:type="string">SR_MagentoCommunity/checkout/summary/item/price/row_excl_tax</item>
                                                                            </item>
                                                                        </item>
                                                                    </item>
                                                                </item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

Now copy vendor/magento/module-weee/view/frontend/web/template/checkout/summary/item/price two template and added your logic here.

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.