Hi,
Using attributes colors and size I got this error:

Notice: Undefined index: attributes in commerce_cart_add_to_cart_form() (line 1704 of sites/all/modules/commerce/modules/cart/commerce_cart.module).

The code is the following:

if ($product_wrapper->{$used_field_name}->raw() != $form['attributes'][$used_field_name]['#default_value']) {
   continue 2;
}

I made this addon to line 1704 to remove the error, but I do not know if this is correct?

if(isset($form['#attributes'][$used_field_name]['#default_value'])){
                 if ($product_wrapper->{$used_field_name}->raw() != $form['attributes'][$used_field_name]['#default_value']) {
                     continue 2;
                 }
              }

Greetings, Martijn

Files: 
CommentFileSizeAuthor
#6 commerce-fix_undefined_index_attributes-1588790-6.patch793 bytesrupertj
PASSED: [[SimpleTest]]: [MySQL] 3,588 pass(es).
[ View ]
#5 1588790-fix-missing-attribute-notice-errors.patch705 bytesdavidwhthomas
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).
[ View ]

Comments

Version:7.x-1.3» 7.x-1.x-dev
Status:Active» Postponed (maintainer needs more info)

The question is what are the products you're referencing and what fields do they have?

Notice appears 6 times in page:

Notice: Undefined index: attributes in commerce_cart_add_to_cart_form() (line 1701 of /www/mysite/sites/all/modules/contrib/commerce/modules/cart/commerce_cart.module).
  • I have a Product Display node with two products in the "Product reference" field.
  • The Add To Cart form displays correctly.
  • The Product has three attributes but only two of them are populated for each item.

Please advice what is the best solution to avoid this Notice?

Same error here, I'm guessing the error occurs because the referenced products contain values for only a subset of all the available fields on that product type.

I also see alot of notice errors for each field that isn't present on that add to cart form, e.g

Notice: Undefined index: field_copies in commerce_cart_add_to_cart_form() (line 1708 of .../sites/all/modules/contrib/commerce/modules/cart/commerce_cart.module).

In my case, it's a digital subscription product, the product type referenced has 'field_copies' but that's not used for this product display and not populated in the referenced products of that type.
Namely because the display is for a digital subscription product, with no print copies.

The problem appears to occur because

commerce_cart.module line 1664

            // Otherwise just use the base language values.
            if (empty($allowed_values)) {
              $allowed_values = _options_get_options($field, $instance, $properties, 'commerce_product', $default_product);
            }

Returns data, even though no values of that field are valid for the current product display.

Status:Postponed (maintainer needs more info)» Needs review
StatusFileSize
new705 bytes
PASSED: [[SimpleTest]]: [MySQL] 3,567 pass(es).
[ View ]

This adjustment fixed the issue for me:

<?php
if ($product_wrapper->{$field_name}->raw() != NULL && ($product_wrapper->{$used_field_name}->raw() != $form['attributes'][$used_field_name]['#default_value'])) {
  continue
2;
}
?>

Patch attached.

cheers,

DT

StatusFileSize
new793 bytes
PASSED: [[SimpleTest]]: [MySQL] 3,588 pass(es).
[ View ]

The patch in #6 eliminated some of the warnings for me, but not all of them. My product has three taxonomy fields attached - two of which are optional. The optional ones still generated warnings when left empty.

The attached patch stops all warnings for me.

Status:Needs review» Fixed

Alrighty, Summit / rupertj's patch seems to do the trick. I had a lot of difficulty replicating this, because it only shows the warning if the first attribute to appear on the Add to Cart form is the one that had no used options. That's because it gets checked on the first pass through the foreach() there, and $form['attributes'] hasn't been set yet. Alternately, we could just go ahead and set $form['attributes'] earlier, but I'm not sure we have any reason to.

Thanks for sticking this out, guys. Committed!

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.