How to display price of an item as FREE instead of 0.0

In this article we will show you a very easy way to change the 0.00 price for your products to a text string. In this case we will use the word “Free”, but you can change it to whatever you like.


Get the modification for OpenCart 3.x here!


We will show two ways of implementing this - by changing the core files or by using the new OCMod functionality in OpenCart 2.0.
 

Edit the core files (Faster but not recommended)

The modification that we have to do is quite simple. What you have to do is open system/library/currency.php (system/library/cart/currency.php for OpenCart 2.2.0.0 and above) and search for this line:

$string = '';

Add this code before or after the searched line:

if ((float)$value == 0 && ($format)) {
return 'Free';
}

We check if the price is 0 (zero) and if so, instead of placing a zeros for it, we change the string to ‘Free’. You can change the word to whatever you like.

Even though it looks not very challenging to implement, I suggest you to use the OpenCart 2.0 OCMod system to apply this change. That is because it is never good to directly change the core files of any system.

Use OCMod 

Using OCMod will not only keep your core files intact, but will give you the ability to easily enable or disable the modification whenever you like. You will also have no problems upgrading your OpenCart to a newer version using this kind of modification.

What you have to do is copy the code below and save it in a file called new_name.ocmod.xml

<modification>
    <name>Show price FREE for zero amount</name>
    <version>1.0 (Initial)</version>
    <link>https://isenselabs.com</link>
    <author>iSenseLabs</author>
    <code>isenselabs_price_free</code>
   
  <file path="system/library/currency.php">
        <operation>
            <search><![CDATA[$string = '';]]></search>
            <add position="after"><![CDATA[
            if ((float)$value == 0 && ($format)) {
                return 'Free';
              }
            ]]></add>
        </operation>
    </file>
</modification>

Note: Keep in mind that the file should end with .ocmod.xml otherwise the system will not implement it.

OpenCart 2.2.x and above: If you are using OpenCart 2.2 or a newer version, you have to make a slight change in the code. What you have to do is pretty simple - check line 8 in the code and replace system/library/currency.php with system/library/cart/currency.php.

When you are ready with the file, open your store administration and navigate to Extensions -> Extension Installer. After that, simply click upload the file using the upload button and wait for the success message. You are almost ready.

The file is uploaded, but the changes are not implemented. You should navigate to Extensions -> Modifications and click on the Refresh button. The page will refresh and you should receive another success message. The modification is now implemented and ready to use.

This is the end result:

 

Found this tutorial useful? Do you want us to show you more handy modifications for your store? Let us know in the comments section below.

Join 11,000+ subscribers receiving actionable E-commerce advice

* Unsubscribe any time

Trending blogs

comments powered by Disqus