-
Notifications
You must be signed in to change notification settings - Fork 21
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
[GTIN] Add WP CLI command for migration #2662
base: add/support-core-gtin-field
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## add/support-core-gtin-field #2662 +/- ##
===============================================================
- Coverage 65.4% 65.1% -0.3%
- Complexity 4622 4641 +19
===============================================================
Files 475 476 +1
Lines 19337 19418 +81
===============================================================
Hits 12638 12638
- Misses 6699 6780 +81
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CLI command is working, but as explained in the comments I think we should handle fatals as well.
I also found the output to be a bit odd mixing the progress bar with output for each product, as it causes the progress bar to jump to multiple lines and there are no new lines in between:
How do other CLI commands handle that? Is it possible to only output status per product when we set a verbose / debug level of output?
* Register service and initialize hooks. | ||
*/ | ||
public function register(): void { | ||
WP_CLI::add_hook( 'after_wp_load', [ $this, 'register_commands' ] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how we can setup a environment without WP_CLI, but based on the doc here it sounds like it might not always be available. And since we aren't doing any conditional checks it seems like it would fatal here.
Should we make this class implement Conditional
and add a is_needed
function to check if WP_CLI is available?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tweaked here 173824b
src/Utility/WPCLIMigrationGTIN.php
Outdated
|
||
$gtin = $this->attribute_manager->get_value( $product, 'gtin' ); | ||
if ( $gtin ) { | ||
$product->set_global_unique_id( $gtin ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This call can throw an error, for example in cases where the GTIN is duplicate (which we didn't previously validate in the GLA data. When I test this I see the following error:
Fatal error: Uncaught WC_Data_Exception: Invalid or duplicated GTIN, UPC, EAN or ISBN. in woocommerce/includes/abstracts/abstract-wc-data.php:943
I didn't test this scenario fully with the regular importer, but I'm pretty sure it will trigger one of the scheduled actions to fail, which should probably be okay, but maybe it should be a bit more graceful so it's not a "fatal error".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tweaked here 31097f6
src/Utility/WPCLIMigrationGTIN.php
Outdated
if ( $gtin ) { | ||
$product->set_global_unique_id( $gtin ); | ||
$product->save(); | ||
WP_CLI::success( sprintf( 'GTIN [ %s ] has been migrated for Product ID: %s - %s', $gtin, $product->get_id(), $product->get_name() ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This success message also appears for invalid GTIN's, because set_global_unique_id
filters out incorrect characters. That makes the output rather odd:
I get this in the output:
Success: GTIN [ abcd ] has been migrated for Product ID: 115 - Variable No Sync - Tiny
GTIN Migration 13 % [=============> ] 0:01 / 0:14
Success: GTIN [ yoast_seo ] has been migrated for Product ID: 2556 - Aerodynamic Leather Wallet - Small
abcd
wouldn't have migrated, and how are we handling yoast_seo
values? Do we not import them from the Yoast data?
Hey @mikkamp Thanks for your suggestions. I tweaked the code addressing them: Since now some of the logs are debug mode. You need to add |
Comming in another PR (we need to do it also for the AS version) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes, it's working smoother now. I can correctly see it importing GTIN numbers now, and it doesn't halt the process when a duplicate is found.
Starting GTIN migration for 2290 products in the store.
GTIN Migration 13 % [======> ] 0:01 / 0:08
Error: Invalid or duplicated GTIN, UPC, EAN or ISBN.
GTIN Migration 100% [===================================================================================================] 0:01 / 0:08
Success: 1 GTIN was migrated in 1 seconds.
I just left a few small comments, but I'll go ahead and approve it since it pretty much ready to go.
WP_CLI::debug( sprintf( 'GTIN [ %s ] has been migrated for Product ID: %s - %s', $gtin, $product->get_id(), $product->get_name() ) ); | ||
++$processed; | ||
} catch ( Exception $e ) { | ||
WP_CLI::error( $e->getMessage(), false ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we combine this error with the product ID, so we can know what to fix? Right now I'm getting this as output:
Starting GTIN migration for 2290 products in the store.
GTIN Migration 13 % [============> ] 0:01 / 0:15
Error: Invalid or duplicated GTIN, UPC, EAN or ISBN.
GTIN Migration 100% [=========================================================================================] 0:01 / 0:15
Warning: No GTIN were migrated.
Which leaves me having to manually figure out which product I should fix.
} | ||
|
||
$gtin = str_replace( '-', '', $gtin ); | ||
if ( is_numeric( $gtin ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nitpick, but I would have used ! is_numeric
here with a continue
like is done for the error checking above. That way we can keep the condition and error message right next to each other. And we don't have to indent the try and catch another time.
Changes proposed in this Pull Request:
Closes part of #2616 as part of pcTzPl-2p2-p2.
This PR creates a WP CLI GTIN Migration command.
Screenshots:
Detailed test instructions:
wp wc g4wc gtin-migration start
Additional details:
Changelog entry