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

Block Shipping, Products and Coupons when Sync Push is disabled #2810

Open
wants to merge 10 commits into
base: feature/api-pull-sync-endpoint
Choose a base branch
from

Conversation

puntope
Copy link
Contributor

@puntope puntope commented Mar 4, 2025

Changes proposed in this Pull Request:

Block Merchant Center Sync in case the Sync is disabled for the datatype

Detailed test instructions:

  1. Checkout PR and have a completed onboarding. All the Sync Datatype for PUSH should be enabled
  2. Create a product. Go to WoOCommere Scheduled actions and see gla/jobs/update_products/process_item. Run it
  3. Update the product. Go to WoOCommere Scheduled actions and see gla/jobs/update_products/process_item. Run it
  4. Create a coupon and select "Show in Google" . Go to WoOCommere Scheduled actions and see gla/jobs/update_coupon/process_item. Run it
  5. Create a coupon and select "Show in Google". Go to WoOCommere Scheduled actions and see gla/jobs/update_coupon/process_item. Run it
  6. Go to Google - Shipping tab and select "Automatically sync my store’s shipping settings to Google."
  7. Go to WooCommerce - Settings - Shipping and create a Shipping Zone
  8. Go to WooCommerce - Status - Scheduled Actions and see gla/jobs/update_shipping_settings/process_item
  9. Deactivate Push Sync for Products, Coupons and Shipping
  10. Repeat step 3. No Job should be scheduled
  11. Try to sync a product manually using the Connection Test Page. It should fail. "Error submitting product to Google: Pushing products will not run if the PUSH Sync functionality has been disabled."
  12. Repeat steps 5. No Job should be scheduled
  13. Repeat step 8. No Job should be scheduled

Additional details:

PT: pcTzPl-2yG-p2

Changelog entry

@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 Mar 4, 2025
Base automatically changed from add/check-pull to feature/api-pull-sync-endpoint March 10, 2025 06:46
@puntope puntope self-assigned this Mar 16, 2025
@puntope puntope requested a review from a team March 16, 2025 16:32
@puntope puntope marked this pull request as ready for review March 16, 2025 22:00
Copy link

codecov bot commented Mar 16, 2025

Codecov Report

Attention: Patch coverage is 40.00000% with 27 lines in your changes missing coverage. Please review.

Project coverage is 67.2%. Comparing base (0652144) to head (89dff2e).
Report is 1 commits behind head on feature/api-pull-sync-endpoint.

Files with missing lines Patch % Lines
src/Product/ProductSyncer.php 7.1% 13 Missing ⚠️
src/ConnectionTest.php 0.0% 6 Missing ⚠️
src/Coupon/CouponSyncer.php 64.3% 5 Missing ⚠️
src/API/Google/Settings.php 0.0% 2 Missing ⚠️
src/Jobs/AbstractCouponSyncerJob.php 0.0% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@                        Coverage Diff                         @@
##             feature/api-pull-sync-endpoint   #2810     +/-   ##
==================================================================
- Coverage                              67.3%   67.2%   -0.1%     
- Complexity                             4705    4711      +6     
==================================================================
  Files                                   481     481             
  Lines                                 19713   19746     +33     
==================================================================
+ Hits                                  13267   13275      +8     
- Misses                                 6446    6471     +25     
Flag Coverage Δ
php-unit-tests 67.2% <40.0%> (-0.1%) ⬇️

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

Files with missing lines Coverage Δ
src/Jobs/AbstractProductSyncerBatchedJob.php 100.0% <100.0%> (ø)
src/Jobs/AbstractProductSyncerJob.php 100.0% <100.0%> (ø)
src/Jobs/UpdateShippingSettings.php 100.0% <100.0%> (ø)
src/MerchantCenter/MerchantCenterService.php 91.5% <100.0%> (-1.3%) ⬇️
src/Shipping/SyncerHooks.php 100.0% <100.0%> (ø)
src/Jobs/AbstractCouponSyncerJob.php 0.0% <0.0%> (ø)
src/API/Google/Settings.php 6.2% <0.0%> (-<0.1%) ⬇️
src/Coupon/CouponSyncer.php 69.9% <64.3%> (-2.6%) ⬇️
src/ConnectionTest.php 0.0% <0.0%> (ø)
src/Product/ProductSyncer.php 86.4% <7.1%> (-12.4%) ⬇️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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 @puntope for working on these changes.

As explained in the comments, I think we should follow some of the simple logic as is done for Shipping. As it is right now a product/coupon status will still be changed to pending sync even if push is disabled, we should not allow any changes if it's disabled.

Comment on lines +146 to +150
public function is_enabled_for_datatype( string $data_type ): bool {
/** @var NotificationsService $notifications_service */
$notifications_service = $this->container->get( NotificationsService::class );
return $notifications_service->is_push_enabled_for_datatype( $data_type );
}
Copy link
Contributor

Choose a reason for hiding this comment

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

So two confusing things about this function:

  1. If it's actually just a wrapper to call is_push_enabled_for_datatype then why doesn't the name also reflect that it's checking if push is enabled?
  2. Push as nothing to do with notifications, so why is it calling a notification service?

Reasoning why I think we shouldn't call the notification service is because we should think of push and pull as two completely independent parts, which can both run together (dark launch), or toggled between the two. We could also decide to later remove one. So making push logic dependent on notifications (which is pull only) doesn't separate the two. Should we have a separate service handling the overlapping logic between the two?

$this->job_repository->get( UpdateShippingSettings::class )->schedule();
if ( $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_SHIPPING ) ) {
$this->job_repository->get( UpdateShippingSettings::class )->schedule();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

So the changes here seem pretty straightforward with simple logic:

  • If pull is enabled: send a notification
  • If push is enabled: schedule a job to start the sync

But when I look at other SyncerHooks I don't see that same clear separation. For example for products I can see that if push is disabled, it will still perform the following steps for a single product (or a batch of products in case it's a variable product):

  1. Check if the product is sync ready
  2. Mark the product as pending sync
  3. If not sync ready prepare delete requests
  4. Schedule the products to be updated
  5. Schedule the products to be deleted

Now because can_scheduler will check if push is enabled, step 4 and 5 won't actually schedule the job in action scheduler. But in step 2 we already modified the product.

So I think we should apply the same simple logic like we did for the shipping syncerhooks, and not even attempt to do anything if push is not enabled.

Note

Same logic applies for coupons.

@@ -62,6 +63,6 @@ public function __construct(
* @return bool Returns true if the job can be scheduled.
*/
public function can_schedule( $args = [] ): bool {
return ! $this->is_running( $args ) && $this->merchant_center->should_push();
return ! $this->is_running( $args ) && $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_PRODUCT );
Copy link
Contributor

Choose a reason for hiding this comment

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

As I outlined in the previous comment, we should prevent pushing at an earlier stage. I can however understand we want some logic checking within the syncer jobs, but shouldn't that be for when a job item is being processed.

Here we are only blocking a job from being scheduled. But what happens if we perform the actions in this order:

  1. Schedule a product update
  2. Disable product push
  3. Scheduled actions runs (at this step I'd expect it to bail as push is no longer enabled)

woocommerce_gla_batch_retry_update_products will also be happily triggered again without being blocked if the previous sync failed.

// Confirm that the Merchant Center account is connected and the user has chosen for the shipping rates to be synced from WooCommerce settings.
return $this->merchant_center->is_connected() && $this->google_settings->should_get_shipping_rates_from_woocommerce();
// Confirm that the Merchant Center account is connected, the user has chosen for the shipping rates to be synced from WooCommerce settings and the Push Sync is enabled for Shipping.
return $this->merchant_center->is_connected() && $this->google_settings->should_get_shipping_rates_from_woocommerce() && $this->merchant_center->is_enabled_for_datatype( NotificationsService::DATATYPE_SHIPPING );
Copy link
Contributor

Choose a reason for hiding this comment

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

So here we are making the change in the right location. Because can_sync_shipping will be checked when scheduling the job but also when processing the items of the job. So if a job was already scheduled and the push disabled later it will still prevent the job from being run.

At most we might want to tweak the failure message to indicate it could also be because push is disabled:

Cannot sync shipping settings. Confirm that the merchant center account is connected and the option to automatically sync the shipping settings is selected.

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