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 GTIN migration JOB #2645

Merged
merged 5 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\CleanupProductsJob;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\DeleteAllProducts;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\MigrateGTIN;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\UpdateAllProducts;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\UpdateProducts;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\AccountService;
Expand Down Expand Up @@ -645,6 +646,18 @@
<input name="page" value="connection-test-admin-page" type="hidden" />
<input name="action" value="wcs-cleanup-products" type="hidden" />
</form>
<form action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" method="GET">

Check warning on line 649 in src/ConnectionTest.php

View check run for this annotation

Codecov / codecov/patch

src/ConnectionTest.php#L649

Added line #L649 was not covered by tests
<table class="form-table" role="presentation">
<tr>
<th><label>GTIN Migration:</label></th>
<td>
<p>
<a class="button" href="<?php echo esc_url( wp_nonce_url( add_query_arg( [ 'action' => 'migrate-gtin' ], $url ), 'migrate-gtin' ) ); ?>">Start GTIN Migration</a>

Check warning on line 655 in src/ConnectionTest.php

View check run for this annotation

Codecov / codecov/patch

src/ConnectionTest.php#L655

Added line #L655 was not covered by tests
</p>
</td>
</tr>
</table>
</form>
<?php } ?>

<hr />
Expand Down Expand Up @@ -1322,6 +1335,14 @@
}
}

if ( 'migrate-gtin' === $_GET['action'] && check_admin_referer( 'migrate-gtin' ) ) {

Check warning on line 1338 in src/ConnectionTest.php

View check run for this annotation

Codecov / codecov/patch

src/ConnectionTest.php#L1338

Added line #L1338 was not covered by tests
/** @var MigrateGTIN $job */
$job = $this->container->get( MigrateGTIN::class );
$job->schedule();
$this->response = 'Successfully scheduled a job to migrate GTIN';

Check warning on line 1342 in src/ConnectionTest.php

View check run for this annotation

Codecov / codecov/patch

src/ConnectionTest.php#L1340-L1342

Added lines #L1340 - L1342 were not covered by tests
}


}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Internal/DependencyManagement/JobServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\JobInitializer;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\JobInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\JobRepository;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\MigrateGTIN;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\CouponNotificationJob;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\ProductNotificationJob;
use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\SettingsNotificationJob;
Expand Down Expand Up @@ -127,6 +128,13 @@
CouponHelper::class
);

// share GTIN migration job
$this->share_action_scheduler_job(
MigrateGTIN::class,
ProductRepository::class,
Product\Attributes\AttributeManager::class
);

Check warning on line 136 in src/Internal/DependencyManagement/JobServiceProvider.php

View check run for this annotation

Codecov / codecov/patch

src/Internal/DependencyManagement/JobServiceProvider.php#L132-L136

Added lines #L132 - L136 were not covered by tests

$this->share_with_tags(
JobRepository::class,
JobInterface::class
Expand Down
135 changes: 135 additions & 0 deletions src/Jobs/MigrateGTIN.php
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;

Check warning on line 48 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L45-L48

Added lines #L45 - L48 were not covered by tests
}

/**
* Get the name of the job.
*
* @return string
*/
public function get_name(): string {
return 'migrate_gtin';

Check warning on line 57 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L56-L57

Added lines #L56 - L57 were not covered by tests
}

/**
* 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();

Check warning on line 68 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L67-L68

Added lines #L67 - L68 were not covered by tests
}

/**
* Process batch items.
*
* @param int[] $items A single batch of WooCommerce product IDs from the get_batch() method.
*/
protected function process_items( array $items ) {

Check warning on line 76 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L76

Added line #L76 was not covered by tests
// todo: prevent execution if migration was completed?
Copy link
Member

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?

Copy link
Contributor Author

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


// update the product core GTIN using G4W GTIN
$products = $this->product_repository->find_by_ids( $items );
foreach ( $products as $product ) {

Check warning on line 81 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L80-L81

Added lines #L80 - L81 were not covered by tests
// void if core GTIN is already set.
if ( $product->get_global_unique_id() ) {
continue;

Check warning on line 84 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L83-L84

Added lines #L83 - L84 were not covered by tests
}

// process variations
if ( $product instanceof \WC_Product_Variable ) {
$variations = $product->get_children();
$this->process_items( $variations );
continue;

Check warning on line 91 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L88-L91

Added lines #L88 - L91 were not covered by tests
}

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

Check warning on line 97 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L94-L97

Added lines #L94 - L97 were not covered by tests
}
}
}

/**
* 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 ) {

Check warning on line 108 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L108

Added line #L108 was not covered by tests
// todo: set migration as completed?
}

Check warning on line 110 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L110

Added line #L110 was not covered by tests

/**
* 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 ) );

Check warning on line 124 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L123-L124

Added lines #L123 - L124 were not covered by tests
}

/**
* 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' );

Check warning on line 133 in src/Jobs/MigrateGTIN.php

View check run for this annotation

Codecov / codecov/patch

src/Jobs/MigrateGTIN.php#L132-L133

Added lines #L132 - L133 were not covered by tests
}
}