Post by cartuser9 » Wed Jun 02, 2021 2:18 pm

I'm playing with some code to see if I understand the ins and outs of OCMOD, and came to an unexpected result.

Does ADD AFTER inserts the text right after the match, or in the next line?

Here are the details of my test:

order_list.twig has a line:

Code: Select all

<td class="text-right">{{ order.total }}</td>
My OCMOD xml says:

Code: Select all

    <file path="admin/view/template/sale/order_list.twig">
    <operation>
      <search><![CDATA[<td class="text-right">{{ order.total }}]]></search>
      <add position="after"><![CDATA[ {{ order.currency_code }}  ]]></add>
    </operation>
  </file> 
The goal is to show the currency code after the order total, but before the '</td>' closes the table cell.

But after applying the modification, the cache shows:

Code: Select all

                // line 132
                echo twig_get_attribute($this->env, $this->source, $context["order"], "total", [], "any", false, false, false, 132);
                echo "</td>";
                // line 133
                echo twig_get_attribute($this->env, $this->source, $context["order"], "currency_code", [], "any", false, false, false, 133);
So the 2nd variable was inserted as a new line, after the end of the line that contained the match, after the '</td>' even when the search string didn't contain '</td>'.

Is this how it is supposed to work, or am I doing something wrong?

Thanks

Newbie

Posts

Joined
Mon May 17, 2021 2:56 am

Post by DigitCart » Wed Jun 02, 2021 2:30 pm

Hi

Try this:

Code: Select all

<search><![CDATA[{{ order.total }}</td>]]></search>
<add position="replace"><![CDATA[{{ order.total }} {{ order.currency_code }}</td>]]></add>

My Extensions


User avatar
Active Member

Posts

Joined
Thu Jun 22, 2017 5:32 pm


Post by OSWorX » Wed Jun 02, 2021 2:43 pm

Basically your code looks okay, maybe read this here: https://github.com/opencart/opencart/wi ... ion-System
If you use
<add position="after">
it is to supposed to add the line/code) directly after the searched item/code.
You can use also a new line in the section to add it in the next line,
Using trim (see Wiki) allows more.

But before you go ahead, forget all about OCMod and VQMod and similiar!
Better to work with Events, see: https://github.com/opencart/opencart/wiki/Events-System
and forum posting here:
viewtopic.php?f=144&t=221533

Why not OCMod anymore?
Events were introduced with OpenCart 2.3.x, extended with OpenCart 3.x and will be used only in OpenCart 4.x
Everything which is (and was) available in the past with VQ- & OCMods are also possible with Events.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by paulfeakins » Wed Jun 02, 2021 6:51 pm

OSWorX wrote:
Wed Jun 02, 2021 2:43 pm
Everything which is (and was) available in the past with VQ- & OCMods are also possible with Events.
Total rubbish, what on Earth made you think that?

You can add events before and after methods but can you modify, remove, or add to code inside a method?

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by thekrotek » Wed Jun 02, 2021 7:29 pm

OC event system is super weak even in its latest state comparing to WP hooks and actions. Removing OCMod is quite bad idea, we'll just revert back to VQMod or somebody will create an OCMOd extension for OC 4.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by OSWorX » Wed Jun 02, 2021 8:24 pm

paulfeakins wrote:
Wed Jun 02, 2021 6:51 pm
OSWorX wrote:
Wed Jun 02, 2021 2:43 pm
Everything which is (and was) available in the past with VQ- & OCMods are also possible with Events.
Total rubbish, what on Earth made you think that?

You can add events before and after methods but can you modify, remove, or add to code inside a method?
Guess you have not understood how events are working?
Simply override the method.
Practising this in a few extensions of me and works perfectly.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by thekrotek » Wed Jun 02, 2021 8:52 pm

OSWorX wrote:
Wed Jun 02, 2021 8:24 pm
Guess you have not understood how events are working?
Simply override the method.
Practising this in a few extensions of me and works perfectly.
So you're saying that to change a particular function a bit, to add a couple lines of code I will have to copy the function completely and then add these couple lines of code to the copy?

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by cartuser9 » Wed Jun 02, 2021 10:05 pm

OSWorX wrote:
Wed Jun 02, 2021 2:43 pm
it is to supposed to add the line/code) directly after the searched item/code.
In my case it doesn't do that exactly. It adds the code after the end of the line that contained the searched code.

That had me confused.

Thanks

Newbie

Posts

Joined
Mon May 17, 2021 2:56 am

Post by cartuser9 » Wed Jun 02, 2021 10:09 pm

DigitCart wrote:
Wed Jun 02, 2021 2:30 pm
Try this:
This works.
Thanks!

Newbie

Posts

Joined
Mon May 17, 2021 2:56 am

Post by straightlight » Wed Jun 02, 2021 10:12 pm

