Problem/Motivation
After upgrading to Commerce version 7.x-1.14, my dblog is just FULL of this error message over and over again:
Warning: array_key_exists() expects parameter 2 to be array, null given in commerce_cart_add_to_cart_form() (line 2072 of ..... /commerce/modules/cart/commerce_cart.module).
And every time it is coupled with this one too:
Notice: Undefined index: attributes in commerce_cart_add_to_cart_form() (line 2072 of ..... /commerce/modules/cart/commerce_cart.module).
I'm running Drupal 7.56 with PHP 7.1.
However these errors, while annoying, appear to be inconsequential. My site is running fine to the end-user, orders are still being processed as normal, etc. I've tried clearing caches and running cron etc. No difference. Anyone else having this problem?
Proposed resolution
A patch or instructions on how to make it stop?
Remaining tasks
Needs expert review.
User interface changes
Pending.
API changes
Pending.
Data model changes
Pending.
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | 2924792-13.add_to_cart_optional_attributes.patch | 1.38 KB | rszrama |
Comments
Comment #2
rszrama commentedWell, bummer. This appears to be a regression introduced by #2919323: Fix dependent attribute option filtering when resolved relative to optional attributes. I suppose in your case $form['attributes'] is empty? Any way you can confirm that for me? If so, I can pair it with something like a
!empty($form['attributes'])check and / oris_array().Comment #3
TynanFox commentedWell, that makes perfect sense. I was wondering why it was happening so sporadically. The vast majority of my products/add-to-cart forms do have attributes, but there are a few products I sell with no options.
And indeed, after testing, viewing product displays which have an empty add-to-cart form are the ones creating this error.
Comment #4
dm66 commentedI confirm this problem.
I change string 2072 in commerce_cart.module to
This fixed the problem.
Comment #5
TynanFox commentedThank you dm66. #4 worked for me too.
I would make a patch to test etc...but I don't have the skills. :( #NotaDeveloper
Comment #6
vagelis-prokopiou commentedHere is a patch guys. Please, test.
To apply:
1.
cdin the commerce module.2. Run:
curl "https://www.drupal.org/files/issues/array_key_exists_error-2924792-6.patch" | git apply -v;Comment #7
Neo13 commentedThis patch fixes the issue with notices but there is another issue with ajax on attribute change. The response is 200 parserror with empty resulttext.
Comment #8
rszrama commentedLinking to #2927572: parameter 2 to be array, null given commerce_cart.module:2072.
Comment #9
babin commentedThanks this patch worked for me too.
Comment #10
TynanFox commentedDon't see any reason to stop it from committing
Comment #11
hunlaprovo commentedIt's been a couple of years. I got the error, updated it now and came out. Repair is good. Thank you so much dm66
if (isset($form['attributes'][$used_field_name]) && array_key_exists('#default_value', $form['attributes'][$used_field_name])) {
Comment #12
rszrama commentedTook me a while to confirm the issue here: it's when you have optional attribute fields on a product type and the product display does not reference any products that use the first one by sort order but do use a higher weighted field. For folks who reported / confirmed, make sure in the future to provide some context about your form to make that a little easier - I thought I was just crazy when I couldn't reproduce it for the last hour. : P
That said, I'm reviewing the patch now, because I'm not sure it shouldn't be || instead of &&.
Comment #13
rszrama commentedAlrighty, I've reviewed this and the logic resulting in the warnings / notices, and it does appear the && was correct but that we could also detect the issue a little earlier. Basically, the reason the isset() would fail is because the attribute field element is not added to the form array when we only add NULL values to its $used_options array:
This code is all designed to only permit products on the form that match the sequence of selected attributes. So what we can do in the offending loop is check to see if the $used_options array for the field we're matching products against has already been removed from the $qualifying_fields array beneath a similar exception check that's already there:
That said, there's no reason not to include the isset() check as well, so I'm adding both. : )
Patch attached for review / testbot.
Comment #15
rszrama commentedCommitting.
Comment #16
gundartb commentedI can confirm that the patch in #13 fixes the problem.
I have thousands of products with many attributes and some need to be set to NONE which was not only throwing hundreds of errors but slowing down the product selection process as well... everytime a customer changed a color or option etc.. it would take 10+ seconds to load and we were getting complaints of slow performance...
This patch not only gets rid of the errors, it speeds things up tremendously! THANKS
Comment #18
ConradFlashback commented#13 works
Please commit.
Comment #19
mglamanSee #14.
Comment #20
tonytheferg commentedHi, I am noticing that each change I make in the selection box takes a little bit longer than the previous one, and eventually i get a 403 error. is that something that is rectified by this patch?