warning: key() [function.key]: Passed variable is not an array or object in /Users/williamlinn/Sites/linnsoft/linnsoft/sites/default/modules/uc_dependent_attributes/uc_dependent_attributes.module on line 134.
warning: Invalid argument supplied for foreach() in /Users/williamlinn/Sites/linnsoft/linnsoft/sites/default/modules/uc_dependent_attributes/uc_dependent_attributes.module on line 142.
warning: reset() [function.reset]: Passed variable is not an array or object in /Users/williamlinn/Sites/linnsoft/linnsoft/sites/default/modules/uc_dependent_attributes/uc_dependent_attributes.module on line 153.

I'm getting this multiple times for the function.key and function.reset in lines 134, 142, 153, 154.

It doesn't update whenever I have multiple attributes configured on a given product. It seems to work if I have only one attribute class with several options, but the minute I add another attribute class, then the updates to the page given by this module effectively update the FIRST attribute class correctly always, but any others below it remain stuck in their previous state.

I noticed that when this happens if I click on one of the options in the first attribute class, that the one directly below it then updates. Seems like a looping bug, hopefully an easy fix cause I would love to use this module.

FYI I'm wanting to use this on product classes with preset attribute classes, but I also tested this with a normal UC Product and imported attributes, the same thing is happening. I'm using 4 options in the first attribute class and 3 options in 2 other attribute classes.

-wil

Comments

TheDarkFlop’s picture

Getting a similar error when I am on the edit attributes page for the product. ...../node/19/edit/attributes

warning: Invalid argument supplied for foreach() in /nfs/c01/h09/mnt/32879/domains/new.prakprint.com/html/sites/all/modules/ubercart/uc_dependent_attributes/uc_dependent_attributes.module on line 187.

Anonymous’s picture

Priority: Critical » Normal

I have the same error. Anyone have ideas how to fix this?

# warning: Invalid argument supplied for foreach() in C:\wamp\www\Drupal\sites\all\modules\uc_dependent_attributes\uc_dependent_attributes.module on line 186.

This is a very irritating error. Any help at all would be greatly appreciated.

vitis’s picture

subscribing

dmurray’s picture

I'm also receiving this error

# warning: Invalid argument supplied for foreach() in /var/www/html/sites/all/modules/ubercart/uc_dependent_attributes/uc_dependent_attributes.module on line 186.
# warning: Invalid argument supplied for foreach() in /var/www/html/sites/all/modules/ubercart/uc_dependent_attributes/uc_dependent_attributes.module on line 186.
# warning: Invalid argument supplied for foreach() in /var/www/html/sites/all/modules/ubercart/uc_dependent_attributes/uc_dependent_attributes.module on line 186
SchwebDesign’s picture

I'm getting the same errors as both blasthaus and dmurray. I suspect it is because i had installed the UC Option Images module which i suspect is somehow not compatible with the uc dependent attributes module. But i'm not sure.... Any thoughts?

I disabled and uninstalled the Option Images module and the problem seemed to have gone away temporarily, but seems to have returned upon installing the devel module and looking at the dev render info for a given product.

wjaspers’s picture

Priority: Normal » Critical

Even stranger; if I try to go from any of the "UC Product tabs" to the added "Options" list, I get redirected to a different product.

I found a "workaround for it":
"REMOVE" your attributes from the product afflicted.
Add the attributes you want back.
Re-configure your attribute options, and save the product.
This will clean up the glitch.

It appears to be only triggered by leaving a "Default" option which you may have turned OFF after configuring your options list.

Anonymous’s picture

With over 640 select boxes this is not an option for me. I am tempted to manually edit the table but I am fearful of breaking something.

As it seems to still work I left it as is. Not ideal however.

FranCarstens’s picture

Priority: Normal » Critical

I'm having the same issue.

warning: Invalid argument supplied for foreach() in uc_dependent_attributes.module on line 186.

I've tried to remove and re-add (new word) the attributes. This did not work.
I've tried to completely replace them with a new set. This did not work.

