Skip to content

Conversation

@barryhughes
Copy link
Member

New hooks are:

  1. woocommerce_before_delete_order
  2. woocommerce_before_trash_order

These fire immediately before an (HPOS) order is deleted or trashed, and act as analogs to delete_post and wp_trash_post in the case of CPT objects.

Closes #34231.

How to test the changes in this Pull Request:

It should be sufficient to review the code, there are no functional differences (since there is no code within core that uses these new actions).

FOR PR REVIEWER ONLY:

  • I have reviewed that everything is sanitized/escaped appropriately for any SQL or XSS injection possibilities. I made sure Linting is not ignored or disabled.

New hooks are:

1. `woocommerce_before_delete_order`
2. `woocommerce_before_trash_order`
@barryhughes barryhughes requested review from a team and jorgeatorres and removed request for a team September 28, 2022 01:52
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Sep 28, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Sep 28, 2022

Test Results Summary

Commit SHA: 6957745

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests11800201200m 42s
E2E Tests15012152017958m 48s
To view the full API test report, click here.
To view the full E2E test report, click here.
To view all test reports, visit the WooCommerce Test Reports Dashboard.

@barryhughes
Copy link
Member Author

✍🏽 E2E fails seem unrelated and are being investigated. Internal link: p1664377590875839-slack-C01BWDDTGKX

Copy link
Member

@jorgeatorres jorgeatorres left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM 👍. Thanks @barryhughes!

One question, though: what are your thoughts on adding these hooks to the CPT datastore too? It might help with compatibility.

@barryhughes
Copy link
Member Author

One question, though: what are your thoughts on adding these hooks to the CPT datastore too? It might help with compatibility.

That's a good question 🤔

It probably would be helpful, since then only one hook is needed, but for them to be truly equivalent I think we would need to hook into the equivalent WordPress actions, such as (illustrative only):

add_action( 
    'delete_post', 
    function( $post_id, $post ) {
        if ( in_array( $post->post_type, wc_get_order_types(), true ) ) {
            do_action( 'woocommerce_before_delete_order', $post_id, $post );
        }
    },
    10,
    2
);

(Because if we modify Abstract_WC_Order_Data_Store_CPT::delete() and trigger these new actions from there, they won't really be equivalent—they would run before pre_delete_post which might mean the order isn't going to be deleted at all.)

I think that would probably work ... but we'd need to make it crystal clear that the hook is fired from both locations. Otherwise, a plugin author might add woocommerce_before_delete_order to make their code work with HPOS orders, without removing existing callbacks hooked into delete_post, which could lead to problems.

Perhaps a way to mitigate this would be to supply a third parameter, which would be the data-store class.

wdyt @jorgeatorres?

@barryhughes
Copy link
Member Author

Or perhaps we don't add duplicate hooks for the CPT data-store, but rename these two new hooks to something like:

  • woocommerce_before_delete_cot_order
  • woocommerce_before_trash_cot_order

@jorgeatorres
Copy link
Member

It probably would be helpful, since then only one hook is needed, but for them to be truly equivalent I think we would need to hook into the equivalent WordPress actions, such as (illustrative only) [...]

This looks ok, but then you'd receive a post instead of an order, unless we load the order again at this point, which I'm not sure we should be doing.

they would run before pre_delete_post which might mean the order isn't going to be deleted at all

Yep, I didn't think of pre_delete_post 😅. Though this sort of disconnection between datastore and the underlying post already occurs: if you call $order->delete( true ); it can still happen that the post isn't deleted (via pre_delete_post) but, regardless, the datastore sets the order ID to 0 and even fires the woocommerce_delete_order hook 🤷‍♂️.

Otherwise, a plugin author might add woocommerce_before_delete_order to make their code work with HPOS orders, without removing existing callbacks hooked into delete_post, which could lead to problems.

I'm thinking this is probably going to be the case, so even though I would've preferred that some canonical actions are always called (instead of depending on the datastore), your current implementation is probably what makes the most sense in the spirit of not breaking existing code. If we receive requests to add the same hook to the posts datastore, we might think about doing something more clever (including not firing both delete_post and the new hook for the same order or something like that).

Given the above, I'm going to merge this as-is, but we can always iterate later. Do let me know if you feel otherwise!

@jorgeatorres jorgeatorres merged commit 5cb5be8 into trunk Sep 30, 2022
@jorgeatorres jorgeatorres deleted the fix/34231-before-delete-hooks branch September 30, 2022 12:11
@github-actions github-actions bot added this to the 7.1.0 milestone Sep 30, 2022
@github-actions
Copy link
Contributor

Hi @jorgeatorres, thanks for merging this pull request. Please take a look at these follow-up tasks you may need to perform:

  • Add the release: add testing instructions label

@barryhughes
Copy link
Member Author

Thanks: sounds like a plan—as you say, we can always iterate further as adoption of HPOS grows and we get more data 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plugin: woocommerce Issues related to the WooCommerce Core plugin.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[COT] Add corresponding hooks for wp_trash_post and delete_post for COT implementation, that should be fired before status changes.

3 participants