Skip to content

WooCommerce Enhanced Conversions duplicate country code in phone numbers #11737

@hussain-t

Description

@hussain-t

Bug Description

When WooCommerce purchases include a phone number that already contains a country code prefix (e.g., +94), Site Kit's Enhanced Conversions implementation adds the country code again programmatically, resulting in a duplicated prefix in the data layer.

Example:

  • User enters: +94771770589
  • Data layer outputs: +9494771770589 (country code +94 duplicated)

Expected behavior:
If the user-entered phone number already contains a country code prefix, Site Kit should not prepend it again.

Bug Bashing Asana ticket for reference.

Steps to reproduce

Steps to reproduce

  1. Install and activate WooCommerce
  2. Enable Site Kit's Enhanced Conversions (ensure gtagUserData feature flag is enabled)
  3. Create a test product in WooCommerce
  4. Proceed to checkout
  5. Enter billing information with a phone number that includes the country code, e.g., +94771770589
  6. Complete the purchase
  7. Inspect the data layer in Tag Assistant
  8. Observe the user_data.phone_number field shows a duplicated country code: +9494771770589

Additional Scenarios

Scenario 1: Short national number without country code

  • Input: 7800099
  • Expected: +447800099 (if billing country is UK)
  • Actual: Phone number doesn't appear in events (fails length validation after normalization)

Scenario 2: User enters a different country code than the billing country

  • Input: +2307800099 (Mauritius +230)
  • Billing Country: UK (which has +44)
  • Expected: +2307800099 (respect user's explicit country code)
  • Actual: +442307800099 (billing country code incorrectly prepended, overriding user's input)

Screenshots

Additional Context

  • PHP Version:
  • OS: [e.g. iOS]
  • Browser: [e.g. chrome, safari]
  • Plugin Version: [e.g. 22]
  • Device: [e.g. iPhone6]

Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

  • Phone numbers entered with a country code prefix (e.g., +94771770589) should not have the country code duplicated in the data layer.
  • Phone numbers entered without a country code prefix (national format) should have the billing country code prepended correctly.
  • Phone numbers entered with a different country code than the billing country should respect the user's explicitly provided country code.
  • All phone numbers should pass E.164 format validation (11-15 characters including +).

Implementation Brief

  • Update the get_normalized_phone() method in includes/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerce.php to detect and handle existing country codes:
    • Preserve the original input to detect if user explicitly provided a + prefix using strpos( trim( $phone ), '+' ) === 0
    • Extract digits using preg_replace( '/[^0-9]/', '', $phone )
    • Get the billing country calling code digits (without the + sign) using ltrim( $calling_code, '+' )
    • If the phone number already starts with the billing country code digits, format as E.164 without duplication: '+' . $phone_digits
    • If the user provided a + prefix but the digits don't match billing country code, respect the user's country code: '+' . $phone_digits
    • Only prepend the billing country code for national numbers (no existing + prefix and doesn't start with country code): Remove leading zeros with ltrim( $phone_digits, '0' ) then prepend $calling_code
    • Validate the final number is between 11-15 characters (E.164 format)

Test Coverage

  • Add unit tests for get_normalized_phone() method in tests/phpunit/integration/Core/Conversion_Tracking/Conversion_Event_Providers/WooCommerceTest.php:
    • Test phone number with correct country code prefix is not duplicated (e.g., +94771770589+94771770589)
    • Test phone number with different country code than billing is respected (e.g., +2307800099 with UK billing → +2307800099)
    • Test national number without country code gets it prepended (e.g., 771770589+94771770589)
    • Test national number with leading zero (e.g., 0771770589+94771770589)

QA Brief

  1. Follow the steps to reproduce.
  2. Inspect the data layer in Tag Assistant.
  3. Observe the user_data.phone_number field shows a correct country code for example +94771770589.

Test Case 1: Phone with correct country code (main bug fix)

  1. Go to WooCommerce checkout
  2. Set billing country to Sri Lanka
  3. Enter phone number with country code: +94771770589
  4. Complete the purchase
  5. On the thank you page, open Tag Assistant and inspect the data layer
  6. Verify: user_data.phone_number shows +94771770589 (no duplication)
  7. Expected: Country code +94 should appear only once

Test Case 2: Phone with different country code than billing

  1. Go to WooCommerce checkout
  2. Set billing country to United Kingdom
  3. Enter phone number with Mauritius country code: +2307800099
  4. Complete the purchase
  5. Inspect the data layer in Tag Assistant
  6. Verify: user_data.phone_number shows +2307800099 (respects user's country code)
  7. Expected: Should NOT be changed to +442307800099

Test Case 3: National number without country code

  1. Go to WooCommerce checkout
  2. Set billing country to Sri Lanka
  3. Enter phone number without country code: 771770589
  4. Complete the purchase
  5. Inspect the data layer in Tag Assistant
  6. Verify: user_data.phone_number shows +94771770589 (country code prepended)
  7. Expected: Billing country code +94 should be automatically added

Test Case 4: National number with leading zero

  1. Go to WooCommerce checkout
  2. Set billing country to Sri Lanka
  3. Enter phone number with leading zero: 0771770589
  4. Complete the purchase
  5. Inspect the data layer in Tag Assistant
  6. Verify: user_data.phone_number shows +94771770589 (leading zero removed, country code prepended)
  7. Expected: Leading zero should be stripped before adding country code

Test Case 5: International format with 00 prefix (same country)

  1. Go to WooCommerce checkout
  2. Set billing country to Sri Lanka
  3. Enter phone with international 00 prefix: 0094770601017
  4. Complete the purchase
  5. Inspect the data layer in Tag Assistant
  6. Verify: user_data.phone_number shows +94770601017 (00 prefix removed, no duplication)
  7. Expected: Should strip 00 and format as E.164, NOT +9494770601017

Test Case 6: International format with 00 prefix (different country)

  1. Go to WooCommerce checkout
  2. Set billing country to Sri Lanka
  3. Enter Indian phone with 00 prefix: 00919361653826
  4. Complete the purchase
  5. Inspect the data layer in Tag Assistant
  6. Verify: user_data.phone_number shows +919361653826 (00 prefix removed, India country code respected)
  7. Expected: Should strip 00 and preserve India's +91 code, NOT +94919361653826

Changelog entry

  • Fix incorrect formatting of WooCommerce enhanced conversions phone numbers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P0High priorityTeam MIssues for Squad 2Type: BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions