Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite clone method to prevent duplicate data when saving a clone. #37313

Merged
merged 5 commits into from Mar 21, 2023

Conversation

vedanshujain
Copy link
Contributor

@vedanshujain vedanshujain commented Mar 20, 2023

All Submissions:

Changes proposed in this Pull Request:

This method overwrites the base class's clone method to make it a no-op. In the base class WC_Data, we are unsetting the meta_id to clone. It seems like this was done to avoid conflicting the metadata when duplicating products (see #14118). However, doing that does not seem necessary for orders.

In fact, when we do that for orders, we lose the capability to clone orders with custom metadata by caching plugins. This is because, when we clone an order object for caching, it will clone the metadata without the ID. Unfortunately, when this cached object with nulled meta ID is retrieved, WC_Data will consider it as a new meta and will insert it as a new meta-data causing duplicates if the object is saved.

Eventually, we should move away from overwriting the __clone method in the base class itself, since it's easily possible to still duplicate the product without having to hook into the __clone method.

How to test the changes in this Pull Request:

Unfortunately, this is not easy to test from UI since this needs cache setting unsettling. On a WP shell, run these commands:

For an order with ID XX, with HPOS enabled:

\Automattic\WooCommerce\Utilities\OrderUtil::orders_cache_usage_is_enabled(); // confirm order cache is enabled, should be true.

$order = wc_get_order( xx ); // retrieve any existing order, this will also cache it.
$order->add_meta_data( 'test', 'test' ); 
$order->save(); // this makes sure that there is at least one custom meta for order. This will also invalidates the cache.
wc_get_order( $order->get_id() ); // sets the cache again.

$refreshed_order  = wc_get_order( $order->get_id() );
$refreshed_order->save_meta_data(); // try to save the order, this will insert duplicated metadata again on trunk, but will be fixed on this branch.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you created a changelog file for each project being changed, ie pnpm --filter=<project> changelog add?
  • Have you included testing instructions?

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.

@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Mar 20, 2023
@codecov
Copy link

codecov bot commented Mar 20, 2023

Codecov Report

Merging #37313 (56d4bbc) into trunk (c4f0170) will decrease coverage by 0.0%.
The diff coverage is n/a.

❗ Current head 56d4bbc differs from pull request most recent head 3a25942. Consider uploading reports for the commit 3a25942 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             trunk   #37313     +/-   ##
==========================================
- Coverage     45.8%    45.8%   -0.0%     
  Complexity   17196    17196             
==========================================
  Files          429      429             
  Lines        64911    64911             
==========================================
- Hits         29703    29702      -1     
- Misses       35208    35209      +1     
Impacted Files Coverage Δ
...ocommerce/includes/abstracts/abstract-wc-order.php 75.8% <ø> (ø)

... and 1 file with indirect coverage changes

@github-actions
Copy link
Contributor

github-actions bot commented Mar 20, 2023

Test Results Summary

Commit SHA: 3a25942

Test 🧪Passed ✅Failed 🚨Broken 🚧Skipped ⏭️Unknown ❔Total 📊Duration ⏱️
API Tests25900202611m 7s
E2E Tests1910010020113m 50s

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.

jorgeatorres
jorgeatorres previously approved these changes Mar 20, 2023
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 looks good to me. Thank you @vedanshujain!

I'm approving, but I can't merge as the e2e tests are failing (though seems unrelated). I even tried updating this branch with the latest trunk and that didn't help. Something is broken with our e2e tests atm 😬.

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.

LGTM.

@jorgeatorres jorgeatorres merged commit eb297c0 into trunk Mar 21, 2023
18 checks passed
@jorgeatorres jorgeatorres deleted the fix/order-cache-duplicate branch March 21, 2023 23:23
@github-actions github-actions bot added this to the 7.7.0 milestone Mar 21, 2023
@vedanshujain vedanshujain modified the milestones: 7.7.0, 7.6.0 Mar 22, 2023
github-actions bot pushed a commit that referenced this pull request Mar 22, 2023
…#37313)

* Add unit test to simulate duplicate meta insert.

* Overwrite clone method to prevent duplicate datq when saving a clone.

* Add changelog.

* Coding standard fixes.

* Fix phpcs

---------

Co-authored-by: Jorge A. Torres <jorge.torres@automattic.com>
lsinger pushed a commit that referenced this pull request Mar 27, 2023
…#37313)

* Add unit test to simulate duplicate meta insert.

* Overwrite clone method to prevent duplicate datq when saving a clone.

* Add changelog.

* Coding standard fixes.

* Fix phpcs

---------

Co-authored-by: Jorge A. Torres <jorge.torres@automattic.com>
lsinger pushed a commit that referenced this pull request Mar 27, 2023
* Overwrite clone method to prevent duplicate data when saving a clone. (#37313)

* Add unit test to simulate duplicate meta insert.

* Overwrite clone method to prevent duplicate datq when saving a clone.

* Add changelog.

* Coding standard fixes.

* Fix phpcs

---------

Co-authored-by: Jorge A. Torres <jorge.torres@automattic.com>

* Prep for cherry pick 37313

---------

Co-authored-by: Vedanshu Jain <vedanshu.jain.2012@gmail.com>
Co-authored-by: Jorge A. Torres <jorge.torres@automattic.com>
Co-authored-by: WooCommerce Bot <no-reply@woocommerce.com>
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.

None yet

2 participants