tion ( $unique_key, $issue ) {
$issue['applicable_countries'] = json_encode( $this->product_issue_countries[ $unique_key ] );
return $issue;
},
array_keys( $product_issues ),
$product_issues
);
/** @var MerchantIssueQuery $issue_query */
$issue_query = $this->container->get( MerchantIssueQuery::class );
$issue_query->update_or_insert( array_values( $product_issues ) );
}
/**
* Include local presync product validation issues in the merchant issues table.
*/
protected function refresh_presync_product_issues(): void {
/** @var MerchantIssueQuery $issue_query */
$issue_query = $this->container->get( MerchantIssueQuery::class );
$created_at = $this->cache_created_time->format( 'Y-m-d H:i:s' );
$issue_action = __( 'Update this attribute in your product data', 'google-listings-and-ads' );
/** @var ProductMetaQueryHelper $product_meta_query_helper */
$product_meta_query_helper = $this->container->get( ProductMetaQueryHelper::class );
// Get all MC statuses.
$all_errors = $product_meta_query_helper->get_all_values( ProductMetaHandler::KEY_ERRORS );
$chunk_size = apply_filters( 'woocommerce_gla_merchant_status_presync_issues_chunk', 500 );
$product_issues = [];
foreach ( $all_errors as $product_id => $presync_errors ) {
// Don't create issues with empty descriptions
// or for variable parents (they contain issues of all children).
$error = $presync_errors[ array_key_first( $presync_errors ) ];
if ( empty( $error ) || ! is_string( $error ) ) {
continue;
}
$product = get_post( $product_id );
// Don't store pre-sync errors for unpublished (draft, trashed) products.
if ( 'publish' !== get_post_status( $product ) ) {
continue;
}
foreach ( $presync_errors as $text ) {
$issue_parts = $this->parse_presync_issue_text( $text );
$product_issues[] = [
'product' => $product->post_title,
'product_id' => $product_id,
'code' => $issue_parts['code'],
'severity' => self::SEVERITY_ERROR,
'issue' => $issue_parts['issue'],
'action' => $issue_action,
'action_url' => 'https://support.google.com/merchants/answer/10538362?hl=en&ref_topic=6098333',
'applicable_countries' => '["all"]',
'source' => 'pre-sync',
'created_at' => $created_at,
];
}
// Do update-or-insert in chunks.
if ( count( $product_issues ) >= $chunk_size ) {
$issue_query->update_or_insert( $product_issues );
$product_issues = [];
}
}
// Handle any leftover issues.
$issue_query->update_or_insert( $product_issues );
}
/**
* Process product status statistics.
*
* @param array $product_view_statuses Product View statuses.
* @see MerchantReport::get_product_view_report
*
* @throws NotFoundExceptionInterface If the class is not found in the container.
* @throws ContainerExceptionInterface If the container throws an exception.
*/
public function process_product_statuses( array $product_view_statuses ): void {
$this->mc_statuses = [];
$product_repository = $this->container->get( ProductRepository::class );
$this->product_data_lookup = $product_repository->find_by_ids_as_associative_array( array_column( $product_view_statuses, 'product_id' ) );
$this->product_statuses = [
'products' => [],
'parents' => [],
];
foreach ( $product_view_statuses as $product_status ) {
$wc_product_id = $product_status['product_id'];
$mc_product_status = $product_status['status'];
$wc_product = $this->product_data_lookup[ $wc_product_id ] ?? null;
if ( ! $wc_product || ! $wc_product_id ) {
// Skip if the product does not exist in WooCommerce.
do_action(
'woocommerce_gla_debug_message',
sprintf( 'Merchant Center product %s not found in this WooCommerce store.', $wc_product_id ),
__METHOD__,
);
continue;
}
if ( $this->product_is_expiring( $product_status['expiration_date'] ) ) {
$mc_product_status = MCStatus::EXPIRING;
}
// Products is used later for global product status statistics.
$this->product_statuses['products'][ $wc_product_id ][ $mc_product_status ] = 1 + ( $this->product_statuses['products'][ $wc_product_id ][ $mc_product_status ] ?? 0 );
// Aggregate parent statuses for mc_status postmeta.
$wc_parent_id = $wc_product->get_parent_id();
if ( ! $wc_parent_id ) {
continue;
}
$this->product_statuses['parents'][ $wc_parent_id ][ $mc_product_status ] = 1 + ( $this->product_statuses['parents'][ $wc_parent_id ][ $mc_product_status ] ?? 0 );
}
$parent_keys = array_values( array_keys( $this->product_statuses['parents'] ) );
$parent_products = $product_repository->find_by_ids_as_associative_array( $parent_keys );
$this->product_data_lookup = $this->product_data_lookup + $parent_products;
// Update each product's mc_status and then update the global statistics.
$this->update_products_meta_with_mc_status();
$this->update_intermediate_product_statistics();
$product_issues = $this->get_product_issues( $product_view_statuses );
$this->refresh_product_issues( $product_issues );
}
/**
* Whether a product is expiring.
*
* @param DateTime $expiration_date
*
* @return bool Whether the product is expiring.
*/
protected function product_is_expiring( DateTime $expiration_date ): bool {
if ( ! $expiration_date ) {
return false;
}
// Products are considered expiring if they will expire within 3 days.
return time() + 3 * DAY_IN_SECONDS > $expiration_date->getTimestamp();
}
/**
* Sum and update the intermediate product status statistics. It will group
* the variations for the same parent.
*
* For the case that one variation is approved and the other disapproved:
* 1. Give each status a priority.
* 2. Store the last highest priority status in `$parent_statuses`.
* 3. Compare if a higher priority status is found for that variable product.
* 4. Loop through the `$parent_statuses` array at the end to add the final status counts.
*
* @return array Product status statistics.
*/
protected function update_intermediate_product_statistics(): array {
$product_statistics = self::DEFAULT_PRODUCT_STATS;
// If the option is set, use it to sum the total quantity.
$product_statistics_intermediate_data = $this->options->get( OptionsInterface::PRODUCT_STATUSES_COUNT_INTERMEDIATE_DATA );
if ( $product_statistics_intermediate_data ) {
$product_statistics = $product_statistics_intermediate_data;
$this->initial_intermediate_data = $product_statistics;
}
$product_statistics_priority = [
MCStatus::APPROVED => 6,
MCStatus::PARTIALLY_APPROVED => 5,
MCStatus::EXPIRING => 4,
MCStatus::PENDING => 3,
MCStatus::DISAPPROVED => 2,
MCStatus::NOT_SYNCED => 1,
];
$parent_statuses = [];
foreach ( $this->product_statuses['products'] as $product_id => $statuses ) {
foreach ( $statuses as $status => $num_products ) {
$product = $this->product_data_lookup[ $product_id ] ?? null;
if ( ! $product ) {
continue;
}
$parent_id = $product->get_parent_id();
if ( ! $parent_id ) {
$product_statistics[ $status ] += $num_products;
} elseif ( ! isset( $parent_statuses[ $parent_id ] ) ) {
$parent_statuses[ $parent_id ] = $status;
} else {
$current_parent_status = $parent_statuses[ $parent_id ];
if ( $product_statistics_priority[ $status ] < $product_statistics_priority[ $current_parent_status ] ) {
$parent_statuses[ $parent_id ] = $status;
}
}
}
}
foreach ( $parent_statuses as $parent_id => $new_parent_status ) {
$current_parent_intermediate_data_status = $product_statistics_intermediate_data['parents'][ $parent_id ] ?? null;
if ( $current_parent_intermediate_data_status === $new_parent_status ) {
continue;
}
if ( ! $current_parent_intermediate_data_status ) {
$product_statistics[ $new_parent_status ] += 1;
$product_statistics['parents'][ $parent_id ] = $new_parent_status;
continue;
}
// Check if the new parent status has higher priority than the previous one.
if ( $product_statistics_priority[ $new_parent_status ] < $product_statistics_priority[ $current_parent_intermediate_data_status ] ) {
$product_statistics[ $current_parent_intermediate_data_status ] -= 1;
$product_statistics[ $new_parent_status ] += 1;
$product_statistics['parents'][ $parent_id ] = $new_parent_status;
} else {
$product_statistics['parents'][ $parent_id ] = $current_parent_intermediate_data_status;
}
}
$this->options->update( OptionsInterface::PRODUCT_STATUSES_COUNT_INTERMEDIATE_DATA, $product_statistics );
return $product_statistics;
}
/**
* Calculate the total count of products in the MC using the statistics.
*
* @since 2.6.4
*
* @param array $statistics
*
* @return int
*/
protected function calculate_total_synced_product_statistics( array $statistics ): int {
if ( ! count( $statistics ) ) {
return 0;
}
$synced_status_values = array_values( array_diff( $statistics, [ $statistics[ MCStatus::NOT_SYNCED ] ] ) );
return array_sum( $synced_status_values );
}
/**
* Handle the failure of the Merchant Center statuses fetching.
*
* @since 2.6.4
*
* @param string $error_message The error message.
*
* @throws NotFoundExceptionInterface If the class is not found in the container.
* @throws ContainerExceptionInterface If the container throws an exception.
*/
public function handle_failed_mc_statuses_fetching( string $error_message = '' ): void {
// Reset the intermediate data to the initial state when starting the job.
$this->options->update( OptionsInterface::PRODUCT_STATUSES_COUNT_INTERMEDIATE_DATA, $this->initial_intermediate_data );
// Let's remove any issue created during the failed fetch.
$this->container->get( MerchantIssueTable::class )->delete_specific_product_issues( array_keys( $this->product_data_lookup ) );
$mc_statuses = [
'timestamp' => $this->cache_created_time->getTimestamp(),
'statistics' => null,
'loading' => false,
'error' => $error_message,
];
$this->container->get( TransientsInterface::class )->set(
Transients::MC_STATUSES,
$mc_statuses,
$this->get_status_lifetime()
);
}
/**
* Handle the completion of the Merchant Center statuses fetching.
*
* @since 2.6.4
*/
public function handle_complete_mc_statuses_fetching() {
$intermediate_data = $this->options->get( OptionsInterface::PRODUCT_STATUSES_COUNT_INTERMEDIATE_DATA, self::DEFAULT_PRODUCT_STATS );
unset( $intermediate_data['parents'] );
$total_synced_products = $this->calculate_total_synced_product_statistics( $intermediate_data );
/** @var ProductRepository $product_repository */
$product_repository = $this->container->get( ProductRepository::class );
$intermediate_data[ MCStatus::NOT_SYNCED ] = count(
$product_repository->find_all_product_ids()
) - $total_synced_products;
$mc_statuses = [
'timestamp' => $this->cache_created_time->getTimestamp(),
'statistics' => $intermediate_data,
'loading' => false,
'error' => null,
];
$this->container->get( TransientsInterface::class )->set(
Transients::MC_STATUSES,
$mc_statuses,
$this->get_status_lifetime()
);
$this->delete_product_statuses_count_intermediate_data();
}
/**
* Update the Merchant Center status for each product.
*/
protected function update_products_meta_with_mc_status() {
// Generate a product_id=>mc_status array.
$new_product_statuses = [];
foreach ( $this->product_statuses as $types ) {
foreach ( $types as $product_id => $statuses ) {
if ( isset( $statuses[ MCStatus::PENDING ] ) ) {
$new_product_statuses[ $product_id ] = MCStatus::PENDING;
} elseif ( isset( $statuses[ MCStatus::EXPIRING ] ) ) {
$new_product_statuses[ $product_id ] = MCStatus::EXPIRING;
} elseif ( isset( $statuses[ MCStatus::APPROVED ] ) ) {
if ( count( $statuses ) > 1 ) {
$new_product_statuses[ $product_id ] = MCStatus::PARTIALLY_APPROVED;
} else {
$new_product_statuses[ $product_id ] = MCStatus::APPROVED;
}
} else {
$new_product_statuses[ $product_id ] = array_key_first( $statuses );
}
}
}
foreach ( $new_product_statuses as $product_id => $new_status ) {
$product = $this->product_data_lookup[ $product_id ] ?? null;
// At this point, the product should exist in WooCommerce but in the case that product is not found, log it.
if ( ! $product ) {
do_action(
'woocommerce_gla_debug_message',
sprintf( 'Merchant Center product with WooCommerce ID %d is not found in this store.', $product_id ),
__METHOD__,
);
continue;
}
$product->add_meta_data( $this->prefix_meta_key( ProductMetaHandler::KEY_MC_STATUS ), $new_status, true );
// We use save_meta_data so we don't trigger the woocommerce_update_product hook and the Syncer Hooks.
$product->save_meta_data();
}
}
/**
* Allows a hook to modify the lifetime of the statuses data.
*
* @return int
*/
protected function get_status_lifetime(): int {
return apply_filters( 'woocommerce_gla_mc_status_lifetime', self::STATUS_LIFETIME );
}
/**
* Valid issues types for issue type filter.
*
* @return string[]
*/
protected function get_valid_issue_types(): array {
return [
self::TYPE_ACCOUNT,
self::TYPE_PRODUCT,
];
}
/**
* Parse the code and formatted issue text out of the presync validation error text.
*
* Converts the error strings:
* "[attribute] Error message." > "Error message [attribute]"
*
* Note:
* If attribute is an array the name can be "[attribute[0]]".
* So we need to match the additional set of square brackets.
*
* @param string $text
*
* @return string[] With indexes `code` and `issue`
*/
protected function parse_presync_issue_text( string $text ): array {
$matches = [];
preg_match( '/^\[([^\]]+\]?)\]\s*(.+)$/', $text, $matches );
if ( count( $matches ) !== 3 ) {
return [
'code' => 'presync_error_attrib_' . md5( $text ),
'issue' => $text,
];
}
// Convert attribute name "imageLink" to "image".
if ( 'imageLink' === $matches[1] ) {
$matches[1] = 'image';
}
// Convert attribute name "additionalImageLinks[]" to "galleryImage".
if ( str_starts_with( $matches[1], 'additionalImageLinks' ) ) {
$matches[1] = 'galleryImage';
}
$matches[2] = trim( $matches[2], ' .' );
return [
'code' => 'presync_error_' . $matches[1],
'issue' => "{$matches[2]} [{$matches[1]}]",
];
}
/**
* Return a standardized Merchant Issue severity value.
*
* @param array $row
*
* @return string
*/
protected function get_issue_severity( array $row ): string {
$is_warning = in_array(
$row['severity'],
[
'warning',
'suggestion',
'demoted',
'unaffected',
],
true
);
return $is_warning ? self::SEVERITY_WARNING : self::SEVERITY_ERROR;
}
/**
* In very rare instances, issue values need to be overridden manually.
*
* @param array $issue
*
* @return array The original issue with any possibly overridden values.
*/
private function maybe_override_issue_values( array $issue ): array {
/**
* Code 'merchant_quality_low' for matching the original issue.
* Ref: https://developers.google.com/shopping-content/guides/account-issues#merchant_quality_low
*
* Issue string "Account isn't eligible for free listings" for matching
* the updated copy after Free and Enhanced Listings merge.
*
* TODO: Remove the condition of matching the $issue['issue']
* if its issue code is the same as 'merchant_quality_low'
* after Google replaces the issue title on their side.
*/
if ( 'merchant_quality_low' === $issue['code'] || "Account isn't eligible for free listings" === $issue['issue'] ) {
$issue['issue'] = 'Show products on additional surfaces across Google through free listings';
$issue['severity'] = self::SEVERITY_WARNING;
$issue['action_url'] = 'https://support.google.com/merchants/answer/9199328?hl=en';
}
/**
* Reference: https://github.com/woocommerce/google-listings-and-ads/issues/1688
*/
if ( 'home_page_issue' === $issue['code'] ) {
$issue['issue'] = 'Website claim is lost, need to re verify and claim your website. Please reference the support link';
$issue['action_url'] = 'https://woocommerce.com/document/google-for-woocommerce/faq/#reverify-website';
}
return $issue;
}
/**
* Getter for get_cache_created_time
*
* @return DateTime The DateTime stored in cache_created_time
*/
public function get_cache_created_time(): DateTime {
return $this->cache_created_time;
}
}