Delete a shipment's profiles when it is deleted on order deletion

Created on 31 August 2022, about 2 years ago
Updated 3 February 2024, 10 months ago

Currently when a cart order is deleted (or any order for that matter), its shipments will be deleted via commerce_shipping_commerce_order_delete(), but any profiles associated with those shipments will remain. It seems like it should be safe to delete these profiles, as they should all have a uid == 0 and exist solely to provide an address for these shipments. If so, we should update the function to delete the profiles before the shipments are deleted.

Only caveat I can think of is if we should be using a deletion hook specific to shipments ... I just wasn't sure if that were really necessary since we already have the hook to respond to order deletion.

Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

🇺🇸United States rszrama

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇱Israel jsacksick

    We can do this from an event subscriber too (we already have a shipments subscriber).

  • Status changed to Needs review almost 2 years ago
    • jsacksick committed 37b4a52b on 8.x-2.x
      Issue #3307180 by khiminrm, jsacksick, rszrama, marchuk.vitaliy: Delete...
  • Status changed to Fixed almost 2 years ago
  • 🇮🇱Israel jsacksick

    Committed the patch from #9! Thanks everyone.

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

  • Status changed to Fixed over 1 year ago
  • 🇺🇸United States lisastreeter

    Took me a while to track down the cause of this error message that appeared after updating my site's modules:
    Drupal\Core\Entity\EntityStorageException: Update existing 'profile' entity while changing the ID is not supported.

    Realized it was this issue affecting custom code in which I delete an order's shipment after it's placed:
    $shipment->delete();

    Leaving this comment here in case anybody else gets the EntityStorageException error message and is looking for a fix. Adding this to my custom code, right before the delete, eliminated my bug:
    $shipment->set('shipping_profile', NULL);

  • 🇮🇱Israel jsacksick

    @lisastreeter: Are you saying this patch introduced a bug? Or is this due to custom code on your site?

  • 🇺🇸United States lisastreeter

    @jacksick. Sorry I was unclear! Totally due to custom code on my site. Not the fault of the patch.

    I only posted a message because it took me a little while to figure out why my checkout was broken after making a bunch of module updates. My custom code takes the shipping profile from the shipment and adds it directly to the order. So admins can easily edit both the billing and shipping addresses on the order form, side-by-side. We don't actually need the "shipment" after checkout. So I was just deleting it in custom event subscriber after attaching the shipping profile to the order. Patch did its job and deleted the shipment's shipping profile, but my custom code was assuming shipping profile still existed. Removing the reference before deleting the shipment was a simple one line fix in my custom code once I figured out what was happening.

  • 🇺🇸United States rhovland Oregon

    We are also experiencing issues due to this commit. We're deleting shipments via JSON API and then recreating them and linking the profile to the new shipment. This is causing the profile to disappear.

    The issue description says "Delete a shipment's profiles when it is deleted on order deletion" but this code deletes the shipment profile even if the order still exists. I'd consider that a regression.

  • 🇺🇸United States lisastreeter

    @rhovland I agree that the title of the issue doesn't really match the code in the patch. Though generally I do think it's a good idea to delete the referenced entities that belong to an entity when the entity is deleted.

    It may have been easier to identify potential issues in custom code if the title was, "Delete a shipment's profile when shipment is deleted." And perhaps the Release could have included a note about the possibility that shipment deletion within custom code could require rework.

  • 🇺🇸United States rhovland Oregon

    So it looks like this patch is technically correct and we're doing it wrong via JSON:API.

    However, if anyone is currently experiencing this problem and needs a quick fix to prevent profiles from being deleted when the shipment still exists here's a patch that adds that check. This definitely won't get committed.

  • 🇳🇱Netherlands finne Amsterdam

    Thanks rhovland, I had the same problem: we are deleting shipments using a custom action in the GUI. Since this commit the shipment's profiles are also deleted: in our case we linked the order billing profile. So that was gone too.

    We should probably rework the usage of profiles, but the patch is very welcome in the meantime.

  • Yeah, noticing this in migrations as well, rolling back shipments causes all the shipping profiles to get deleted.

Production build 0.71.5 2024