Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 5, 2026

Fix for guest order shipping address overwritten by billing address

Problem Statement

When editing a guest order in the admin panel with different billing and shipping addresses, the shipping address was incorrectly overwritten by the billing address during the initialization of the "Edit Order" process.

Root Cause

In Mage_Sales_Model_Quote_Address::_populateBeforeSaveData() at line 325, the logic unconditionally set same_as_billing for new addresses (without an ID) based on whether the customer is registered. For guest orders, _isSameAsBilling() returns true, which overwrote the explicitly set value from initFromOrder() that had correctly determined the addresses were different.

Solution Implemented

  • Analyze the current code in Address.php and Create.php
  • Implement the fix: Add !$this->hasSameAsBilling() check to line 325 in Address.php
  • Create unit tests to validate the fix
  • Run code review - No issues found
  • Run security checks - No vulnerabilities detected
  • Fix all PHP CS Fixer errors in test file
  • Fix all Rector errors in test file
  • Verify the fix resolves the issue

Changes Made

  1. ✅ Modified /app/code/core/Mage/Sales/Model/Quote/Address.php line 325

    • Before: if (!$this->getId())
    • After: if (!$this->getId() && !$this->hasSameAsBilling())
    • This ensures explicitly set same_as_billing values are preserved during order editing
  2. ✅ Created comprehensive unit tests in /tests/unit/Mage/Sales/Model/Quote/AddressTest.php

    • Tests verify explicitly set values are preserved for guest orders with different addresses
    • Tests verify explicitly set values are preserved for guest orders with same addresses
    • Tests verify default behavior still works when no value is explicitly set
    • Tests verify behavior for registered customers
    • Fixed all PHP CS Fixer errors (trailing whitespace, docblock formatting, static assertions, trailing commas)
    • Fixed all Rector errors (removed unnecessary setAccessible calls)

How It Works

The fix checks two conditions before applying default logic:

  1. !$this->getId() - Address is new (no database ID yet)
  2. !$this->hasSameAsBilling() - No explicit value has been set

This means:

  • ✅ When initFromOrder() sets same_as_billing=0 (different addresses), it's preserved
  • ✅ When initFromOrder() sets same_as_billing=1 (same addresses), it's preserved
  • ✅ When creating a new order without explicit value, default logic applies
  • ✅ Works correctly for both guest and registered customers

Impact

This is a minimal, surgical fix that:

  • ✅ Resolves the reported bug for guest order editing
  • ✅ Maintains backward compatibility with existing functionality
  • ✅ Preserves default behavior when no explicit value is set
  • ✅ Requires only a single-line change to production code
  • ✅ Follows all project coding standards (PHP CS Fixer and Rector compliant)
Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG] Guest order shipping address overwritten by billing address during order edit</issue_title>
<issue_description>### Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When editing a guest order in the admin panel, if the order originally had different billing and shipping addresses, the shipping address is incorrectly overwritten by the billing address during the initialization of the "Edit Order" process.

This happens because Mage_Sales_Model_Quote_Address::_populateBeforeSaveData contains logic that forces the same_as_billing flag to 1 for guest orders if the address is new (which it is during the initFromOrder process).

Expected Behavior

When editing order address should be different.

Steps To Reproduce

  1. Create a guest order with different billing and shipping addresses.
  2. In the Admin Panel, go to Sales > Orders and open the order.
  3. Click the Edit button.
  4. Observe that the Shipping Address section now contains the Billing Address data, and the "Same As Billing Address" checkbox is checked.

Environment

- OpenMage 20.15.0
- php: 8.3

Anything else?

Root Cause In app/code/core/Mage/Sales/Model/Quote/Address.php, the method _populateBeforeSaveData has the following logic:

if (!$this->getId()) {
    $this->setSameAsBilling((int) $this->_isSameAsBilling());
}

For guest orders, _isSameAsBilling() returns true if the customer is not registered. This logic overwrites any value explicitly set during the order conversion process (e.g., in Mage_Adminhtml_Model_Sales_Order_Create::initFromOrder).

Proposed Fix
Modify the condition to only apply the default guest logic if the same_as_billing flag has not been explicitly set yet.