Right now, I'm stumped and looking at a list of 720 "warning: Invalid argument supplied for foreach()..."

I'm not using Option Images or Devel.

Stan.Ezersky’s picture

I have the same error

warning: Invalid argument supplied for foreach() in /var/www/shop/sites/all/modules/ubercart/uc_dependent_attributes/uc_dependent_attributes.module on line 186.

wjaspers’s picture

Status: Active » Needs review
StatusFileSize
new988 bytes

In peering through the code, I can see where this problem is stated to occur.
It looks as if the author's function array_hierarchy can't tell when an array value contains an array or not.
Here's a potential patch for the issue ... I haven't had time to test it, but it should alleviate arrays blowing up in your site(s).

Since PHP4 is officially dead, I added typecasts to some of the author's built-in functions regarding arrays. Typecasting to an array will ensure that whatever value is sent to a "foreach" is valid to loop on. If the value isn't useful, it will simply turn into array( 'value' );
Additional comments are welcome.

OH crap! Winmerge ran the patch backwards. Don't use this one.

wjaspers’s picture

Try this one instead. Sorry about the confusion.

evronique’s picture

This patch doesn't work for me I have always:

* warning: Invalid argument supplied for foreach() in [...]/modules/uc_dependent_attributes/uc_dependent_attributes.module on line 142.
* warning: reset() [function.reset]: Passed variable is not an array or object in [...]/modules/uc_dependent_attributes/uc_dependent_attributes.module on line 153.
* warning: key() [function.key]: Passed variable is not an array or object in [...]/modules/uc_dependent_attributes/uc_dependent_attributes.module on line 154.

Any idea?

wjaspers’s picture

Status: Needs review » Needs work

Well ... back to the drawing board.
I'll take another look at it this weekend, if I have time.

summit’s picture

Subscribe, having the same problem. Hopefully you can get the solution soon!
Thanks a lot in advance for solving this!

greetings, Martijn

zincdesign’s picture

Subscribing

RotHorseKid’s picture

Subscribing

FranCarstens’s picture

This issue persists, subscribing.

SchwebDesign’s picture

subscribing

stevesmename’s picture

StatusFileSize
new607 bytes

Attaching patch that I used for a quick fix. Was receiving the same error as in comment #4; this error doesn't do any harm the code just seemed not to properly handle empty arrays.

jantoine’s picture

Title: key()warning: Invalid argument supplied for foreach() » Remnant database records cause 'Invalid argument supplied for foreach()' warnings
Version: 6.x-1.0 » 6.x-1.x-dev
Status: Needs work » Needs review
StatusFileSize
new2.74 KB

These errors are due to remnant database records not being deleted when product attributes and options are not updated. I have attached a patch that properly hooks into the product update process and cleans up the database. To clean up any existing products, simply save the products 'options' page.

stevesmename’s picture

Title: Remnant database records cause 'Invalid argument supplied for foreach()' warnings » Checkboxes and/or Text field attributes cause 'Invalid argument supplied for foreach()' warnings
Version: 6.x-1.x-dev » 6.x-1.0

