Page 1 of 1

How to add a Delete function to the Orders admin screen

Posted: Sun Feb 17, 2008 10:00 pm
by AbleMods
Advisory
Please use extreme caution when using this modification. It is unforgiving. Make a mistake and you're restoring your database. There is no "Undo". Seriously. There is also no "Are you sure?" when using this modification. Real site admins don't need it (plus I don't know how to add one) ;)

This modification assumes AC7 RC2.

Introduction
There are many times where I have found the need to delete an order. It usually involves my own testing or an unusual payment processing failure.
When an order gets messed up, the customer usually places another order, if they even know there was a problem. Usually this results in 3-4 bad orders on the Admin side. there's no way to delete them, so you're forced to sort "around" them as you work to process your business. It also forces the customer to live with a really confusing order history.

This modification allows the Admin to delete a single or group of orders right from Admin screen using the "Update Selected Orders" feature.

How it works
This modification uses the same concept already built into the Update Selected Orders dropdown. Just click the checkboxes for the orders to delete, select the "Delete" dropdown option and click Go.

Modifications
First make a backup copy of your ~/Admin/Orders/default.aspx and default.aspx.cs files. These are the files we will be changing in this modification.

Edit ~/Admin/orders/Default.aspx file and look for the start of the dropdown list control - here is some of this code:

Code: Select all

                                <asp:DropDownList ID="BatchAction" runat="server">
                                    <asp:ListItem Text=""></asp:ListItem>
                                    <asp:ListItem Text="Process Payment" Value="PAY"></asp:ListItem>
                                    <asp:ListItem Text="Mark Shipped" Value="SHIP"></asp:ListItem>
                                    <asp:ListItem Text="Mark Shipped with Options" Value="SHIPOPT"></asp:ListItem>
                                    <asp:ListItem Text="Cancel" Value="CANCEL"></asp:ListItem>
                                    <asp:ListItem Text="-----------"></asp:ListItem>
Directly below the first "------------" ListItem entry you see, add this code:

Code: Select all

                                    <asp:ListItem Text="* DELETE *" Value="DELETE"></asp:ListItem>
                                    <asp:ListItem Text="-----------"></asp:ListItem>
Save it.

Now edit the ~/Admin/Orders/Default.aspx.cs file. Find the code located in the BatchButton_Click function that looks like this:

Code: Select all

                    case "SHIPOPT":
                        Response.Redirect("Batch/Ship.aspx?orders=" + GetOrderList(orderIds));
                        break;
We need to Insert another CASE section of code like this directly below the "break;" line. So add the following code as described:

Code: Select all

                    case "DELETE":
                        int DelCount = 0;
                        foreach (int orderId in orderIds)
                        {
                            Order order = OrderDataSource.Load(orderId);
                            if (order != null)
                            {
                                order.Delete();
                                messages.Add("Order #" + order.OrderId + " deleted.");
                                DelCount++;
                            }
                        }
                        messages.Add(DelCount + " orders deleted.");
                        break;

Save it.

Testing
It's critical you test this in your development environment first. There is no "Undo" feature - once an order is deleted, it's gone. Note how you can delete one order at a time, or delete multiple orders in a single command. Test both scenarios and confirm the properly selected orders were removed and no others.

Cascade Deletes
Deleting an order cascades through the other files automatically. Payment records, notes and history, shipments etc will also be automatically deleted. There should be no trace of the order left when the command is finished.

Conclusion
Order management is a key point in the business process where mistakes can be made. Clearing out unwanted orders helps the admin keep a clean running store with less potential for errors. It also gives the customer a good looking order history to review their purchases.

Posted: Mon Feb 18, 2008 7:05 am
by sweeperq
Is there a reason you wouldn't just cancel the order instead?

Posted: Mon Feb 18, 2008 7:17 am
by AbleMods
of course, but cancelled orders never leave the list or the customer order history.

Posted: Mon Feb 18, 2008 12:58 pm
by batmike
Works great for me ... and helps keep things clean and neat. Thanks!

Posted: Thu Feb 21, 2008 10:13 am
by compunerdy
Works great..I hope able is going to include some of these nice add ons with future releases so we dont have to manually change them after each update.

Posted: Fri Feb 22, 2008 1:23 pm
by Hostmaster
Joe another great add on by you.
I actually placed " ** DELETE Permanently No Undo: Print First " as the list Item text. That way the end user, my client, can’t yell when he deletes something good and doesn’t have a copy :D

Re: How to add a Delete function to the Orders admin screen

Posted: Sun May 11, 2008 12:58 am
by compunerdy
Does this do a restock?

Re: How to add a Delete function to the Orders admin screen

Posted: Fri Nov 28, 2008 1:25 pm
by TTMedia
BUmp.. does this do a restock?

I assume it doesn't matter because stock is only pulled when the order is put to ship.. and at that point, it's too late to cancel/delete the order.

Re: How to add a Delete function to the Orders admin screen

Posted: Fri Nov 28, 2008 9:03 pm
by AbleMods
Dunno. Test it and see what it does. I don't maintain my own inventory figures.

Re: How to add a Delete function to the Orders admin screen

Posted: Wed Feb 25, 2009 5:28 pm
by tosypian
Does this work as is on the latest version of AC7?

Re: How to add a Delete function to the Orders admin screen

Posted: Thu Feb 26, 2009 5:45 am
by mazhar
Yes it will work.

Re: How to add a Delete function to the Orders admin screen

Posted: Thu Jun 11, 2009 9:16 am
by robrbecker
You can add a delete confirmation dialog easily.

In Admin\Orders\Default.aspx add an OnClientClick attribute to the button so it looks like:

Code: Select all

<asp:Button ID="BatchButton" runat="server" Text="GO" OnClick="BatchButton_Click" OnClientClick="if ($('ctl00_MainContent_BatchAction').value == 'DELETE') return confirm('Are  you sure you want to delete this order? It will be totally and permanently gone!');"/>

Re: How to add a Delete function to the Orders admin screen

Posted: Thu Jun 11, 2009 11:58 am
by ZLA
As a less permanent delete function, I would recommend the following if you are decent at SQL:
[*]Create copies of all order table schemas. For example del_Orders, del_OrderItems, etc.
[*]Modify the schemas to remove identity from any columns but leave them as primary keys.
[*]Create a delete trigger on ac_Orders that copies that order's data from ac_Orders to del_Orders, ac_OrderItems to del_OrderItems, etc. If necessary, you could build something which would iterate through sysColumns to build the copy statements dynamically in case you have customized and added columns to any of these core tables.

You could also modify the schemas and add DeleteId, DeletedOn, DeletedBy columns if you need type of info as well.

Now if you ever need that data you can look it up via your favorite database tool. Or go one better and create an undelete function which reinserts these records into the main table using SET IDENTITY OFF.

Re: How to add a Delete function to the Orders admin screen

Posted: Thu May 12, 2011 3:45 pm
by jasonhendee
Very cool, and much needed mod - Thanks!