Score:0

How do I save a specific "state" of the product in the order item?

br flag

I'd be necessary for my site to save "a specific state" of the product(s) and relative variation(s) purchased in the order.

I mean, with the "standard" Commerce configuration, in the relative order item are stored only:

a reference to the purchasable entity, a quantity, a unit price and a total price

Therefore, for example if the body of the purchased product or relative variation is changed after a user buy it, it is impossible to get and show the value of it at the moment of the purchase.

Is there a module\way to preserve that information in the order item?

I'm thinking at worst to somehow programmatically save that at the moment of the order item creation, but before proceeding I'd like to check existing solutions etc but unfortunately I've found nothing about.

I am using the Commerce module 2.x.

Dylan avatar
kr flag
I think the proper way would be to have product revisions, and then relate the order item to that revision, but that may only ever be in the form of a patch ex: https://www.drupal.org/project/commerce/issues/2656896. I would probably either opt to 1) never edit, only create new products or 2) store a JSON output of the product data in an order item field.
Giuseppe avatar
br flag
I'd avoid to put a so critical patch. Good idea with the json field. Is there a service to save an entity (namely commerce product, in this case) to JSON? Should I use serialization api?
Dylan avatar
kr flag
Honestly I would probably try to $entity->toArray() and json_encode() after I parsed out the data I actually want, ----- but it would be coolers to serialize the entity like: https://www.drupal.org/docs/8/api/serialization-api/serialization-api-overview ----- \Symfony\Component\Serializer\SerializerInterface ----- $output = $this->serializer->serialize($entity, 'json'); ----- $entity = $this->serializer->deserialize($output, Drupal\commerce_product\Entity\ProductVariation::class, 'json');
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.