My first addon, please help

I am creating Payment methods based on locations addon and need some help



Working with function fn_get_payment_methods located in core\fn.cart.php



function fn_get_payment_methods(&$auth, $lang_code = CART_LANGUAGE)
{
$condition = '';
if (AREA == 'C') {
$condition .= " AND (" . fn_find_array_in_set($auth['usergroup_ids'], '?:payments.usergroup_ids', true) . ")";
$condition .= " AND ?:payments.status = 'A' ";
$condition .= fn_get_localizations_condition('?:payments.localization');
}

$payment_methods = db_get_hash_array("SELECT ?:payments.payment_id, ?:payments.a_surcharge, ?:payments.p_surcharge, ?:payments.payment_category, ?:payment_descriptions.*, ?:payment_processors.processor, ?:payment_processors.type AS processor_type FROM ?:payments LEFT JOIN ?:payment_descriptions ON ?:payments.payment_id = ?:payment_descriptions.payment_id AND ?:payment_descriptions.lang_code = ?s LEFT JOIN ?:payment_processors ON ?:payment_processors.processor_id = ?:payments.processor_id WHERE 1 $condition ORDER BY ?:payments.position", 'payment_id', $lang_code);

fn_set_hook('get_payment_methods', $payment_methods, $auth);

return $payment_methods;
}




basically I only need to modify select query


$payment_methods = db_get_hash_array("SELECT ?:payments.payment_id...



so I have used get_payment_methods hook, and created new function “fn_location_based_payments_get_payment_methods” in my \addons\location_based_payments\func.php



I have just copied whole function content and modified select query to my needs. My func.php actually works, but I feel this is not most elegant way to execute db_get_hash_array twice. I am also aware about possible conflicts with other addons which use this hook


function fn_location_based_payments_get_payment_methods ($payment_methods, $auth)
{
$lang_code = CART_LANGUAGE;
$condition = '';
if (AREA == 'C') {
$condition .= " AND (" . fn_find_array_in_set($auth['usergroup_ids'], '?:payments.usergroup_ids', true) . ")";
$condition .= " AND ?:payments.status = 'A' ";
$condition .= fn_get_localizations_condition('?:payments.localization');
}

$payment_methods = db_get_hash_array("SELECT ?:payments.payment_id, ?:payments.a_surcharge, ?:payments.p_surcharge, ?:payments.payment_category, ?:payment_descriptions.*, ?:payment_processors.processor, ?:payment_processors.type AS processor_type FROM ?:payments LEFT JOIN ?:payment_descriptions ON ?:payments.payment_id = ?:payment_descriptions.payment_id AND ?:payment_descriptions.lang_code = ?s LEFT JOIN ?:payment_processors ON ?:payment_processors.processor_id = ?:payments.processor_id RIGHT JOIN ?:payment_destinations_addon ON ?:payments.payment_id = ?:payment_destinations_addon.payment_id WHERE 1 $condition ORDER BY ?:payments.position", 'payment_id', $lang_code);
}




Is there any way to modify this select statement without execute db_get_hash_array twice?







Also I need to add some code ( db insert statement) to function fn_update_payment located in \controllers\admin\payments.php

I know I can add some code to payments.php by creating payments.post.php file in addon, but i need this code in specific function.

As I know its not possible to declare the same function twice.

Short answer is no. However, you might be able to just loop through the array of payment methods and unset those you don't want to keep.

Thanks tbirnseth,

[quote name='tbirnseth' timestamp='1373255585' post='164981']

Short answer is no. However, you might be able to just loop through the array of payment methods and unset those you don't want to keep.

[/quote]



good point, I will do it in this way



and what about my second question? Is there any safe way to inject additional code to fn_update_payment function located in \controllers\admin\payments.php controller? I need to insert new records to my table when payment is modified\inserted in admin area.





Also it is generally safe to modify core database tables? I am talking about adding additional column.

adding fields to core tables is safe.



this function fn_update_payment is located in fn.cart.php. The controller is calling it. In most cased using payments.post.php or payments.pre.php will help