Problem/Motivation

I'm getting a fatal error when building a view of a Product Variation and adding a field of the 'Rendered Entity' of the Product Variation and choosing 'Add to Cart Form' for the Display Mode of the Rendered Entity.

I see this error in the Developer Console:

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /admin/structure/views/view/upcoming_events/preview/default
StatusText: OK
ResponseText: Error: Call to a member function bundle() on null in commerce_variation_cart_form_theme_suggestions_alter() (line 125 of /code/web/modules/contrib/commerce_variation_cart_form/commerce_variation_cart_form.module).

Steps to reproduce

Create a View of a Product Variation.
Add a field of the 'Rendered Entity'
Choose 'Add to Cart Form' for the Display Mode
Note the error displayed in the Developer Console.

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

brooke_heaton created an issue. See original summary.

brooke_heaton’s picture

Status: Needs work » Active
brooke_heaton’s picture

Patch adds isset checks for product and variation variables.

brooke_heaton’s picture

Assigned: Unassigned » brooke_heaton
Status: Active » Needs work
brooke_heaton’s picture

Status: Needs work » Needs review
johnpitcairn’s picture

Status: Needs review » Needs work

Thanks. I do wonder why those variables aren't getting set in the first place. I'll investigate further when I get the chance.

brooke_heaton’s picture

@john-pitcairn - I wasn't able to dig that deep. I was just trying to get myself out of a broken view. Wish I could give more insight. Perhaps it's template related, but I'm not sure.

brooke_heaton’s picture

@john-pitcairn - can you clarify what work is needed here? Are you wanting to find out specifically why the variables aren't getting set?

johnpitcairn’s picture

Yeah - rather than just wrapping in a check for null (I'm not saying that's a bad idea), is there something else the module or you should be doing.

Can I clarify - Is "add to cart form" your own custom view mode you created for product variation entities? Because it's also the name of a field formatter for the product's variation reference field.

johnpitcairn’s picture

OK I can certainly reproduce it. It doesn't matter what the view mode is, the error happens immediately when you add the field. Could you please check your console errors at that step, before configuring or saving the added field?

I'll see what I can figure out in the debugger.

Are you using D8 or D9? I notice in D9 some pre-render callback is also producing a TrustedCallback exception, before this module starts any rendering of the pseudo-field.

And do you wind up with a workable view, with add to cart buttons?

johnpitcairn’s picture

Assigned: brooke_heaton » Unassigned
johnpitcairn’s picture

There's definitely a problem here, but at the moment I'm a little stumped, getting seemingly a different error related to untrusted pre-render callbacks and an empty langcode after that. Your exact Drupal version, and the full text of the error message would be useful.

vitaliyb98’s picture

Hi, I've checked this issue and have an idea why it happens.
I believe this is not an issue with this module itself.

It looks like a problem related to orphaned variations (variations without a parent product). From my experience, this issue often appears after migrations when a variation is migrated without its parent product or when variations are created programmatically and, due to a mistake, end up without an associated product.

At the moment, I don't think it's strictly necessary to apply a fix for this.
However, to improve stability, we could add a condition that checks whether the variation has a parent product and, if not, simply avoid rendering the "Add to cart" form.

vitaliyb98’s picture

Status: Needs work » Needs review

Added a possible fix with tests for it.

johnpitcairn’s picture

Status: Needs review » Needs work

This should call $entity->getProduct() earlier and avoid loading the service or creating the order item if it is missing.

I think "orphaned" is not the right name, it implies there was a product that has been removed, which is unlikely - just "missing product" and testMissingProduct() ?

vitaliyb98’s picture

Status: Needs work » Needs review

Hi @johnpitcairn,

Thanks for you feedback! I made changes according to it

johnpitcairn’s picture

Status: Needs review » Fixed

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

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

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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