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

[GTIN] Add WP CLI command for migration #2662

Open
wants to merge 6 commits into
base: add/support-core-gtin-field
Choose a base branch
from

Conversation

puntope
Copy link
Contributor

@puntope puntope commented Nov 1, 2024

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:

Screenshot 2024-11-01 at 12 17 25

Detailed test instructions:

  1. Checkout the PR and prepare some products for GTIN migration (it should be published variation or simple and have the GTIN in Google for WooCommerce tab and not in Inventory)
  2. Run WP CLI wp wc g4wc gtin-migration start
  3. See the GTIN migrated.

Additional details:

Changelog entry

Add - WP CLI Command for GTIN Migration

@puntope puntope self-assigned this Nov 1, 2024
@github-actions github-actions bot added type: enhancement The issue is a request for an enhancement. changelog: add A new feature, function, or functionality was added. labels Nov 1, 2024
@puntope puntope requested a review from a team November 1, 2024 18:30
@puntope puntope marked this pull request as ready for review November 1, 2024 18:33
Copy link

codecov bot commented Nov 1, 2024

Codecov Report

Attention: Patch coverage is 0% with 81 lines in your changes missing coverage. Please review.

Project coverage is 65.1%. Comparing base (02e6224) to head (476adf7).
Report is 3 commits behind head on add/support-core-gtin-field.

Files with missing lines Patch % Lines
src/Utility/WPCLIMigrationGTIN.php 0.0% 80 Missing ⚠️
...ernal/DependencyManagement/CoreServiceProvider.php 0.0% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@                       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     
Flag Coverage Δ
php-unit-tests 65.1% <0.0%> (-0.3%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ernal/DependencyManagement/CoreServiceProvider.php 0.0% <0.0%> (ø)
src/Utility/WPCLIMigrationGTIN.php 0.0% <0.0%> (ø)

Copy link
Contributor

@mikkamp mikkamp left a 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:
image

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' ] );
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tweaked here 173824b


$gtin = $this->attribute_manager->get_value( $product, 'gtin' );
if ( $gtin ) {
$product->set_global_unique_id( $gtin );
Copy link
Contributor

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".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tweaked here 31097f6

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() ) );
Copy link
Contributor

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?

@puntope
Copy link
Contributor Author

puntope commented Nov 6, 2024

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 --debug to see them.

@puntope
Copy link
Contributor Author

puntope commented Nov 6, 2024

how are we handling yoast_seo values?

Comming in another PR (we need to do it also for the AS version)

@puntope puntope requested a review from mikkamp November 6, 2024 17:02
Copy link
Contributor

@mikkamp mikkamp left a 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 );
Copy link
Contributor

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 ) ) {
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: add A new feature, function, or functionality was added. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants