IRC conversation explaining this situation and potential solution:

15:12 bojanz: joshmiller: Look at \Drupal\commerce_product\LineItemTypeMap
15:12 bojanz: joshmiller: it caches this relationship: Product type -> Product variation type -> Line item type
15:13 bojanz: joshmiller: so if you ask for the line item type for a product type, it gives you one, without needing to load the first two config entities
15:13 joshmiller: ah, cool
15:13 bojanz: joshmiller: look at commerce_product_entity_bundle_create(), it clears that cache when a new product type is created
15:13 bojanz: (you can't edit the product type -> product variation type relationship afterwards)
15:14 bojanz: joshmiller: however, you can change the line item type specified on a product variation type
15:14 bojanz: joshmiller: so we need to write a hook that reacts on commerce_product_variation_type save
15:14 bojanz: joshmiller: if the save was an update, and the line item type changed, clear the cache
15:16 bojanz: joshmiller: there are no D8 concepts in this task :)
15:17 joshmiller: yay! hooks!
15:20 bojanz: joshmiller: now, I am adding a complication
15:20 bojanz: joshmiller: instead of using a normal hook, we'll use the method version of it
15:20 bojanz: joshmiller: if you create a public function postSave(EntityStorageInterface $storage, $update = TRUE) { on the ProductVariationType, it will function the same as a post save hook
15:21 bojanz: joshmiller: $update tells you whether the entity was modified or created, and $this->original->lineItemType gives you the original value to compare against
15:21 bojanz: joshmiller: Commerce already has examples of such postSave() methods, so you can look at them
15:22 bojanz: (the actual line for clearing the cache remains the same as in commerce_product_entity_bundle_create)

Comments

joshmiller created an issue. See original summary.

joshmiller’s picture

StatusFileSize
new755 bytes

I think this is what Bojan was getting at? Might be missing a hook, but I think this is right...

joshmiller’s picture

StatusFileSize
new1.07 KB

Bojan's feedback and an updated patch:

15:44 bojanz: joshmiller: you need a use statement for EntityStorageInterface, otherwise the code will crash
15:44 bojanz: joshmiller: you also need to check that the line item type setting has changed
15:44 bojanz: so after comparing $update you need to compare $this->lineItemType with $this->original->lineItemType
15:45 joshmiller: your mom needs to be compared
15:45 joshmiller: ok, will check back in
15:45 bojanz: she's uncomparable

My thoughts

I think the patch included is wrong and it should be either comparing the productType (instead of the lineItemType) or it should be a public postSave() in LineItemTypeForm. But, leaving this here because bojan who has been feeding me breadcrumbs to get me to the right place has disappeared into a fog of St. Patrick celebration.

joshmiller’s picture

Status: Active » Needs review
bojanz’s picture

Status: Needs review » Needs work

You're adding the method to the wrong class. Such methods go to the entity class, not the form. So, ProductVariationType in this case.

joshmiller’s picture

StatusFileSize
new978 bytes

Well that makes more sense :D

Attached is an updated patch ... phpstorm doesn't like the $this->original but that may not be a problem... ?

Thanks for your patience bojan.

joshmiller’s picture

Status: Needs work » Needs review

  • bojanz committed 252c7d4 on 8.x-2.x authored by joshmiller
    Issue #2689535 by joshmiller: Clear the Product -> Line item cache on...
bojanz’s picture

Status: Needs review » Fixed

Boom!

Status: Fixed » Closed (fixed)

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