Post by bigint » Wed Apr 12, 2017 5:03 am

Hi,

I am running Opencart 2.3.0.2 with the default theme. I would like to add the text 'From' before the price, so I wrote the little OCMod below:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<modification>
	<name>Add From Price</name>
	<code>add-from-price</code>
	<version>1.0</version>
	<author>me</author>
	<link>http://ocuk.co.uk/</link>
	<file path="catalog/view/theme/*/template/product/product.tpl">
		<operation>
			<search><![CDATA[
            <li>
             <h2><?php echo $price; ?></h2>
            </li>
			]]></search>
			<add position="replace"><![CDATA[             
            <li>
             <h2>From <?php echo $price; ?></h2>
            </li>                     
            ]]></add>
		</operation>
	</file>
</modification>
However, no text is added before the price.

I cannot understand this because the same OCMod works fine if I add text before the <h1> product heading title tag or the <li> tag containing stock availability and these are both in the same product.tpl - so the mod is working just not for the price.

If I manually, add the text 'From' in the actual template code on line 141, like so:

Code: Select all

<h2>From <?php echo $price; ?></h2>
The text 'From' appears fine before the price.

What am I missing please? Any help would be much appreciated.
Last edited by OpenCart Addons on Wed Apr 12, 2017 12:30 pm, edited 1 time in total.

Newbie

Posts

Joined
Wed Mar 08, 2017 9:24 pm

Post by ashwani_multi » Wed Apr 12, 2017 1:03 pm

please try this code

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<modification>
	<name>Add From Price</name>
	<code>add-from-price</code>
	<version>1.0</version>
	<author>me</author>
	<link>http://ocuk.co.uk/</link>
	<file path="catalog/view/theme/*/template/product/product.tpl">
		<operation>
			<search><![CDATA[<?php echo $price; ?>]]></search>
			<add position="before"><![CDATA[From ]]></add>
		</operation>
	</file>
</modification>

Opencart Quick checkout
View All modules
Any opencart issue contact me at support@opencartextensions.in

Thank you


User avatar
New member

Posts

Joined
Tue Dec 09, 2014 2:40 pm
Location - Ludhiana,Punjab,India

Post by bigint_b » Wed May 03, 2017 12:49 am

Thank you ashwani_multi.

I created the original post. I tried to post back immediately, but my new OpenCart forum account would not let me reply to posts. Therefore, I raised a ticket and waited for support to get back to me. They said this is a known issue and suggested I opened a new account. Here I am.

Regarding your kind help, the code you provided adds the text before the price on the line above, not in front of the price on the same line, I presume because the price is wrapped in a <h2> tag and you used (add position="before").

Therefore, I wrapped the additional text in <h2> tag and reduced the ‘margin-bottom’ to bring the two closer together.

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<modification>
	<name>Add From Price</name>
	<code>add-from-price</code>
	<version>1.0</version>
	<author>me</author>
	<link>http://ocuk.co.uk/</link>
		<file path="catalog/view/theme/*/template/product/product.tpl">
		<operation>
			<search><![CDATA[<?php echo $price; ?>]]></search>
			<add position="before"><![CDATA[<h2 style="margin-bottom: -10px;">From</h2>]]></add>
		</operation>
	</file>
</modification>
This looked better, but it was not what I wanted.

I looked at your code again and I noted that you had just used <?php echo $price; ?> in <search>, without the header tags.

So, I tried again using add position="replace", without the header tags. This time the text was added just where I wanted it. Below is the code to modify the price with the additional text ‘From’ on the same line as the price.

I needed your help to get there. Thanks again; I am grateful.

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<modification>
	<name>Add From Price</name>
	<code>add-from-price</code>
	<version>1.0</version>
	<author>me</author>
	<link>http://ocuk.co.uk/</link>
	<file path="catalog/view/theme/*/template/product/product.tpl">
		<operation>
            <search><![CDATA[
            <?php echo $price; ?>
            ]]></search>
			<add position="replace"><![CDATA[From <?php echo $price; ?>]]></add>
		</operation>
	</file>
</modification>
Update:

My client then asked for the price with tax and without tax to be swapped positions.

The best method I found for achieving this was to add another product.tpl file in system\storage\modification\catalog\view\theme\default\template\product\product.tpl, which causes the file to overwrite the original like the Wordpress child theme system.

Then, I swapped the price with tax and without tax around. But this stopped the above ocmod to add the text ‘From’, from working - I presume because the duplicate product.tpl is called after the ocmod.

So, to get the words ‘from’ and ‘incl tax’ and ‘excl tax’ in the correct positions I added the 'from' 'incl' and 'excl' text and style directly onto the duplicate template (see below) and disabled the ocmod to add 'from' in front of price.

The below final code swapped the prices for with and without tax around and added the text ‘From’ in front of the top price. The code swapped n the duplicate of product.tpl was on line 137 down (OpenCart 2.3.0.2).

Code: Select all

 <?php if ($price) { ?>
          <ul class="list-unstyled">
            <?php } ?>
            <?php if ($tax) { ?>
            <li><h2>From: <?php echo $tax; ?><span style="font-weight: 500; font-size: 14px;">&nbsp; (excl. tax)</span></h2></li>

            <?php if (!$special) { ?>
            <li>
              <h4><?php echo $price; ?> (incl. tax)</h4>
            </li>
            <?php } else { ?>
            <li><span style="text-decoration: line-through;"><?php echo $price; ?></span></li>
            <li>
              <h2><?php echo $special; ?></h2>
            </li>
          
            <?php } ?>
            <?php if ($points) { ?>
            <li><?php echo $text_points; ?> <?php echo $points; ?></li>
            <?php } ?>
            <?php if ($discounts) { ?>
            <li>
              <hr>
            </li>
            <?php foreach ($discounts as $discount) { ?>
            <li><?php echo $discount['quantity']; ?><?php echo $text_discount; ?><?php echo $discount['price']; ?></li>
            <?php } ?>
            <?php } ?>
          </ul>
I hope this helps somebody else.

Newbie

Posts

Joined
Thu Apr 13, 2017 2:12 pm

Post by bigint_b » Wed May 03, 2017 3:11 pm

Update:

If you swap the prices with and without tax around, you may want to do the same for related products and the category page products.

For related products look for line 352 in your duplicate product.tpl and just swap the related products price code around. E.G.

Code: Select all

 <p class="price">
                <?php if ($product['tax']) { ?>
                <span class="price-tax" style="color: #444;"><?php echo $product['tax']; ?> &nbsp;Ex. Tax <?php echo $text_tax; ?> </span>
                <?php } ?>
                <?php if (!$product['special']) { ?>
                <span style="color: #999;"><?php echo $product['price']; ?>&nbsp; Inc. Tax</span> 
                <?php } else { ?>
                <span class="price-new"><?php echo $product['special']; ?></span> <span class="price-old"><?php echo $product['price']; ?> </span>
                <?php } ?>
              </p>
For the category page, copy category.tpl from \catalog\view\theme\default\template\product\category.tpl and add it to \system\storage\modification\catalog\view\theme\default\template\product\category.tpl, making another child theme style duplicate. We make a duplicate so the changes act like mod and don't hurt the original file.

Look for lines 107 - 116 category and use the same code above to replace it.

I commented out <?php echo $text_tax; ?> which adds the 'Ex. Tax:' text, and added my own text to remove the semicolon.

Newbie

Posts

Joined
Thu Apr 13, 2017 2:12 pm
Who is online

Users browsing this forum: No registered users and 4 guests