Forum
Why does editing an address create a new one?
OK, so I go to edit an address, hit save and it has created a new record rather than editing the original one! This rather screws up my plan of keeping just one address per customer and allowing them to edit it!
We'd need to see your code to be able to help!
OK, well I have been looking for a way to allow pass the logged in member address to the edit address form when the user shops for the first time (using this as a add address form really). I then want to show this one address next time they shop with an option to edit, so that there is only ever one address in the system. This is how I have done it. It seems to work except for the extra address being added instead of editing the existing one. I am sure I have probably got something muddled somewhere along the line as it took me a while to get my head around the whole thing!
So we start at address.php (the user is already logged in as a member at this point). I test if an address exists for the customer:
If no address, then the shop edit address form is pre-populated with their member address, but they are allowed to change it:
The page refreshes on submitting (only one address is created at this point - yippee!) and shows the same as they would if there was already an address on the system so checkout/order_address_form.html looks like this:
Note that I am filtering on the 'Main' field - I was trying to find a way to filter for just the address that had been created in the previous form, but since multiples are being created on edit, it is pretty useless! Here, you will see that I have hidden the radio buttons and show just the address an edit link and a submit button. The submit should send the order address form so that the order address is now set (although I am now getting a blank page on checkout for some reason - was working before. May be just something I have made a mess of while making changes to the address set up, but that is the next issue to sort out!). If they edit an address, they go to address-edit.php:
addresses/edit-real.html:
This goes back to address.php again after completion. It is at this point that a whole new address seems to be added instead of editing the original one. The form is populated with the correct address as per the address ID.
So that is the whole flow of the address thing. Am I doing something wrong?
perch_shop_edit_address_form()
will create a new address.If you want to edit an address, you need to pass in the ID of the address to edit, which you can get with
perch_shop_customer_addresses()
Yes, I thought that is what I was doing. In address-edit.php I have:
The 's' is passed on from the order address form where I have:
Therefore, it is getting the addressID for the address that is shown on the address form (the correct ID according to the database). The correct address also shows as pre-populated on the edit form on address-edit.php. I just don't get why it should be creating a new one still.
Are you trying to edit an address that is already associated with an order?
I realise I forgot to show where I was getting $addresses from on address.php
No, not already associated with an order, just with that customer. This is before I have placed an order.
I know it is unlikely that someone will save their address to the shop for the first time and then edit it before placing an order, but they might do...
Hmm, OK so also when I proceed to the checkout page (after submiting the order address form, selecting billing and shipping addresses (always the same anyway)) the address gets added twice more (although only shows up on my address.php page once if I go back there)!
All I want to do is have ONE customer address. This is used for billing and shipping (they have no choice about that). The first time they order, I want the form it pre-poulated with their member address for ease of use. I then want them to be able to edit that address, but not add any new ones.
Am I going about it in completely the wrong way?
Can you show me the debug output when the edit happens?
It's normal to have multiples in the database table - they get locked when an order is placed so that you can't destroy your tax audit history.
Sorry, I didn't get an email that time telling me you had replied.
On address-edit.php at the time of editing:
SELECT * FROM perch2_members_sessions WHERE sessionID='560a5239b7dbf27e57322897e036a6a41c367e62' AND sessionHttpFootprint='14e734a83aa2b75dd785af4979363931fedba4fe' AND sessionExpires>'2016-10-17 18:34:12' LIMIT 1 User is logged in [1] SELECT * FROM perch2_pages WHERE pagePath='/shop/address-edit.php' LIMIT 1 [1] SELECT * FROM perch2_shop_cart WHERE cartID=9 [1] SELECT * FROM perch2_shop_cart WHERE cartID=9 [250] SELECT country, countryID FROM perch2_shop_countries ORDER BY country ASC [25] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0 Using template: \templates\shop\addresses\edit-real.html [1] SELECT * FROM perch2_shop_customers WHERE memberID=1 [1] SELECT * FROM perch2_shop_addresses WHERE customerID='1' AND addressID=3 [1] SELECT * FROM perch2_shop_countries WHERE countryID='236' LIMIT 1
and on address.php after editing an addres (and now seeing two addresses):
Are either of those after the form is posted?
The second one is. The first is before...... I have the page to redirect to address.php after the form is posted. I will remove the redirection and send the debug on the address-edit.php AFTER the form is subitted
Here you go:
The ID of the address I was editing was 5, but it clearly references 6 here, which is the ID of the new address it creates instead of editing 5
Are you passing the address ID through in the edit form?
No! That'd be it then! Thank you for pointing out my utter stupidity! Too many late nights me thinks...
So now it is working, do you think this is a good way of going about what I am trying to do in general then? If I really must make the member keep two addresses (one for member, one for shop) I want it to be as simple as possible by pre-populating the initial address form and then keeping just the one address and allowing them to edit it. This is the only way I coudl get my head around doing that - is it how you'd do it?
Do you need to keep an address against the Member if you keep it in the Shop?
Yes as the whole member thing needs to come first really. The shop is more of an extension to the membership allowing them to place an order. We need to easily access all the member's data including the address in one place rather than having to go to the shop app just for their address.
Ok, so I think that's your answer.