-
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
Fix incorrect product statistics count #1728
Changes from 7 commits
e5c0520
0970482
9f661dc
cac5dd0
2d9c4a3
7dfd462
3f7a2cc
6c84398
40f189a
41c52e0
39e3190
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Google\Service\ShoppingContent\Product as GoogleProduct; | ||
use WC_Product; | ||
use WC_Product_Variation; | ||
use WP_Post; | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
|
@@ -289,6 +290,17 @@ public function get_wc_product( int $product_id ): WC_Product { | |
return $this->wc->get_product( $product_id ); | ||
} | ||
|
||
/** | ||
* Get WooCommerce product by WP get_post | ||
* | ||
* @param int $product_id | ||
* | ||
* @return WP_Post | ||
*/ | ||
public function get_wc_product_by_wp_post( int $product_id ): WP_Post { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After we call this function we double check to make sure the product has a value (is not null), but the return type of this function does not allow null. Do we need a nullable return type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in 6c84398. |
||
return get_post( $product_id ); | ||
} | ||
|
||
/** | ||
* @param WC_Product $product | ||
* | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,7 +6,7 @@ | |||||
use Automattic\WooCommerce\GoogleListingsAndAds\Infrastructure\Service; | ||||||
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper; | ||||||
use Automattic\WooCommerce\GoogleListingsAndAds\Value\ChannelVisibility; | ||||||
use Automattic\WooCommerce\GoogleListingsAndAds\Value\MCStatus; | ||||||
use Automattic\WooCommerce\GoogleListingsAndAds\Value\SyncStatus; | ||||||
use WC_Product; | ||||||
|
||||||
defined( 'ABSPATH' ) || exit; | ||||||
|
@@ -81,12 +81,13 @@ public function find_ids( array $args = [], int $limit = -1, int $offset = 0 ): | |||||
* Find and return an array of WooCommerce product objects based on the provided product IDs. | ||||||
* | ||||||
* @param int[] $ids Array of WooCommerce product IDs | ||||||
* @param array $args Array of WooCommerce args (except 'return'), and product metadata. | ||||||
* @param int $limit Maximum number of results to retrieve or -1 for unlimited. | ||||||
* @param int $offset Amount to offset product results. | ||||||
* | ||||||
* @return WC_Product[] Array of WooCommerce product objects | ||||||
*/ | ||||||
public function find_by_ids( array $ids, int $limit = -1, int $offset = 0 ): array { | ||||||
public function find_by_ids( array $ids, array $args = [], int $limit = -1, int $offset = 0 ): array { | ||||||
$args['include'] = $ids; | ||||||
|
||||||
return $this->find( $args, $limit, $offset ); | ||||||
|
@@ -163,7 +164,8 @@ public function find_sync_ready_products( array $args = [], int $limit = - 1, in | |||||
* @return array | ||||||
*/ | ||||||
public function find_delete_product_ids( array $ids, int $limit = - 1, int $offset = 0 ): array { | ||||||
$results = $this->find_by_ids( $ids, $limit, $offset ); | ||||||
$args = [ 'status' => 'trash' ]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now working for items that need to be deleted because they are in the trash. But for items that get deleted from Merchant Center for other reasons are not found/updated. For example I set a product to catalog visibility = hidden, which will trigger it to be deleted from the Merchant Center, but it's status remains "published". Without any status it was previously querying the default WP statuses so it seems we should include
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching this, I've updated the issue #1785 so it will be handled in the next PR. Merging this PR first. |
||||||
$results = $this->find_by_ids( $ids, $args, $limit, $offset ); | ||||||
return $this->product_filter->filter_products_for_delete( $results )->get_product_ids(); | ||||||
} | ||||||
|
||||||
|
@@ -264,9 +266,9 @@ public function find_mc_not_synced_product_ids( int $limit = -1, int $offset = 0 | |||||
'type' => $types, | ||||||
'meta_query' => [ | ||||||
[ | ||||||
'key' => ProductMetaHandler::KEY_MC_STATUS, | ||||||
'compare' => '=', | ||||||
'value' => MCStatus::NOT_SYNCED, | ||||||
'key' => ProductMetaHandler::KEY_SYNC_STATUS, | ||||||
'compare' => '!=', | ||||||
'value' => SyncStatus::SYNCED, | ||||||
], | ||||||
], | ||||||
]; | ||||||
|
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.
By removing these lines we are reverting the PR #1238
I can see the flaws in the filter, but I don't think we should revert it completely otherwise we'll reintroduce the endless retry upon failure. I can see the logic of adding the status equals trash, but if we look at the sync hooks. It's hooked into both when a product is trashed and when it's deleted completely. In the latter case it won't be able to query the product data at all. Which means:
delete_by_batch_request
In that case maybe we should move the filtering from process_items to the code block where the retries are scheduled. If we filter the ID map at that point then we can remove any no longer existing products and ones that retried too many times.
Although I would move this to a separate issue/PR.
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.
Ah, my brain wasn't working properly yesterday, I actually forgot to add back the deleted code so even though I added "status equals trash" logic in
find_delete_product_ids
method fromProductRepository.php
, there is no one calling this method. So if I added it back like below It would try to find the product with statustrash
then exclude the ones which are not ready to delete. Note thatarray_intersect( $product_id_map, $ready_ids );
can work asarray_intersect
is working through the array values.Correct me if I'm wrong, from the WooCommerce admin products page I can only delete a
trash
product completely. Which means based on the above code, we would calldelete_by_batch_request
when a product status is becomingtrash
, and we would not calldelete_by_batch_request
again when the product is deleted completely. Is it correct?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.
A product can still be force deleted (without going to trash) either through WP-CLI, API or making use of a plugin.
In that case we have the ID mapping scheduled for removal, but we can't load any product details when the scheduled task actually starts running. So I imagine in that case the filter wouldn't work and the product would get excluded from the batch.
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.
Right, I think in this case it would be better to move this into different issue/PR so we don't block this PR. Do you think I should revert the commits 3f7a2cc and 7dfd462 then create an issue, or adding a new commit including the change in my previous comment and create an issue?
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 think fixing it temporarily by changing the status to trash (as long as we aren't reverting previous behaviour) is a good option. And then create a separate followup issue for filtering later, since that's covering behaviour that was already broken.
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.
The follow up issue is created in #1785.