I tested the patches above, even my own (#19) and none work. The theories also seem inaccurate.

The code below generates the error, lines 47-66. The $possible array is never generated for attributes that are either checboxes or text fields; if the product doesn't have other attributes that are either select box or radio options, then the array is never built and the error messages are displayed.

  // Filter attribute/option conbinations on the product add_to_cart form if
  // submitted via ajaxSubmit
  if (strstr($form_id, 'uc_product_add_to_cart_form_') && !empty($_POST)) {
    // Only attempt to adjust attributes if they exist
    if (isset($form['attributes'])) {
      // Build a 2-dimensional array of possible attribute and option ids
      foreach (element_children($form['attributes']) as $aid) {
        // Only Select and Radio attributes effect adjustments
        if ($form['attributes'][$aid]['#type'] == 'select' || $form['attributes'][$aid]['#type'] == 'radios') {
          foreach($form['attributes'][$aid]['#options'] as $oid => $option) {
            // Ignore additional required option
            if (!empty($oid)) {
              $possible[$aid][$oid] = array();
            }
          }
        }
      }
  
      // Build a hierarchy array of all possible attribute option combinations
      $possible = array_hierarchy($possible);

The below code attempts to check if $possible is not set so that errors are not thrown. Replace your existing function with below (note this relates to 6.x-1.0).

/**
 * Implementation of hook_form_alter().
 */
function uc_dependent_attributes_form_alter(&$form, $form_state, $form_id) {
  // Add an enabled checkbox to the product adjustments form
  if ($form_id == 'uc_product_adjustments_form') {
    // Retrieve disabled combinations from the database
    $result = db_query("SELECT combination FROM {uc_dependent_attributes} WHERE nid = %d", $form['nid']['#value']);
    $combos = array();
    while ($combo = db_result($result)) {
      $combos[] = $combo;
    }

    // Update the table header
    $form['table']['head']['#value'] = '<th>Enabled</th>'. $form['table']['head']['#value'];

    // Loop through each form element adding an enabled checkbox
    foreach (element_children($form['table']['body']) as $key) {
      $form['table']['body'][$key]['enabled'] = array(
        '#type' => 'checkbox',
        '#default_value' => !in_array($form['table']['body'][$key]['combo_array']['#value'], $combos),
        '#prefix' => '<td>',
        '#suffix' => '</td>',
        '#weight' => -1,
      );
    }

    // Add an additional form submit handler
    $form['#submit'][] = 'uc_dependent_attributes_uc_product_adjustments_form_submit';
  }
  // Filter attribute/option conbinations on the product add_to_cart form if
  // submitted via ajaxSubmit
  if (strstr($form_id, 'uc_product_add_to_cart_form_') && !empty($_POST)) {
    // Only attempt to adjust attributes if they exist
    if (isset($form['attributes'])) {
      // Build a 2-dimensional array of possible attribute and option ids
      foreach (element_children($form['attributes']) as $aid) {
        // Only Select and Radio attributes effect adjustments
        if ($form['attributes'][$aid]['#type'] == 'select' || $form['attributes'][$aid]['#type'] == 'radios') {
          foreach($form['attributes'][$aid]['#options'] as $oid => $option) {
            // Ignore additional required option
            if (!empty($oid)) {
              $possible[$aid][$oid] = array();
            }
          }
        }
      }
      
      if(!is_array($possible)) {
        $possible = array();
      }
    
      // Build a hierarchy array of all possible attribute option combinations
      $possible = array_hierarchy($possible);

  
      $disabled = array();
      // Retrieve all disabled attribute option combinations
      $result = db_query("SELECT combination FROM {uc_dependent_attributes} WHERE nid = %d AND enabled = 0", $form['nid']['#value']);
      while ($data = db_result($result)) {
        $combo = array();
        $combo_ordered = array();
  
        // Build a 2-dimensional array of disabled attribute and option ids
        foreach (unserialize($data) as $key => $value) {
          $combo[$key][$value] = array();
        }
  
        // Reorder 2-dimensional array to match form
        foreach (element_children($form['attributes']) as $aid) {
          // Only Select and Radio attributes effect adjustments
          if ($form['attributes'][$aid]['#type'] == 'select' || $form['attributes'][$aid]['#type'] == 'radios') {
            $combo_ordered[$aid] = $combo[$aid];
          }
        }
  
        // Build an array hierarchy and merge with other combos
        $disabled = array_merge_recursive_preserve($disabled, array_hierarchy($combo_ordered));
      }
      
      if(!empty($possible)) {
        // Filter out all disabled combinations from the possible combinations
        $filtered = array_diff_assoc_recursive($possible, $disabled);
        
        // Update the form attributes
        _uc_dependent_attributes_filter($filtered, $_POST['attributes'], $form['#parameters'][2]->attributes);
      }
    }
  }
}