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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rszrama’s picture

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?

zorroposada’s picture

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?

davidwhthomas’s picture

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.

davidwhthomas’s picture

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.

davidwhthomas’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
705 bytes

This adjustment fixed the issue for me:

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

rupertj’s picture

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.

rszrama’s picture

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.

Neo13’s picture

Issue summary: View changes

This again became an issue in latest dev.

narquam’s picture

Also seeing this issue on the latest release.

narquam’s picture

If anyone is having the Notice: Undefined index: attributes in commerce_cart_add_to_cart_form() issue again, it seems like the solution is adding an isset back to line 2072 in commerce_cart.module. This is what I did and it resolves the issue again.

if (isset($form['attributes'][$used_field_name]['#default_value']) && array_key_exists('#default_value', $form['attributes'][$used_field_name])) {

vensires’s picture

It is fixed in the dev version though.

erier’s picture

#11 fixed it for me. Thanks narquam