Skip to content

Commit

Permalink
Group variations with same parent for synced products of statistics API
Browse files Browse the repository at this point in the history
  • Loading branch information
ianlin committed Oct 4, 2022
1 parent 69f14cd commit 4c01002
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/MerchantCenter/MerchantStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,15 @@ protected function get_synced_google_ids(): array {
/** @var ProductMetaQueryHelper $product_meta_query_helper */
$product_meta_query_helper = $this->container->get( ProductMetaQueryHelper::class );

// Get only synced simple and variations
// Get only synced simple and variations, and group variation products if they have the same parent
/** @var ProductHelper $product_helper */
$product_helper = $this->container->get( ProductHelper::class );
$args['type'] = array_diff( ProductSyncer::get_supported_product_types(), [ 'variable' ] );
$filtered_product_ids = array_flip( $product_repository->find_synced_product_ids( $args ) );
$filtered_product_ids = array_flip(
$product_helper->maybe_swap_for_parent_ids(
$product_repository->find_synced_product_ids( $args )
)
);
$all_google_ids = $product_meta_query_helper->get_all_values( ProductMetaHandler::KEY_GOOGLE_IDS );
$filtered_google_ids = [];
foreach ( array_intersect_key( $all_google_ids, $filtered_product_ids ) as $product_ids ) {
Expand Down
16 changes: 11 additions & 5 deletions tests/Unit/MerchantCenter/MerchantStatusesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantCenterService;
use Automattic\WooCommerce\GoogleListingsAndAds\MerchantCenter\MerchantStatuses;
use Automattic\WooCommerce\GoogleListingsAndAds\Options\TransientsInterface;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductHelper;
use Automattic\WooCommerce\GoogleListingsAndAds\Product\ProductRepository;
use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\UnitTest;
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\League\Container\Container;
Expand All @@ -18,12 +19,13 @@
defined( 'ABSPATH' ) || exit;

/**
* @property Merchant $merchant
* @property MerchantIssueQuery $merchant_issue_query
* @property MerchantCenterService $merchant_center_service
* @property Merchant $merchant
* @property MerchantIssueQuery $merchant_issue_query
* @property MerchantCenterService $merchant_center_service
* @property ShoppingContent\AccountStatus $account_status
* @property ProductMetaQueryHelper $product_meta_query_helper
* @property MerchantStatuses $merchant_statuses
* @property ProductMetaQueryHelper $product_meta_query_helper
* @property MerchantStatuses $merchant_statuses
* @property ProductHelper $product_helper
* @package Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\MerchantCenter
* @group MerchantCenterStatuses
*/
Expand All @@ -35,6 +37,7 @@ class MerchantStatusesTest extends UnitTest {
private $account_status;
private $product_meta_query_helper;
private $merchant_statuses;
private $product_helper;

/**
* Runs before each test is executed.
Expand All @@ -46,6 +49,7 @@ public function setUp(): void {
$this->merchant_center_service = $this->createMock( MerchantCenterService::class );
$this->account_status = $this->createMock( ShoppingContent\AccountStatus::class );
$this->product_meta_query_helper = $this->createMock( ProductMetaQueryHelper::class );
$this->product_helper = $this->createMock( ProductHelper::class );

$product_repository = $this->createMock( ProductRepository::class );
$transients = $this->createMock( TransientsInterface::class );
Expand All @@ -59,13 +63,15 @@ public function setUp(): void {
$container->share( ProductRepository::class, $product_repository );
$container->share( ProductMetaQueryHelper::class, $this->product_meta_query_helper );
$container->share( MerchantIssueTable::class, $merchant_issue_table );
$container->share( ProductHelper::class, $this->product_helper );

$this->merchant_statuses = new MerchantStatuses();
$this->merchant_statuses->set_container( $container );
}

public function test_refresh_account_issues() {
$this->product_meta_query_helper->expects( $this->any() )->method( 'get_all_values' )->willReturn( [] );
$this->product_helper->expects( $this->once() )->method( 'maybe_swap_for_parent_ids' )->willReturn( [] );

$this->account_status->expects( $this->any() )
->method( 'getAccountLevelIssues' )
Expand Down

0 comments on commit 4c01002

Please sign in to comment.