-
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
Refactor & fix undesired exceptions in AS Jobs #2666
Open
puntope
wants to merge
1
commit into
add/support-core-gtin-field
Choose a base branch
from
fix/prevent-errors-as
base: add/support-core-gtin-field
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ | |||||
|
||||||
namespace Automattic\WooCommerce\GoogleListingsAndAds\Utility; | ||||||
|
||||||
use Automattic\WooCommerce\GoogleListingsAndAds\HelperTraits\GTINMigrationUtilities; | ||||||
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Conditional; | ||||||
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Registerable; | ||||||
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service; | ||||||
|
@@ -22,7 +23,7 @@ | |||||
* @since x.x.x | ||||||
*/ | ||||||
class WPCLIMigrationGTIN implements Service, Registerable, Conditional { | ||||||
|
||||||
use GTINMigrationUtilities; | ||||||
|
||||||
/** @var AttributeManager */ | ||||||
public AttributeManager $attribute_manager; | ||||||
|
@@ -157,7 +158,6 @@ | |||||
* @return int The number of items processed. | ||||||
*/ | ||||||
protected function process_items( array $items ): int { | ||||||
// todo: refactor avoiding duplication with MigrateGTIN Job after https://github.com/woocommerce/google-listings-and-ads/pull/2656 gets merged. | ||||||
// update the product core GTIN using G4W GTIN | ||||||
$products = $this->product_repository->find_by_ids( $items ); | ||||||
$processed = 0; | ||||||
|
@@ -172,30 +172,29 @@ | |||||
|
||||||
// void if core GTIN is already set. | ||||||
if ( $product->get_global_unique_id() ) { | ||||||
WP_CLI::debug( sprintf( 'GTIN has been skipped for Product ID: %s - %s. GTIN was found in Product Inventory tab.', $product->get_id(), $product->get_name() ) ); | ||||||
$this->debug( $this->error_gtin_already_set( $product ) ); | ||||||
continue; | ||||||
} | ||||||
|
||||||
$gtin = $this->attribute_manager->get_value( $product, 'gtin' ); | ||||||
if ( ! $gtin ) { | ||||||
WP_CLI::debug( sprintf( 'GTIN has been skipped for Product ID: %s - %s. No GTIN was found', $product->get_id(), $product->get_name() ) ); | ||||||
$this->debug( $this->error_gtin_not_found( $product ) ); | ||||||
continue; | ||||||
} | ||||||
|
||||||
$gtin = str_replace( '-', '', $gtin ); | ||||||
|
||||||
$gtin = $this->prepare_gtin( $gtin ); | ||||||
if ( ! is_numeric( $gtin ) ) { | ||||||
WP_CLI::debug( sprintf( 'GTIN [ %s ] has been skipped for Product ID: %s - %s. Invalid GTIN was found.', $gtin, $product->get_id(), $product->get_name() ) ); | ||||||
$this->debug( $this->error_gtin_invalid( $product, $gtin ) ); | ||||||
continue; | ||||||
} | ||||||
|
||||||
try { | ||||||
$product->set_global_unique_id( $gtin ); | ||||||
$product->save(); | ||||||
++$processed; | ||||||
WP_CLI::debug( sprintf( 'GTIN [ %s ] has been migrated for Product ID: %s - %s', $gtin, $product->get_id(), $product->get_name() ) ); | ||||||
$this->debug( $this->successful_migrated_gtin( $product, $gtin ) ); | ||||||
} catch ( Exception $e ) { | ||||||
WP_CLI::error( sprintf( 'GTIN [ %s ] for Product ID: %s - %s has an error - %s', $gtin, $product->get_id(), $product->get_name(), $e->getMessage() ), false ); | ||||||
$this->error( $this->error_gtin_not_saved( $product, $gtin, $e ) ); | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -211,4 +210,25 @@ | |||||
public static function is_needed(): bool { | ||||||
return defined( 'WP_CLI' ) && WP_CLI; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Add some info in the debug console. | ||||||
* Add --debug to see this kind of logs in WP CLI | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a small nitpick about the wording (this should be plural):
Suggested change
|
||||||
* | ||||||
* @param string $message | ||||||
* @return void | ||||||
*/ | ||||||
protected function debug( string $message ): void { | ||||||
WP_CLI::debug( $message ); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Add some info in the error console. | ||||||
* | ||||||
* @param string $message | ||||||
* @return void | ||||||
*/ | ||||||
protected function error( string $message ): void { | ||||||
WP_CLI::error( $message, false ); | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not a big deal, and we don't seem to be consistent. But in most cases in the code we add
use WC_Product
to be able to remove the\
. But I'm not attached to either method so both are fine.