0

Getting notice:

Trying to access array offset on value of type bool in uc_quote_uc_order() (line 311.

This only happens when login as administrator This is the code:

/**
 * Implements hook_uc_order().
 */
function uc_quote_uc_order($op, $order, $arg2) {
  switch ($op) {
    case 'save':
      if (isset($order->quote['method'])) {
        db_merge('uc_order_quotes')
          ->key(array('order_id' => $order->order_id))
          ->fields(array(
            'method' => $order->quote['method'],
            'accessorials' => $order->quote['accessorials'],
            'rate' => $order->quote['rate'],
          ))
          ->execute();
      }
      break;

    case 'load':
      $quote = db_query("SELECT method, accessorials, rate FROM {uc_order_quotes} WHERE 
order_id = :id", array(':id' => $order->order_id))->fetchAssoc();
      $order->quote = $quote;

311>> $order->quote['accessorials'] = strval($quote['accessorials']); break;

    case 'delete':
      db_delete('uc_order_quotes')
        ->condition('order_id', $order->order_id)
        ->execute();
      break;
  }
}
stealth
  • 1
  • 1
  • 4
    Which line is line 311? – msbit Jun 22 '21 at 12:25
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Nico Haase Jun 22 '21 at 12:25
  • 1
    This likely means that `$order->quote` has been set to false. I'm guessing `$order->quote` is the result of a database query and the query returned no records. You should use getters and setters instead of accessing object properties directly. – wetmarble Jun 22 '21 at 14:25
  • Line 311 = $order->quote['accessorials'] = strval($quote['accessorials']); – stealth Jun 22 '21 at 16:25
  • Okay, as @wetmarble has pointed out, `$quote` is `false`, because, as per https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Database%21Statement.php/function/Statement%3A%3AfetchAssoc/8.2.x `fetchAssoc` returns "An associative array, or FALSE if there is no next row.". So, you have to handle that case before trying to proceed, either work out why `uc_order_quotes` has no matching rows (hint: run the query manually) or bail if `fetchAssoc` returns `false`. – msbit Jun 23 '21 at 02:32
  • Oh, and if you can't make headway on the `uc_order_quotes` issue, maybe check https://drupal.stackexchange.com – msbit Jun 23 '21 at 02:34
  • Does anyone know how to code this to resolve this issue? – stealth Jun 23 '21 at 16:04
  • 1
    What is "this"? What have you tried to resolve the problem? Have you tried to run this using a debugger that could show you what `$quote` contains? Please don't guess or explain what it contains, debug it properly – Nico Haase Jun 23 '21 at 16:06

0 Answers0