Skip to content

Commit

Permalink
Merge pull request #2277 from woocommerce/add/refactor-product-issues
Browse files Browse the repository at this point in the history
Refactor product issues
  • Loading branch information
jorgemd24 committed Mar 5, 2024
2 parents d13c57b + c06f4e9 commit f1ab4e3
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 133 deletions.
1 change: 1 addition & 0 deletions src/API/Google/MerchantReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function get_product_view_report( $next_page_token = null ): array {
}

$product_view_data['statuses'][ $wc_product_id ] = [
'mc_id' => $product_view->getId(),
'product_id' => $wc_product_id,
'status' => $mc_product_status,
'expiration_date' => $this->convert_shopping_content_date( $product_view->getExpirationDate() ),
Expand Down
13 changes: 12 additions & 1 deletion src/DB/Table/MerchantIssueTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,22 @@ public static function get_raw_name(): string {
*
* @param DateTime $created_before Delete all records created before this.
*/
public function delete_stale( DateTime $created_before ) {
public function delete_stale( DateTime $created_before ): void {
$query = "DELETE FROM `{$this->get_sql_safe_name()}` WHERE `created_at` < '%s'";
$this->wpdb->query( $this->wpdb->prepare( $query, $created_before->format( 'Y-m-d H:i:s' ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL
}

/**
* Delete product issues for specific products and source.
*
* @param array $products_ids Array of product IDs to delete issues for.
* @param string $source The source of the issues. Default is 'mc'.
*/
public function delete_specific_product_issues( array $products_ids, string $source = 'mc' ): void {
$placeholder = '(' . implode( ',', array_fill( 0, count( $products_ids ), '%d' ) ) . ')';
$this->wpdb->query( $this->wpdb->prepare( "DELETE FROM `{$this->get_sql_safe_name()}` WHERE `product_id` IN {$placeholder} AND `source` = %s", array_merge( $products_ids, [ $source ] ) ) ); // phpcs:ignore WordPress.DB.PreparedSQL
}

/**
* Get the columns for the table.
*
Expand Down
3 changes: 1 addition & 2 deletions src/Jobs/UpdateMerchantProductStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public function process_items( array $items ) {

// Clear the cache if we're starting from the beginning.
if ( ! $next_page_token ) {
$this->merchant_statuses->clear_cache();
$this->merchant_statuses->delete_product_statuses_count_intermediate_data();
$this->merchant_statuses->clear_product_statuses_cache_and_issues();
}

$results = $this->merchant_report->get_product_view_report( $next_page_token );
Expand Down
Loading

0 comments on commit f1ab4e3

Please sign in to comment.