Disable Cash On Delivery For Orders Amount Exceending

Hi, i want disable method payment Cash on Delivery for orders amount exceending 100€.

I have insert code attached into app/controllers/frontend/checkout.php before line $cart['payment_surcharge'] = 0;

$payment_methods = fn_prepare_checkout_payment_methods($cart, $auth);
foreach ($payment_methods as $k => $v) {
if ($cart['subtotal'] >= 100 && $payment_methods[$k]['payment_id'] == 14){
  unset($payment_methods[$k]);
 }
}
Tygh::$app['view']->assign('shipping_rates', $_SESSION['shipping_rates']);
Tygh::$app['view']->assign('payment_methods', $payment_methods);

Unfortunately this code doesn't works. There is any errors?

Do you receive any error, blank screen or payment method is still displayed in the list of available payment methods?

To debug the code, please add

fn_print_r($cart['subtotal'], $payment_methods[$k]['payment_id']);

after

foreach ($payment_methods as $k => $v) {

Any error or blank screen. Payment is still displayed in the list.

Any error or blank screen. Payment is still displayed in the list.

use below code in checkout_select_default_payment_method hook to fix this or this in checkout.php controller after line " fn_set_hook('checkout_select_default_payment_method', $cart, $payment_methods, $completed_steps);"

$available_payment_ids = array();
    foreach ($payment_methods as $group) {
        foreach ($group as $method) {
            $available_payment_ids[] = $method['payment_id'];
        }
    }
// Change default payment if it doesn't exists
if (!in_array($cart['payment_id'], $available_payment_ids)) {
    $cart['payment_id'] = reset($available_payment_ids);
    $cart['payment_method_data'] = fn_get_payment_method_data($cart['payment_id']);
}

Hi, thanks for reply!

Do you receive any error, blank screen or payment method is still displayed in the list of available payment methods?

To debug the code, please add

fn_print_r($cart['subtotal'], $payment_methods[$k]['payment_id']);

after

foreach ($payment_methods as $k => $v) {

I have inserted debug code and system print only subtotal (any other value)

use below code in checkout_select_default_payment_method hook to fix this or this in checkout.php controller after line " fn_set_hook('checkout_select_default_payment_method', $cart, $payment_methods, $completed_steps);"

$available_payment_ids = array();
    foreach ($payment_methods as $group) {
        foreach ($group as $method) {
            $available_payment_ids[] = $method['payment_id'];
        }
    }
// Change default payment if it doesn't exists
if (!in_array($cart['payment_id'], $available_payment_ids)) {
    $cart['payment_id'] = reset($available_payment_ids);
    $cart['payment_method_data'] = fn_get_payment_method_data($cart['payment_id']);
}

i have created hook (init.php and func.php) with code attached above and insert my code into checkout.php. The result is still the same (COD in always show).

Vivek Gupta, on 10 Nov 2016 - 10:49 PM, said:snapback.png

use below code in checkout_select_default_payment_method hook to fix this or this in checkout.php controller after line " fn_set_hook('checkout_select_default_payment_method', $cart, $payment_methods, $completed_steps);"

$available_payment_ids = array();
foreach ($payment_methods as $group) {
foreach ($group as $method) {
$available_payment_ids[] = $method['payment_id'];
}
}

// Change default payment if it doesn't exists
if (!in_array($cart['payment_id'], $available_payment_ids)) {
$cart['payment_id'] = reset($available_payment_ids);
$cart['payment_method_data'] = fn_get_payment_method_data($cart['payment_id']);
}


i have created hook (init.php and func.php) with code attached above and insert my code into checkout.php. The result is still the same (COD in always show).

use php hook in /app/addons/my_changes/init.php

fn_register_hooks(
‘prepare_checkout_payment_methods’,
‘checkout_select_default_payment_method’
);

Write code in /app/addons/my_changes/func.php

function fn_my_changes_prepare_checkout_payment_methods(&$cart, &$auth, &$payment_groups)
{
    foreach ($payment_groups as $g_key => $group) {
        foreach ($group as $p_key => $payment) {
            if ($payment['template'] == 'views/orders/components/payments/cod.tpl' && $cart['total'] > 100) {
                unset($payment_groups[$g_key][$p_key]);
            }
        }
        if (empty($payment_groups[$g_key])) {
            unset($payment_groups[$g_key]);
        }
    }
}
function fn_my_changes_checkout_select_default_payment_method(&$cart, &$payment_methods, &$completed_steps)
{
        $available_payment_ids = array();
        foreach ($payment_methods as $group) {
            foreach ($group as $method) {
                $available_payment_ids[] = $method['payment_id'];
            }
        }
    // Change default payment if it doesn't exists
    if (!in_array($cart['payment_id'], $available_payment_ids)) {
        $cart['payment_id'] = reset($available_payment_ids);
        $cart['payment_method_data'] = fn_get_payment_method_data($cart['payment_id']);
    }

}

Put this .. surely it will work . :)

I have inserted debug code and system print only subtotal (any other value)

In this case add the following code to see the content of the payment_methods array. Possibly it has more levels

fn_print_r($payment_methods);

use php hook in /app/addons/my_changes/init.php

fn_register_hooks(
‘prepare_checkout_payment_methods’,
‘checkout_select_default_payment_method’
);

Write code in /app/addons/my_changes/func.php

function fn_my_changes_prepare_checkout_payment_methods(&$cart, &$auth, &$payment_groups)
{
    foreach ($payment_groups as $g_key => $group) {
        foreach ($group as $p_key => $payment) {
            if ($payment['template'] == 'views/orders/components/payments/cod.tpl' && $cart['total'] > 100) {
                unset($payment_groups[$g_key][$p_key]);
            }
        }
        if (empty($payment_groups[$g_key])) {
            unset($payment_groups[$g_key]);
        }
    }
}
function fn_my_changes_checkout_select_default_payment_method(&$cart, &$payment_methods, &$completed_steps)
{
        $available_payment_ids = array();
        foreach ($payment_methods as $group) {
            foreach ($group as $method) {
                $available_payment_ids[] = $method['payment_id'];
            }
        }
    // Change default payment if it doesn't exists
    if (!in_array($cart['payment_id'], $available_payment_ids)) {
        $cart['payment_id'] = reset($available_payment_ids);
        $cart['payment_method_data'] = fn_get_payment_method_data($cart['payment_id']);
    }

}

Put this .. surely it will work . :)

Thanks! Works perfectly!

Hello Vivek,

Can you please tell me where this code exactly use.

I checked the files i see the code but did not under stand where the limit will restrict from admin panel.

please guide me if want i will gave you the access.