Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

Disable Cash On Delivery For Orders Amount Exceending Rate Topic   - - - - -

 
  • vin86
  • Member
  • Members
  • Join Date: 16-Nov 09
  • 42 posts

Posted 09 November 2016 - 11:37 AM #1

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?



 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 22363 posts

Posted 09 November 2016 - 01:50 PM #2

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) {

GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 1210     Multi-Vendor              USD 1250    Multi-Vendor PLUS           USD 3100 (2775)
CS-Cart Ultimate  USD 4025     CS-Cart + YOUPI      USD 1459      Multi-Vendor Ultimate       USD 7500 (6000)


 
  • vin86
  • Member
  • Members
  • Join Date: 16-Nov 09
  • 42 posts

Posted 10 November 2016 - 11:09 AM #3

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



 
  • Vivek Gupta
  • Senior Member
  • Members
  • Join Date: 09-Jul 15
  • 247 posts

Posted 10 November 2016 - 05:19 PM #4

 

 

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']);
    }





Vivek Gupta 
 


 
  • vin86
  • Member
  • Members
  • Join Date: 16-Nov 09
  • 42 posts

Posted 11 November 2016 - 11:31 AM #5

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
  • Senior Member
  • Members
  • Join Date: 09-Jul 15
  • 247 posts

Posted 11 November 2016 - 12:09 PM #6

 

 

 

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 .   :)


Vivek Gupta 
 


 
  • eComLabs
  • CS-Cart Expert
  • Authorized Reseller
  • Join Date: 27-Jan 14
  • 22363 posts

Posted 11 November 2016 - 02:00 PM #7

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);

GET A FREE QUOTE | CS-Cart Add-ons | CS-Cart Licenses | CS-Cart Development | CS-Cart Design | Server Configuration | UniTheme and YOUPI
CS-Cart                USD 1210     Multi-Vendor              USD 1250    Multi-Vendor PLUS           USD 3100 (2775)
CS-Cart Ultimate  USD 4025     CS-Cart + YOUPI      USD 1459      Multi-Vendor Ultimate       USD 7500 (6000)


 
  • vin86
  • Member
  • Members
  • Join Date: 16-Nov 09
  • 42 posts

Posted 12 November 2016 - 04:25 PM #8

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!



 
  • dikazo
  • Newbie
  • Members
  • Join Date: 22-Mar 21
  • 9 posts

Posted 27 October 2021 - 05:20 AM #9

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.