Skip to content
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

Allow mapping of a Google ID without a prefix #2773

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Product/ProductHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,11 @@ public function get_wc_product_id( string $mc_product_id ): int {
$mc_product_id_tokens = explode( ':', $mc_product_id );
$mc_product_id = end( $mc_product_id_tokens );

// Support a fully numeric ID both with and without the `gla_` prefix.
$wc_product_id = 0;
$pattern = '/' . preg_quote( $this->get_slug(), '/' ) . '_(\d+)$/';
$pattern = '/^(' . preg_quote( $this->get_slug(), '/' ) . '_)?(\d+)$/';
if ( preg_match( $pattern, $mc_product_id, $matches ) ) {
$wc_product_id = (int) $matches[1];
$wc_product_id = (int) $matches[2];
}

/**
Expand Down
43 changes: 43 additions & 0 deletions tests/Unit/Product/ProductHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,57 @@ public function test_get_synced_google_product_ids( WC_Product $product ) {
$this->assertEquals( [ 'US' => 'online:en:US:gla_1' ], $this->product_helper->get_synced_google_product_ids( $product ) );
}

/**
* Test a legitimate Google product ID both with and without namespacing.
*
* @return void
*/
public function test_get_wc_product_id() {
$google_id = 'online:en:US:gla_1234567';
$product_id = $this->product_helper->get_wc_product_id( $google_id );

$this->assertEquals( 1234567, $product_id );

$google_id = 'gla_4567890';
$product_id = $this->product_helper->get_wc_product_id( $google_id );

$this->assertEquals( 4567890, $product_id );
}

/**
* Confirm we support converting a product ID without the `gla_` prefix.
*/
public function test_get_wc_product_id_without_prefix() {
$google_id = 'online:en:US:1234567';
$product_id = $this->product_helper->get_wc_product_id( $google_id );

$this->assertEquals( 1234567, $product_id );
}

/**
* If we don't add the prefix confirm it only accepts a fully numeric ID.
*/
public function test_get_wc_product_id_without_prefix_only_numeric() {
$google_id = 'online:en:US:123abc';
$product_id = $this->product_helper->get_wc_product_id( $google_id );

$this->assertEquals( 0, $product_id );

$google_id = 'online:en:US:abc123';
$product_id = $this->product_helper->get_wc_product_id( $google_id );

$this->assertEquals( 0, $product_id );
}

/**
* Confirm `gla_` prefix must be at the beginning and followed by a numeric ID.
*/
public function test_get_wc_product_id_returns_zero_if_no_id_matches() {
$google_id = 'online:en:US:invalid_gla_123';
$product_id = $this->product_helper->get_wc_product_id( $google_id );

$this->assertEquals( 0, $product_id );

$google_id = 'online:en:US:gla_invalid_id_1';
$product_id = $this->product_helper->get_wc_product_id( $google_id );

Expand Down
Loading