Error on adding product to cart

Created on 6 February 2024, 9 months ago
Updated 17 September 2024, about 2 months ago

On the last two versions of the module (2.37 and 2.38) I receive the following error when adding a product to the cart:

TypeError: Drupal\commerce\Context::__construct(): Argument #1 ($customer) must be of type Drupal\Core\Session\AccountInterface, null given, called in /var/www/html/web/modules/contrib/commerce/modules/cart/src/Form/AddToCartForm.php on line 248 in Drupal\commerce\Context->__construct() (line 58 of modules/contrib/commerce/src/Context.php).

Drupal version: 10.2.2

🐛 Bug report
Status

Needs work

Version

2.40

Component

Cart

Created by

🇧🇪Belgium JonJacobs

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @JonJacobs
  • 🇺🇸United States rhovland Oregon

    This is caused by changes to AddToCartForm.php

    If you have a module that modifies the cart form such as a contrib module like commerce_pado or commerce_vado then check if there is an update that addresses this error.

    If you have a custom module that modifies the add to cart form then the code needs to be fixed there.

  • Status changed to Closed: outdated 6 months ago
  • Status changed to Needs work about 2 months ago
  • 🇮🇳India Manav

    Hi,
    I faced the same in one of my commerce projects which was working fine with commerce-2.36 but after upgrading to 2.40 ver (the latest version) I got the same issue.

    So as per the @rhovland in #2 🐛 Error on adding product to cart Needs work I checked all my custom codes and disabled all the modules and themes but I was still facing the same issue so I downgraded to commence-2.40 to 2.36 and its resolved my issue.

    I also checked with 2.37. 2.38, 2.39 but I got the same problem.

    So, it means that there is not an issue with the custom code but there is something that is passing incorrect values for anonymous sessions which is creating this issue.

    Drupal: 10.3
    PHP: 8.3
    MariaDb: 10.11

    I am reopening this issue as a bug for the 2.40 version.

  • First commit to issue fork.
  • First commit to issue fork.
  • 🇮🇱Israel jsacksick

    This isn't the right fix, the fix is to call the parent create method if you're swapping the add to cart form.

  • 🇬🇪Georgia maximkashuba Batumi

    For 2.40 version some services are missing during building AddToCartForm. So i added a constructor to get them.

  • 🇮🇱Israel jsacksick

    But do you have any custom or contrib code extending the add to cart form? If that is the case, they should be calling the parent create() method.

  • 🇬🇪Georgia maximkashuba Batumi

    yes, you are right. The form is extended changed the create method. Works as expected.
    Thank you.

Production build 0.71.5 2024