--- app/code/core/Mage/Sales/Model/Quote/Address.php
+++ app/code/core/Mage/Sales/Model/Quote/Address.php
@@ -342,7 +342,7 @@
              * Set same_as_billing to "1" when default shipping address is set as default
              * and it is not equal billing address
              */
-            if (!$this->getId()) {
+            if (!$this->getId() && !$this->hasSameAsBilling()) {
                 $this->setSameAsBilling((int) $this->_isSameAsBilling());
             }
         }

Why this is correct
By checking !$this->hasSameAsBilling(), we ensure that if the conversion logic has already determined that the addresses are different (and thus called setSameAsBilling(0)), we don't revert that decision based on the generic guest rule. This preserves the data integrity during order editing and reordering.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- Modified _populateBeforeSaveData in Address.php to preserve explicitly set same_as_billing flag
- Added unit tests to validate the fix for guest and registered customers
- Tests verify that explicitly set values are preserved while defaults still apply when not set

Co-authored-by: addison74 <[email protected]>
Copilot AI changed the title [WIP] Fix guest order shipping address overwrite issue Fix guest order shipping address overwritten by billing address during order edit Jan 5, 2026
Copilot AI requested a review from addison74 January 5, 2026 10:35
@github-actions github-actions bot added Component: Sales Relates to Mage_Sales phpunit labels Jan 5, 2026
- Remove trailing whitespace from all lines
- Add blank line between docblock description and @Covers annotation
- Format multi-line assertSame calls to comply with coding standards

Co-authored-by: addison74 <[email protected]>
Copy link
Contributor Author

Copilot AI commented Jan 5, 2026

@copilot - fix all PHP CS Fixer errors reported bellow:

...

Fixed all PHP CS Fixer errors in commit 8db9588:

  • Removed trailing whitespace from all lines
  • Added blank lines between docblock descriptions and annotations
  • Formatted multi-line assertSame calls to comply with coding standards

- Change $this->assertSame to self::assertSame (static call)
- Add trailing commas after last parameter in all assertSame calls
- Remove trailing whitespace after closing braces

Co-authored-by: addison74 <[email protected]>
Copy link
Contributor Author

Copilot AI commented Jan 5, 2026

@copilot - fix more PHP CS Fixer errors. see bellow.

...

Fixed all additional PHP CS Fixer errors in commit 41270a6:

  • Changed $this->assertSame to self::assertSame (static assertions)
  • Added trailing commas after last parameter in all assertSame calls
  • Removed trailing whitespace after closing braces

- Remove setAccessible(true) calls from ReflectionMethod invocations
- Not needed in PHP 8.1+ for protected methods accessed via reflection

Co-authored-by: addison74 <[email protected]>
Copy link
Contributor Author

Copilot AI commented Jan 5, 2026

@copilot - fix Rector errors. see bellow

...

Fixed all Rector errors in commit 7156cfe:

  • Removed setAccessible(true) calls from all 4 ReflectionMethod invocations
  • These calls are no longer needed in PHP 8.1+ for protected methods accessed via reflection

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jan 5, 2026

@github-actions
Copy link
Contributor

github-actions bot commented Jan 5, 2026

Test Results

970 tests  +4   962 ✅ +4   17s ⏱️ -1s
214 suites +1     8 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit 7156cfe. ± Comparison against base commit 533b2a7.

♻️ This comment has been updated with latest results.

@addison74 addison74 marked this pull request as ready for review January 5, 2026 12:42
Copilot AI review requested due to automatic review settings January 5, 2026 12:42
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where guest order shipping addresses were incorrectly overwritten by billing addresses during order edit in the admin panel. The fix adds a conditional check to preserve explicitly set same_as_billing values while maintaining the default behavior for new orders.

Key Changes:

  • Added !$this->hasSameAsBilling() condition to prevent overwriting explicitly set address relationship flags
  • Comprehensive unit tests covering all scenarios: guest orders with different/same addresses, registered customers, and default behavior

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
app/code/core/Mage/Sales/Model/Quote/Address.php Modified line 325 to add !$this->hasSameAsBilling() check, preserving explicitly set same_as_billing values during order edit
tests/unit/Mage/Sales/Model/Quote/AddressTest.php Added comprehensive unit tests covering guest orders, registered customers, explicit values, and default behavior scenarios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Component: Sales Relates to Mage_Sales phpunit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Guest order shipping address overwritten by billing address during order edit

3 participants