-
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
API PULL - Prevent notification errors for deleted products #2356
base: develop
Are you sure you want to change the base?
API PULL - Prevent notification errors for deleted products #2356
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## feature/google-api-project #2356 +/- ##
==============================================================
- Coverage 64.2% 64.2% -0.0%
- Complexity 4484 4486 +2
==============================================================
Files 471 471
Lines 18862 18879 +17
==============================================================
+ Hits 12117 12120 +3
- Misses 6745 6759 +14
Flags with carried forward coverage won't be shown. Click here to find out more.
|
…tion-erorrs-deleted-products # Conflicts: # src/Jobs/Notifications/AbstractItemNotificationJob.php
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.
Thanks for handling the case when products/coupons no longer exist before sending notifications. I noticed that when I try to delete a product, the notification coupon.delete
would be triggered as well:
DEBUG Automattic\WooCommerce\GoogleListingsAndAds\API\WP\NotificationsService::notify Notification - Item ID: 143 - Topic: coupon.delete - Data []
src/Coupon/CouponHelper.php
Outdated
try { | ||
$coupon = $this->get_wc_coupon( $coupon_id ); | ||
} catch ( InvalidValue $e ) { | ||
return true; // Sent true for forcing delete notification as the product doesn't exist anymore. |
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.
Should be coupon
in the comment.
src/Coupon/SyncerHooks.php
Outdated
@@ -299,11 +299,17 @@ protected function handle_delete_coupon( int $coupon_id ) { | |||
protected function maybe_send_delete_notification( int $coupon_id ): void { | |||
$coupon = $this->wc->maybe_get_coupon( $coupon_id ); | |||
|
|||
if ( $coupon instanceof WC_Coupon && $this->coupon_helper->should_trigger_delete_notification( $coupon ) ) { | |||
if ( is_null( $coupon ) ) { | |||
// In case product is not anymore in DB we send the notification directly. |
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.
Should be coupon
in the comment.
@@ -299,11 +299,17 @@ protected function handle_delete_coupon( int $coupon_id ) { | |||
protected function maybe_send_delete_notification( int $coupon_id ): void { | |||
$coupon = $this->wc->maybe_get_coupon( $coupon_id ); | |||
|
|||
if ( $coupon instanceof WC_Coupon && $this->coupon_helper->should_trigger_delete_notification( $coupon ) ) { | |||
if ( is_null( $coupon ) ) { |
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.
I guess since we are also listening to actions trashed_post
or deleted_post
for triggering delete coupons function, this line would cause the coupon.delete
notification being triggered when a product is trashed.
google-listings-and-ads/src/Coupon/SyncerHooks.php
Lines 134 to 135 in 61fe02d
add_action( 'trashed_post', [ $this, 'delete_by_id' ], 90 ); | |
add_action( 'deleted_post', [ $this, 'delete_by_id' ], 90 ); |
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.
Thanks @ianlin. That is expected.
When the item is trashed is still available in the DB and it won't be null and we schedule the deletion Notification. In case the item is not anymore in DB we notify the deletion directly.
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.
Yeah, I understand in this PR it triggers the deletion notifications straightaway. But my question was a bit different: when I tried to delete a product, the coupon deletion notification would be triggered as well. It did not happen prior to this PR.
…tion-erorrs-deleted-products
Changes proposed in this Pull Request:
When we send notifications we take as granted that the product or coupon are available in DB when handling its metas. This is not always true as the Job and some intermediate functions can run after the Product or Coupon is permanently deleted.
When this happens, the notifications meta handler or some other functions will throw some exceptions.
This PR fixes that ensuring that the item is available. Introducing the next changes:
Detailed test instructions:
gla/jobs/notifications/products/process_item
job is scheduled with CREATE topic.gla/jobs/notifications/products/process_item
job is scheduled with UPDATE topic.gla/jobs/notifications/products/process_item
job is scheduled with DELETE topic.gla/jobs/notifications/products/process_item
job is scheduled with CREATE topic.wp_delete_post( {post_id}, true );
wp_delete_post( {post_id}, true );
wp_delete_post( {post_id}, true );
Additional details
I considered as well having some kind of table/options to keep the Notification metas so we don't depend anymore on the item itself. But that would require a big re-structure of the Notifications so this may be discussed for phase 2.