cartuser9 wrote:
Wed Jun 02, 2021 10:05 pm
OSWorX wrote:
Wed Jun 02, 2021 2:43 pm
it is to supposed to add the line/code) directly after the searched item/code.
In my case it doesn't do that exactly. It adds the code after the end of the line that contained the searched code.

That had me confused.

Thanks
Which simply means you could use a /after event instead of a /before event. In addition, you could even call another modulated controller to use the rest of the code without the need to reproduce the entire function. An e.g to take a look into, as suggested many times on the forum, is the lookup into the catalog/controller/extension/analytics folder and look at the controllers that launches the events.
Last edited by straightlight on Wed Jun 02, 2021 10:14 pm, edited 1 time in total.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by JNeuhoff » Wed Jun 02, 2021 10:13 pm

it is to supposed to add the line/code) directly after the searched item/code.
Wrong. When using OCmod the code is added after the line containing the search expression.
To add code right after the search expression, use the replace as explained above.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by cartuser9 » Wed Jun 02, 2021 10:25 pm

Thanks all.

Just for completion, the full code of my first mod ever, to show the currency in the orders table, would be:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<modification>
  <name>Show currency on Orders Table</name>
  <code>orders_table_mod</code>
  <version>1.0</version>
  <author>Me</author>
  <link>http://mywebsite.com</link>
  
    <file path="admin/view/template/sale/order_list.twig">
    <operation>
		<search><![CDATA[{{ order.total }}</td>]]></search>
		<add position="replace"><![CDATA[{{ order.total }} {{ order.currency_code }}</td>]]></add>
    </operation>
  </file> 
  
    <file path="admin/controller/sale/order.php">
    <operation>
		<search><![CDATA['customer'      => $result['customer'],]]></search>
		<add position="after"><![CDATA['currency_code' => $result['currency_code'],]]></add>
    </operation>
  </file>   

</modification>

Newbie

Posts

Joined
Mon May 17, 2021 2:56 am

Post by paulfeakins » Thu Jun 03, 2021 7:50 pm

OSWorX wrote:
Wed Jun 02, 2021 8:24 pm
Simply override the method.
So 2 extensions cannot modify different lines of the same method?

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by OSWorX » Fri Jun 04, 2021 5:55 pm

JNeuhoff wrote:
Wed Jun 02, 2021 10:13 pm
it is to supposed to add the line/code) directly after the searched item/code.
Wrong. When using OCmod the code is added after the line containing the search expression.
To add code right after the search expression, use the replace as explained above.
It depends also on the written code.
Writing:
<search><![CDATA[{{ order.total }}</td>]]></search>
<add position="after"><![CDATA[ {{ order.currency_code }}]]></add>
will add it directly after.
Instead writing:
<search><![CDATA[{{ order.total }}]]></search>
<add position="after"><![CDATA[
{{ order.currency_code }}
]]></add>
will add it next line.

Additonally the trim parameter (default = false) can be used.

Using replace will replace the searched code - also here depends how the replacement is defined.

Should I be wrong, no problem in learning ..

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by OSWorX » Fri Jun 04, 2021 5:56 pm

paulfeakins wrote:
Thu Jun 03, 2021 7:50 pm
OSWorX wrote:
Wed Jun 02, 2021 8:24 pm
Simply override the method.
So 2 extensions cannot modify different lines of the same method?
Why asking me?
You are the developer ..

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by JNeuhoff » Fri Jun 04, 2021 6:09 pm

OSWorX wrote:
Fri Jun 04, 2021 5:55 pm
JNeuhoff wrote:
Wed Jun 02, 2021 10:13 pm
it is to supposed to add the line/code) directly after the searched item/code.
Wrong. When using OCmod the code is added after the line containing the search expression.
To add code right after the search expression, use the replace as explained above.
It depends also on the written code.
Writing:
<search><![CDATA[{{ order.total }}</td>]]></search>
<add position="after"><![CDATA[ {{ order.currency_code }}]]></add>
will add it directly after.
Instead writing:
<search><![CDATA[{{ order.total }}]]></search>
<add position="after"><![CDATA[
{{ order.currency_code }}
]]></add>
will add it next line.

Additonally the trim parameter (default = false) can be used.

Using replace will replace the searched code - also here depends how the replacement is defined.

Should I be wrong, no problem in learning ..
OK, I stand corrected here,

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by paulfeakins » Fri Jun 04, 2021 6:55 pm

OSWorX wrote:
Fri Jun 04, 2021 5:56 pm
Why asking me?
You are the developer ..
Well your signature says "Full Stack Web Developer" ...

But if you're not a developer why are you making untrue statements like this?:
OSWorX wrote:
Wed Jun 02, 2021 2:43 pm
Everything which is (and was) available in the past with VQ- & OCMods are also possible with Events.
The answer is NO events cannot do anything that vQmod and OCMOD can do.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom
Who is online

Users browsing this forum: No registered users and 22 guests