Page 1 of 2

How to not edit core code

Posted: Tue Mar 09, 2021 9:59 am
by Portman
Hi,

I am still trying to get my head around how to customize my site with out editing the core code. most of it I am getting a handle on, thanks to all your help, but this one has got me stumped

I need to customize the following file;

includes/system/segments/checkout/insert_order.php

What do I do with this file to follow the modification rules?

Re: How to not edit core code

Posted: Tue Mar 09, 2021 10:03 am
by ecartz
In what way do you want to customize that file? I.e. if you were modifying the file directly, what would you be doing?

Re: How to not edit core code

Posted: Tue Mar 09, 2021 11:29 am
by burt
Do always remember that the "rule" about no core code changes is only there to aid shopowners when they come to upgrade. IE if you change nothing core, the upgrade is always easy.

You can do anything you wish to any file, there is no set in stone rule..

Re: How to not edit core code

Posted: Tue Mar 09, 2021 4:52 pm
by zipurman
Portman wrote: Tue Mar 09, 2021 9:59 am I need to customize the following file;

includes/system/segments/checkout/insert_order.php
There is a hook target in that file "siteWide insertOrder".

You can write a hook targeting that and then do most changes required.

Re: How to not edit core code

Posted: Tue Mar 16, 2021 6:38 am
by Portman
burt wrote: Tue Mar 09, 2021 11:29 am Do always remember that the "rule" about no core code changes is only there to aid shopowners when they come to upgrade. IE if you change nothing core, the upgrade is always easy.

You can do anything you wish to any file, there is no set in stone rule..
Thanks Burt,

I am just trying to follow the rule as it makes sense and I am always trying to learn new things on this platform

Re: How to not edit core code

Posted: Tue Mar 16, 2021 6:42 am
by Portman
ecartz wrote: Tue Mar 09, 2021 10:03 am In what way do you want to customize that file? I.e. if you were modifying the file directly, what would you be doing?
HI ezcart,

I am adding an extra inverse currency field to the orders in the database...it is really convoluted to explain but in brief I am trying to make it so that there are static prices for different groups that are based on currencies (based on the wholesale pro addon) - so the currency value for each currency is set to 1 but I want the inverse currency conversion so that I can convert the sales into Australian dollars for reports etc in admin

Re: How to not edit core code

Posted: Tue Mar 16, 2021 6:44 am
by Portman
zipurman wrote: Tue Mar 09, 2021 4:52 pm
There is a hook target in that file "siteWide insertOrder".

You can write a hook targeting that and then do most changes required.
Thanks Zipurman, can you dumb that down a bit for me I really am at a very low level of understanding at this point.

Re: How to not edit core code

Posted: Tue Mar 16, 2021 7:18 am
by ecartz
It is relatively straightforward for me to convert instructions like add a line of code that says

Code: Select all

  'inverse_currency_code' => portman_calculate_inverse_currency_code($order),
to the orders array in the file to instead say

Code: Select all

class hook_shop_siteWide_inverse_currency_code {

  public function listen_insertOrder($parameters) {
    $parameters['sql_data']['orders']['inverse_currency_code'] = portman_calculate_inverse_currency_code($GLOBALS['order']);
  }

}
So if you know what you would do if you modified core, the easiest way to ask how to do the same thing without modifying core is to show what core modifications you would make. Because as is, after your first post, I would have had to speculate as to what you wanted to do. Note that Preston went ahead and did that by pointing out the hook point to modify what is inserted in the database. That was my first guess as to what you wanted to do as well, but it's not the only possibility.

After your second post, I know more about what you are trying to do, but I don't really know how you would go about doing it. I know (or at least I think I understood) that you want to add a value to a new column in the database. But I don't know what the column name is nor how you determine the value. So when writing up my example, I had to make up those things. That's purely wasted work if you already know how you would do it with a core modification. Because now you have to translate my example to what you are actually doing. If you tell me what your code modification would have been, then it's less work for both of us. You don't have to translate from another example and I don't have to make up the "from" code.

If it's hard to explain, you can skip that part. Just write it as a core modification and verify that it does what you want. Show us that core modification. Then we can often tell you how to do it without the core modification. But without that first code block, it is hard to explain in general how to do it. The answer really depends on what you want to do. Inserting an extra column in a table is rather simple with that file. Adding a new table can be more complicated. Sometimes it might be best to do it in a different place altogether.

Re: How to not edit core code

Posted: Mon Apr 05, 2021 3:55 am
by Portman
Hi ezcartz...

sorry about the delay in response to your post, life got in the way.

Being very new to this I just assumed that if I did a modification to this file that I would just save the modified file somewhere else in the override folder or something, that is why my question was vague as I did not think changes to code were needed to be shown if that was what the answer was.

All I have done is added the following line

Code: Select all

  $sql_data['orders'] = [
    'customers_id' => $_SESSION['customer_id'],
    'customers_name' => $GLOBALS['customer_data']->get('name', $order->customer),
    'customers_company' => $GLOBALS['customer_data']->get('company', $order->customer),

...

    'currency' => $order->info['currency'],
    'currency_value' => $order->info['currency_value'],
    'inv_currency' => $order->info['inv_currency'],//added by portman to create second exchange rate
  ];
Am I looking at creating a hook to do this without editing the core code? if that is the case can you show me how I do that as it will be the first time that I do it.

Re: How to not edit core code

Posted: Mon Apr 05, 2021 4:07 am
by ecartz

Code: Select all

class hook_shop_siteWide_inv_currency {

  public function listen_insertOrder($parameters) {
    $parameters['sql_data']['orders']['inv_currency'] = $GLOBALS['order']->info['inv_currency'];
  }

}