Skip to content

Commit

Permalink
Merge pull request #2677 from woocommerce/add/support-yoast
Browse files Browse the repository at this point in the history
[GTIN] Add Migration support for YOAST SEO
  • Loading branch information
puntope authored Nov 21, 2024
2 parents 0831421 + 66dfa38 commit 6155941
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
49 changes: 32 additions & 17 deletions src/HelperTraits/GTINMigrationUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsInterface;
use Exception;
use WC_Product;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -35,7 +36,7 @@ protected function get_gtin_hidden_version(): string {
* @return bool
*/
protected function is_gtin_available_in_core(): bool {
return version_compare( WC_VERSION, '9.2', '>=' ) && method_exists( \WC_Product::class, 'get_global_unique_id' );
return version_compare( WC_VERSION, '9.2', '>=' ) && method_exists( WC_Product::class, 'get_global_unique_id' );
}

/**
Expand Down Expand Up @@ -82,7 +83,7 @@ protected function get_gtin_migration_status(): string {
*
* @return OptionsInterface
*/
protected function options() {
protected function options(): OptionsInterface {
return $this->options ?? woogle_get_container()->get( OptionsInterface::class );
}

Expand All @@ -92,63 +93,77 @@ protected function options() {
* @param string $gtin
* @return string
*/
protected function prepare_gtin( string $gtin ) {
protected function prepare_gtin( string $gtin ): string {
return str_replace( '-', '', $gtin );
}

/**
* Gets the message when the GTIN is invalid.
*
* @param \WC_Product $product
* @param string $gtin
* @param WC_Product $product
* @param string $gtin
* @return string
*/
protected function error_gtin_invalid( \WC_Product $product, string $gtin ) {
protected function error_gtin_invalid( WC_Product $product, string $gtin ): string {
return sprintf( 'GTIN [ %s ] has been skipped for Product ID: %s - %s. Invalid GTIN was found.', $gtin, $product->get_id(), $product->get_name() );
}

/**
* Gets the message when the GTIN is already in the Product Inventory
*
* @param \WC_Product $product
* @param WC_Product $product
* @return string
*/
protected function error_gtin_already_set( \WC_Product $product ) {
protected function error_gtin_already_set( WC_Product $product ): string {
return sprintf( 'GTIN has been skipped for Product ID: %s - %s. GTIN was found in Product Inventory tab.', $product->get_id(), $product->get_name() );
}

/**
* Gets the message when the GTIN is not found.
*
* @param \WC_Product $product
* @param WC_Product $product
* @return string
*/
protected function error_gtin_not_found( \WC_Product $product ) {
protected function error_gtin_not_found( WC_Product $product ): string {
return sprintf( 'GTIN has been skipped for Product ID: %s - %s. No GTIN was found', $product->get_id(), $product->get_name() );
}

/**
* Gets the message when the GTIN had an error when saving.
*
* @param \WC_Product $product
* @param string $gtin
* @param Exception $e
* @param WC_Product $product
* @param string $gtin
* @param Exception $e
*
* @return string
*/
protected function error_gtin_not_saved( \WC_Product $product, string $gtin, Exception $e ) {
protected function error_gtin_not_saved( WC_Product $product, string $gtin, Exception $e ): string {
return sprintf( 'GTIN [ %s ] for Product ID: %s - %s has an error - %s', $gtin, $product->get_id(), $product->get_name(), $e->getMessage() );
}

/**
* Gets the message when the GTIN is successfully migrated.
*
* @param \WC_Product $product
* @param string $gtin
* @param WC_Product $product
* @param string $gtin
*
* @return string
*/
protected function successful_migrated_gtin( \WC_Product $product, string $gtin ) {
protected function successful_migrated_gtin( WC_Product $product, string $gtin ): string {
return sprintf( 'GTIN [ %s ] has been migrated for Product ID: %s - %s', $gtin, $product->get_id(), $product->get_name() );
}

/**
* Gets the GTIN value
*
* @param WC_Product $product The product
* @return string|null
*/
protected function get_gtin( WC_Product $product ): ?string {
/**
* Filters the value of the GTIN before performing the migration.
* This value will be he one that we copy inside the Product Inventory GTIN.
*/
return apply_filters( 'woocommerce_gla_gtin_migration_value', $this->attribute_manager->get_value( $product, 'gtin' ), $product );
}
}
13 changes: 13 additions & 0 deletions src/Integration/YoastWooCommerceSeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ function ( $sources, $attribute_id ) {
10,
2
);

add_filter(
'woocommerce_gla_gtin_migration_value',
function ( $gtin, $product ) {
if ( ! $gtin || self::VALUE_KEY === $gtin ) {
return $this->get_gtin( self::VALUE_KEY, $product );
}

return $gtin;
},
10,
2
);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Jobs/MigrateGTIN.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ protected function process_items( array $items ) {
continue;
}

$gtin = $this->attribute_manager->get_value( $product, 'gtin' );
$gtin = $this->get_gtin( $product );

if ( ! $gtin ) {
$this->debug( $this->error_gtin_not_found( $product ) );
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/WPCLIMigrationGTIN.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected function process_items( array $items ): int {
continue;
}

$gtin = $this->attribute_manager->get_value( $product, 'gtin' );
$gtin = $this->get_gtin( $product );
if ( ! $gtin ) {
$this->debug( $this->error_gtin_not_found( $product ) );
continue;
Expand Down

0 comments on commit 6155941

Please sign in to comment.