Describe your bug or feature request.

We developed a Drupal 11 website for a large bike manufacturer using Drupal Commerce. The bikes on the website are commerce products (variants actually).
The stores that sell this brand are Commerce Stores, and they each have at least one (Drupal) user (a dealer) that can log into the website. When a dealer is logged in, he can order bikes.

The client contacted us that a specific dealer is unable to order bikes.
When I masquerade as this dealer, I can see the error in the logs:

Error: Call to a member function isPublished() on null in Drupal\commerce_cart\CartProvider->isEligibleCart() (regel 263 van /var/www/html/web/modules/contrib/commerce/modules/cart/src/CartProvider.php)

#0 /var/www/html/web/modules/contrib/commerce/modules/cart/src/CartProvider.php(222): Drupal\commerce_cart\CartProvider->isEligibleCart()
#1 /var/www/html/web/modules/contrib/commerce/modules/cart/src/CartProvider.php(161): Drupal\commerce_cart\CartProvider->loadCartData()
#2 /var/www/html/web/modules/contrib/commerce/modules/cart/src/CartProvider.php(148): Drupal\commerce_cart\CartProvider->getCartIds()
#3 /var/www/html/web/modules/contrib/commerce/modules/cart/src/CartLazyBuilders.php(56): Drupal\commerce_cart\CartProvider->getCarts()
#4 [internal function]: Drupal\commerce_cart\CartLazyBuilders->cartBlock()
#5 /var/www/html/web/core/lib/Drupal/Core/Security/DoTrustedCallbackTrait.php(107): call_user_func_array()
#6 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(876): Drupal\Core\Render\Renderer->doTrustedCallback()
#7 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(414): Drupal\Core\Render\Renderer->doCallback()
#8 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(250): Drupal\Core\Render\Renderer->doRender()
#9 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(141): Drupal\Core\Render\Renderer->doRenderRoot()
#10 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}()
#11 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(140): Drupal\Core\Render\Renderer->executeInRenderContext()
#12 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(167): Drupal\Core\Render\Renderer->renderInIsolation()
#13 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(204): Drupal\Core\Render\Renderer->doRenderPlaceholder()
#14 /var/www/html/web/core/modules/big_pipe/src/Render/BigPipe.php(730): Drupal\Core\Render\Renderer->renderPlaceholder()
#15 /var/www/html/web/core/modules/big_pipe/src/Render/BigPipe.php(500): Drupal\big_pipe\Render\BigPipe->renderPlaceholder()
#16 [internal function]: Drupal\big_pipe\Render\BigPipe->Drupal\big_pipe\Render\{closure}()
#17 /var/www/html/web/core/modules/big_pipe/src/Render/BigPipe.php(507): Fiber->start()
#18 /var/www/html/web/core/modules/big_pipe/src/Render/BigPipe.php(256): Drupal\big_pipe\Render\BigPipe->sendPlaceholders()
#19 /var/www/html/web/core/modules/big_pipe/src/Render/BigPipeResponse.php(116): Drupal\big_pipe\Render\BigPipe->sendContent()
#20 /var/www/html/vendor/symfony/http-foundation/Response.php(397): Drupal\big_pipe\Render\BigPipeResponse->sendContent()
#21 /var/www/html/web/index.php(20): Symfony\Component\HttpFoundation\Response->send()
#22 {main}

The lines of code that throws the error looks like this:

    // A cart belonging to an unpublished store should no longer be eligible.
    if (!$cart->getStore()->isPublished()) {
      return FALSE;
    }

I have no idea why getStore returns null, but the error can be fixed by adding an extra check:

    // A cart without valid store should not be valid.
    if (!$cart->getStore()) {
      return FALSE;
    }
    // A cart belonging to an unpublished store should no longer be eligible.
    if (!$cart->getStore()->isPublished()) {
      return FALSE;
    }

I have attached a patch that adds this extra check.

Issue fork commerce-3552231

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

flyke created an issue. See original summary.

jsacksick’s picture

Could you open a merge request please?

Also, we can simply do the following instead:
if (!$cart->getStore()?->isPublished()). There is no need for a separate condition.

tbkot made their first commit to this issue’s fork.

tbkot’s picture

Status: Active » Needs review

@jsacksick, I've updated the condition there, so it should be good to go.

  • jsacksick committed 676da522 on 3.x authored by tbkot
    [#3552231] fix: Call to a member function isPublished() on null.
    
    By:...
jsacksick’s picture

Status: Needs review » Fixed

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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