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.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | call_to_a_member_function_bundle_on_null_3212847_3.patch | 868 bytes | brooke_heaton |
Issue fork commerce_variation_cart_form-3212847
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
Comment #2
brooke_heaton commentedComment #3
brooke_heaton commentedPatch adds isset checks for product and variation variables.
Comment #4
brooke_heaton commentedComment #5
brooke_heaton commentedComment #6
johnpitcairn commentedThanks. I do wonder why those variables aren't getting set in the first place. I'll investigate further when I get the chance.
Comment #7
brooke_heaton commented@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.
Comment #8
brooke_heaton commented@john-pitcairn - can you clarify what work is needed here? Are you wanting to find out specifically why the variables aren't getting set?
Comment #9
johnpitcairn commentedYeah - 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.
Comment #10
johnpitcairn commentedOK 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?
Comment #11
johnpitcairn commentedComment #12
johnpitcairn commentedThere'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.
Comment #13
vitaliyb98 commentedHi, 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.
Comment #15
vitaliyb98 commentedAdded a possible fix with tests for it.
Comment #16
johnpitcairn commentedThis 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()?Comment #17
vitaliyb98 commentedHi @johnpitcairn,
Thanks for you feedback! I made changes according to it
Comment #19
johnpitcairn commented