-
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 GTIN migration JOB #2645
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a635f8d
Add GTIN Migration Job
puntope ae745ab
Merge branch 'add/support-core-gtin-field' into add/gtin-migration
puntope 7d7b0a0
PHPCS
puntope d39b358
Get all children instead of just available variations
puntope 02e6224
Merge branch 'add/support-core-gtin-field' into add/gtin-migration
puntope 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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
declare( strict_types=1 ); | ||
|
||
namespace Automattic\WooCommerce\GoogleListingsAndAds\Jobs; | ||
|
||
use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionSchedulerInterface; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareInterface; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Options\OptionsAwareTrait; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Product\Attributes\AttributeManager; | ||
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductRepository; | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* Class MigrateGTIN | ||
* | ||
* Schedules GTIN migration for all the products in the store. | ||
* | ||
* @package Automattic\WooCommerce\GoogleListingsAndAds\Jobs | ||
* @since x.x.x | ||
*/ | ||
class MigrateGTIN extends AbstractBatchedActionSchedulerJob implements OptionsAwareInterface { | ||
use OptionsAwareTrait; | ||
|
||
/** | ||
* @var ProductRepository | ||
*/ | ||
protected $product_repository; | ||
|
||
|
||
/** | ||
* @var AttributeManager | ||
*/ | ||
protected $attribute_manager; | ||
|
||
|
||
/** | ||
* Job constructor. | ||
* | ||
* @param ActionSchedulerInterface $action_scheduler | ||
* @param ActionSchedulerJobMonitor $monitor | ||
* @param ProductRepository $product_repository | ||
* @param AttributeManager $attribute_manager | ||
*/ | ||
public function __construct( ActionSchedulerInterface $action_scheduler, ActionSchedulerJobMonitor $monitor, ProductRepository $product_repository, AttributeManager $attribute_manager ) { | ||
parent::__construct( $action_scheduler, $monitor ); | ||
$this->product_repository = $product_repository; | ||
$this->attribute_manager = $attribute_manager; | ||
} | ||
|
||
/** | ||
* Get the name of the job. | ||
* | ||
* @return string | ||
*/ | ||
public function get_name(): string { | ||
return 'migrate_gtin'; | ||
} | ||
|
||
/** | ||
* Can the job be scheduled. | ||
* | ||
* @param array|null $args | ||
* | ||
* @return bool Returns true if the job can be scheduled. | ||
*/ | ||
public function can_schedule( $args = [] ): bool { | ||
return parent::can_schedule( $args ) && $this->is_gtin_available_in_core(); | ||
} | ||
|
||
/** | ||
* Process batch items. | ||
* | ||
* @param int[] $items A single batch of WooCommerce product IDs from the get_batch() method. | ||
*/ | ||
protected function process_items( array $items ) { | ||
// todo: prevent execution if migration was completed? | ||
|
||
// update the product core GTIN using G4W GTIN | ||
$products = $this->product_repository->find_by_ids( $items ); | ||
foreach ( $products as $product ) { | ||
// void if core GTIN is already set. | ||
if ( $product->get_global_unique_id() ) { | ||
continue; | ||
} | ||
|
||
// process variations | ||
if ( $product instanceof \WC_Product_Variable ) { | ||
$variations = $product->get_children(); | ||
$this->process_items( $variations ); | ||
continue; | ||
} | ||
|
||
$gtin = $this->attribute_manager->get_value( $product, 'gtin' ); | ||
if ( $gtin ) { | ||
$product->set_global_unique_id( $gtin ); | ||
$product->save(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Called when the job is completed. | ||
* | ||
* @param int $final_batch_number The final batch number when the job was completed. | ||
* If equal to 1 then no items were processed by the job. | ||
*/ | ||
protected function handle_complete( int $final_batch_number ) { | ||
// todo: set migration as completed? | ||
} | ||
|
||
/** | ||
* Get a single batch of items. | ||
* | ||
* If no items are returned the job will stop. | ||
* | ||
* @param int $batch_number The batch number increments for each new batch in the job cycle. | ||
* | ||
* @return array | ||
* | ||
* @throws Exception If an error occurs. The exception will be logged by ActionScheduler. | ||
*/ | ||
protected function get_batch( int $batch_number ): array { | ||
return $this->product_repository->find_all_product_ids( $this->get_batch_size(), $this->get_query_offset( $batch_number ) ); | ||
} | ||
|
||
/** | ||
* Verifies if GTIN logic is available in core. | ||
* | ||
* @return bool | ||
*/ | ||
protected function is_gtin_available_in_core(): bool { | ||
return method_exists( \WC_Product::class, 'get_global_unique_id' ); | ||
} | ||
} |
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.
❓ Is this something you are planning to add in the follow-up PRs, or was it missed?
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.
Yes. This is a todo and will be done in a further PR when setting the Banner for the merchant to run the job