Skip to content

Commit 9df7fe9

Browse files
Copilotaddison74
andcommitted
Fix guest order shipping address overwrite issue
- 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]>
1 parent e87de2b commit 9df7fe9

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

app/code/core/Mage/Sales/Model/Quote/Address.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ protected function _populateBeforeSaveData()
322322
* Set same_as_billing to "1" when default shipping address is set as default
323323
* and it is not equal billing address
324324
*/
325-
if (!$this->getId()) {
325+
if (!$this->getId() && !$this->hasSameAsBilling()) {
326326
$this->setSameAsBilling((int) $this->_isSameAsBilling());
327327
}
328328
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
/**
4+
* @copyright For copyright and license information, read the COPYING.txt file.
5+
* @link /COPYING.txt
6+
* @license Open Software License (OSL 3.0)
7+
* @package OpenMage_Tests
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace unit\Mage\Sales\Model\Quote;
13+
14+
use Mage;
15+
use Mage_Customer_Model_Group;
16+
use Mage_Sales_Model_Quote;
17+
use Mage_Sales_Model_Quote_Address;
18+
use OpenMage\Tests\Unit\OpenMageTest;
19+
20+
final class AddressTest extends OpenMageTest
21+
{
22+
/**
23+
* Test that explicitly set same_as_billing flag is preserved for guest orders
24+
*
25+
* This test validates the fix for the issue where guest order shipping addresses
26+
* were being overwritten by billing addresses during order edit.
27+
*
28+
* @covers Mage_Sales_Model_Quote_Address::_populateBeforeSaveData
29+
* @group Model
30+
*/
31+
public function testPreservesExplicitlySetSameAsBillingForGuestOrders(): void
32+
{
33+
// Create a quote for a guest customer (no customer ID)
34+
$quote = new Mage_Sales_Model_Quote();
35+
$quote->setCustomerId(null);
36+
$quote->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
37+
38+
// Create a shipping address with different data than billing
39+
$address = new Mage_Sales_Model_Quote_Address();
40+
$address->setQuote($quote);
41+
$address->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING);
42+
43+
// Explicitly set same_as_billing to false (0) to indicate different addresses
44+
// This simulates what happens during order edit when addresses differ
45+
$address->setSameAsBilling(0);
46+
47+
// Trigger the _populateBeforeSaveData method via _beforeSave
48+
// This is normally called when saving the address
49+
$reflectionClass = new \ReflectionClass($address);
50+
$method = $reflectionClass->getMethod('_populateBeforeSaveData');
51+
$method->setAccessible(true);
52+
$method->invoke($address);
53+
54+
// Assert that the explicitly set value was preserved
55+
$this->assertSame(0, $address->getSameAsBilling(),
56+
'Explicitly set same_as_billing=0 should be preserved for guest orders during order edit');
57+
}
58+
59+
/**
60+
* Test that same_as_billing is set to default for new addresses without explicit value
61+
*
62+
* @covers Mage_Sales_Model_Quote_Address::_populateBeforeSaveData
63+
* @group Model
64+
*/
65+
public function testSetsDefaultSameAsBillingForNewGuestAddressesWithoutExplicitValue(): void
66+
{
67+
// Create a quote for a guest customer (no customer ID)
68+
$quote = new Mage_Sales_Model_Quote();
69+
$quote->setCustomerId(null);
70+
$quote->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
71+
72+
// Create a shipping address WITHOUT explicitly setting same_as_billing
73+
$address = new Mage_Sales_Model_Quote_Address();
74+
$address->setQuote($quote);
75+
$address->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING);
76+
77+
// DO NOT set same_as_billing - let the default logic handle it
78+
79+
// Trigger the _populateBeforeSaveData method
80+
$reflectionClass = new \ReflectionClass($address);
81+
$method = $reflectionClass->getMethod('_populateBeforeSaveData');
82+
$method->setAccessible(true);
83+
$method->invoke($address);
84+
85+
// For guest orders, default behavior should set same_as_billing to 1
86+
$this->assertSame(1, $address->getSameAsBilling(),
87+
'Default same_as_billing=1 should be set for new guest shipping addresses without explicit value');
88+
}
89+
90+
/**
91+
* Test that explicitly set same_as_billing=1 is preserved
92+
*
93+
* @covers Mage_Sales_Model_Quote_Address::_populateBeforeSaveData
94+
* @group Model
95+
*/
96+
public function testPreservesExplicitlySetSameAsBillingTrue(): void
97+
{
98+
// Create a quote for a guest customer
99+
$quote = new Mage_Sales_Model_Quote();
100+
$quote->setCustomerId(null);
101+
$quote->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
102+
103+
// Create a shipping address
104+
$address = new Mage_Sales_Model_Quote_Address();
105+
$address->setQuote($quote);
106+
$address->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING);
107+
108+
// Explicitly set same_as_billing to true (1)
109+
$address->setSameAsBilling(1);
110+
111+
// Trigger the _populateBeforeSaveData method
112+
$reflectionClass = new \ReflectionClass($address);
113+
$method = $reflectionClass->getMethod('_populateBeforeSaveData');
114+
$method->setAccessible(true);
115+
$method->invoke($address);
116+
117+
// Assert that the explicitly set value was preserved
118+
$this->assertSame(1, $address->getSameAsBilling(),
119+
'Explicitly set same_as_billing=1 should be preserved');
120+
}
121+
122+
/**
123+
* Test that the fix works for registered customers with different addresses
124+
*
125+
* @covers Mage_Sales_Model_Quote_Address::_populateBeforeSaveData
126+
* @group Model
127+
*/
128+
public function testPreservesExplicitlySetSameAsBillingForRegisteredCustomers(): void
129+
{
130+
// Create a quote for a registered customer
131+
$quote = new Mage_Sales_Model_Quote();
132+
$quote->setCustomerId(123); // Registered customer
133+
134+
// Create a shipping address
135+
$address = new Mage_Sales_Model_Quote_Address();
136+
$address->setQuote($quote);
137+
$address->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING);
138+
139+
// Explicitly set same_as_billing to false
140+
$address->setSameAsBilling(0);
141+
142+
// Trigger the _populateBeforeSaveData method
143+
$reflectionClass = new \ReflectionClass($address);
144+
$method = $reflectionClass->getMethod('_populateBeforeSaveData');
145+
$method->setAccessible(true);
146+
$method->invoke($address);
147+
148+
// Assert that the explicitly set value was preserved
149+
$this->assertSame(0, $address->getSameAsBilling(),
150+
'Explicitly set same_as_billing=0 should be preserved for registered customers');
151+
}
152+
}

0 commit comments

Comments
 (0)