I hate to be the bearer of bad news again. :(

I just upgraded to 7.x-1.15. Upon viewing any of my product displays, no matter which type, the following entry is shown in my DB log:

Warning: reset() expects parameter 1 to be array, null given in commerce_cart_add_to_cart_form() (line 2110 of ...sites/all/modules/commerce/modules/cart/commerce_cart.module).

The checkout workflows are unaffected and there doesn't seem to be any problems for end-user workflow.

I am using Drupal 7.67 with Drupal Commerce 7.x-1.15 and PHP 7.2.

Comments

TynanFox created an issue. See original summary.

TynanFox’s picture

It would appear this is occurring due to the changes made regarding generation of the add_to_cart form. Since that issue was worked on and patched for over 5 years, I don't even know where I would begin diving into the changes to determine the root cause. :( I am happy to provide any further information needed regarding my installation or product types or whatever!

gaijinu’s picture

I can confirm the issue

TynanFox’s picture

Priority: Normal » Major
Status: Active » Needs review
StatusFileSize
new859 bytes

Here is a basic patch, the point of which is merely to suppress the PHP error warning/message. My entire DB log is FULL of this error message, so I am uploading this as a temporizing solution to stop this behavior, but it does NOT address the underlying problem causing this error, and a more formal solution is needed as well as input from the maintainers.

Side note: hooray for my first patch! :)

travis-bradbury’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Setting this to Needs Work for a few issues but the general approach seems fine to me (though I don't really know why it's breaking for you but didn't cause problems for anyone involved with #2293537: Cart form attributes selects do not work with translated taxonomies/products).

field_get_items() is not guaranteed to return an array, so assuming it would return an array with an element 0 was incorrect.

That was done in several places so I think it should get fixed in multiple places, not just commerce_cart_add_to_cart_form().

-            $field_value = reset($field_items[0]);
+            if (!empty($field_items[0])){
+              $field_value = reset($field_items[0]);
+            }else{
+              $field_value = NULL;
+            }

There's a few changes necessary to meet the Drupal coding standards: https://www.drupal.org/docs/develop/standards

Use spaces between parentheses/braces - so if (!empty(...)){ would be if (!empty(...)) {.
put else on a new line, not in line with the closing brace of the if statement.

And to commit a patch it would be necessary to write a test that demonstrates the problem - which might require a sleuth to determine the root cause.

TynanFox’s picture

Happy to work on the issues with coding standards, etc.

However, I have only intermittently examined the code in-depth of just a few parts of the Commerce core code base, and admittedly, I haven't taken a deep dive into the Cart submodule. That said, I am quite familiar with my site's architecture, so I am happy to dig in and try to locate a root cause. But perhaps you could point me in the right direction? If you were investigating this, where would YOU start? Happy to give it a go from there.

travis-bradbury’s picture

I haven't tried replicating the issue yet but I'd venture a guess that you have at least one attribute field on your product variation that has no value. Is that true?

TynanFox’s picture

Yes indeed. I use Commerce Sale Price, which includes attributes for "On Sale" "Start Date" "End Date" etc. On very many of my products, these fields are left blank/empty/null. Additionally I have some other attribute fields which may or may not have data depending on the product.

TynanFox’s picture

StatusFileSize
new875 bytes
new790 bytes

Basic update of the patch to reflect the coding standards you mentioned (and also to allow me to learn more about how Git works and how to create an Interdiff). Still looking into the root cause, etc.

TynanFox’s picture

Whoops, in all my newbie glory, I accidentally committed my patch in #9 directly to the 7.x-1.x branch. Sorry. If anyone out there sees/wants to revert that commit, I won't be offended. I only meant to do it to my working branch.

pvasilion’s picture

We are experiencing this on a client site as well. We don't use the Commerce Sale Price module.

Going to apply the patch for now.

waverate’s picture

Patch at #9 works for me. Thank you @TynanFox.

wolfshine’s picture

I can confirm the issue when you have at least one attribute field on your product variation that has no value.

rszrama’s picture

Priority: Major » Normal
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3.1 KB

Thanks for the fix, @TynanFox! I was able to confirm it fixed the issue - we obviously didn't test the previous changes with optional attributes (and apparently never added full test coverage for them).

Also, re: @tbradbury's review, I did expand the patch to the 3 other places turning up reset() warnings for me in my test environment. Likely a factor of me having multiple types of attributes on the test product display turned up the other ones.

rszrama’s picture

Status: Needs review » Fixed

I saw a few more places where field values are assumed to be an array, but in context, it isn't clear to me that setting a default value to NULL in the absence of a response would be the right behavior, so I'm leaving them alone until we see bug reports. Committed!

  • rszrama committed 7beed98 on 7.x-1.x authored by TynanFox
    Issue #3057706 by TynanFox, rszrama: fix Add to Cart form warnings...

Status: Fixed » Closed (fixed)

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

Infinitee’s picture

I was still getting this warning to the point that my host charged me a $900 penalty for CPU overages. I was forced to put my site in maintenance mode until fixed.

All modules and core are up to date and I am using Commerce Kickstart.

Fixed with patch #14. I posted a notification in Commerce Kickstart issues... https://www.drupal.org/project/commerce_kickstart/issues/3108817#comment...

waqarit’s picture

#14 has worked for me.

rszrama’s picture

Good, because it's been committed for a while now. : D

hunlaprovo’s picture

Warning: reset() expects parameter 1 to be array, null given commerce_cart_add_to_cart_form()
/html/profiles/commerce_kickstart/modules/contrib/commerce/modules/cart/commerce_cart.module 2110
commerce_kickstart-7.x-2.66
Today I updated the

wqmeng’s picture

Also get many warnings after upgrade Commerce from 7.x-1.13 to 7.x-1.15

Warning: reset() expects parameter 1 to be array, null given in reset() (line 2141 of /home/www/com/sites/all/modules/commerce/modules/cart/commerce_cart.module).

line 2141, Should be the same error as above line 2110, As I have patched many other patches before.

vensires’s picture

Version: 7.x-1.15 » 7.x-1.x-dev

Changing version to make it more easily identifiable that it's commited in 7.x-1.x-dev. No stable release exists yet to fix this issue.