Search code examples
phpcs-cart

CS-Cart: Invalid Argument Supplied For Foreach() In App\controllers\backend\index.php


In the CS-Cart admin on the front page only I'm getting the error:

Warning: Invalid argument supplied for foreach() in app\controllers\backend\index.php on line 255

The code for this function is:

function fn_get_orders_taxes_subtotal($orders, $params)
{
    $subtotal = 0;

    if (!empty($orders)) {
        $_oids = array();
        foreach ($orders as $order) {
            if (in_array($order['status'], $params['paid_statuses'])) {
                $oids[] = $order['order_id'];
            }
        }

        if (empty($oids)) {
            return $subtotal;
        }

        $taxes = db_get_fields('SELECT data FROM ?:order_data WHERE order_id IN (?a) AND type = ?s', $oids, 'T');

        if (!empty($taxes)) {
            foreach ($taxes as $tax) {
                $tax = unserialize($tax);
                foreach ($tax as $id => $tax_data) {
                    $subtotal += !empty($tax_data['tax_subtotal']) ? $tax_data['tax_subtotal'] : 0;
                }
            }
        }
    }

    return $subtotal;
}

and the specific line in question is:

foreach ($tax as $id => $tax_data) {

Any ideas what may be going on here? Strangely, this doesn't show if template debugging is turned on.


Solution

  • check out by print your two foreach variable in that function is array, I mean your $taxes got rows and $tax got proper serialize data , you can ignore that error by embed an if condition with is_array(), here is a demonstration ..

    if (is_array($taxes)) {
        foreach ($taxes as $tax) {
            $tax = unserialize($tax);
           if(is_array($tax)){    
             foreach ($tax as $id => $tax_data) {
                $subtotal += !empty($tax_data['tax_subtotal']) ? $tax_data['tax_subtotal'] : 0;
             }
           }
        }
    }