Web Development Blog

How to edit orders with Configurable Products in Magento

08.10.2009 06:35PM by Evan Johnson

Up to and including the current release of Magento (1.3.2.3), an admin cannot properly edit an order containing configurable products. Magento warns you of this when you click the “Edit” button, and if you proceed the order appears on the edit screen containing only the Simple and Virtual products. Configurable, Bundled, and other products are removed from the order.

I am finding this more and more with Magento: it is the most feature-rich and impressive of all Open Source commerce solutions, but it is still a young piece of software and the result is that all of these great features are only about 80% operational. But enough ranting about Magento (for now).

You need to be able to edit an orders containing configurable products! The code below allows you to do this by converting the Configurable products into their respective Simple products. This is not the same as actually being able to edit the Configurable products. This approach is the same one that Varien takes when allowing an admin to create new orders: you cannot add Configurable products, but you can add any of the Simple products that constitute the Configurable ones.

NOTE: As always, it’s better to roll these into extensions for future upgradability, and do not attempt this on a production server without backing everything up! I don’t have to time to provide support if my little hack causes problems for you, so be careful!

Now on to the code!

First, we want to create a new list of product types that can be edited. We don’t want to mess with the list of product types that can be created however, so we need to make a new list. In Adminhtml/etc/config.xml, add the following code around line 399 (below the adminhtml->sales->order->create list of product types):

<edit>
   <available_product_types>
      <simple/>
      <virtual/>
      <configurable/>
   </available_product_types>
</edit>

This puts Configurable products on the “guest list”, as it were, allowing them to be edited.

Then in Adminhtml/Block/Sales/Order/View.php, in the__construct method, change the code in the “if ($this->_isAllowedAction(‘edit’) && $this->getOrder()->canEdit()) {” block from:

Mage::getConfig()->getNode('adminhtml/sales/order/create/available_product_types')->asArray())

to:

Mage::getConfig()->getNode('adminhtml/sales/order/edit/available_product_types')->asArray())

This code bypasses the JS warning you get when trying to edit an order containing configurable products, since it will not be needed. The warning about Bundled and other non-editable product types will still appear.

And finally (this is the real guts of the hack), in Mage/Adminhtml/Model/Sales/Order/Create.php, in the initFromOrder() method, change the code in the “foreach ($order->getItemsCollection” block as follows:

foreach ($order->getItemsCollection(
            array_keys(Mage::getConfig()->getNode('adminhtml/sales/order/edit/available_product_types')->asArray()),
            false
            ) as $orderItem) {
                if ($order->getReordered()) {
                    $qty = $orderItem->getQtyOrdered();
                }
                else {
                    $qty = $orderItem->getQtyOrdered() - $orderItem->getQtyShipped() - $orderItem->getQtyInvoiced();
                }
                if ($qty > 0) {
                     //if it's a child configurable product
                    if ($orderItem->_getData("product_type") != "configurable") {
                        $item = $this->initFromOrderItem($orderItem, $qty);
                        if (is_string($item)) {
                            Mage::throwException($item);
                        }
                    }
                }
        }

This code grabs the Configurable product’s child Simple products and adds them to the cart instead. This may look a little strange if a front end customer created the order, then re-orders it after the admin has edited it… but otherwise it works fine. The correct number of the correct Simple products are in the order – not lost to the void as they were previously.

It seems to me like it would have been easier for Varien to include this functionality rather than all of the code checking to ignore Configurable products? Who knows. But I hope my code snippet can help someone out!

UPDATE 9.1.09 There is a known issue with Catalog Price Rule sale prices using this hack. To get around it, just remember to hit the “Update Item’s and Qtys” button as soon as you have opened up the order for editing. This re-applies the sale price rules to the Simple items (which replaced the Configurable item).

P.S. I doubt this is compatible with OrganicInternet’s Simple Configurable Products module, but I never tested against it so I don’t know.

Comment

Gravatar Image
On Aug 30, 09:39 PM, (Link)
Matt said:

Hi Evan, great article. Your “find” and “replace” code for the removal of the JS block are the same as far as I can tell.

Doesn’t adding configurable as an editable type eliminate the JS issue here?

Gravatar Image
On Aug 30, 11:53 PM, (Link)
Evan Johnson said:

@Matt

If you look close, the difference in the JS is that “create” has been replaced with “edit”.

What we did in the config.xml was make two separate lists: “creatable” products and “editable” products. The lists are different because we actually don’t want configurable products to be on the order Create page. We only want to add Configurable products to the Edit page.

We then have to change the JS so that it consults the new “edit/available_product_types” list instead of the “create/available_product_types” list.

I hope this helps to clear things up?

Gravatar Image
On Aug 31, 01:22 PM, (Link)
Matt said:

Ahhh I see the difference now. I guess I wasn’t looking closely enough. There are a few threads on the magento forums about this issue, I linked back here in the most relevant one.

Will adding this code to app/code/local work (as opposed to editing the original files)? I think that’s generally best practice but I’m not sure Magento will look in local for admin code.

Gravatar Image
On Aug 31, 11:06 PM, (Link)
Evan Johnson said:

Yes, putting changed Core files in the /local override directory makes it easier to upgrade Magento in the future. However, the BEST way is always to roll it in to an extension. (It’s just not the fastest way.)

I do seem to remember that overriding Controllers can be troublesome… but this hack just changes the Etc, Model and Block code, so it should be fine in the /local directory. I have not tried it though!

Gravatar Image
On Sep 8, 11:02 PM, (Link)
Scott Conrad said:

Evan… Excellent article! Any idea if this would work on bundles as well?

Gravatar Image
On Sep 17, 11:40 PM, (Link)
Evan Johnson said:

@Scott

I have not yet had the pleasure of working with Bundled products. If Bundles are just Parent products which contain various Simple products, I see no reason why a similar approach shouldn’t work. Just change $orderItem->_getData("product_type") to look for "bundle" instead of "configurable? But in my experience with Magento it probably won’t be that easy.

Gravatar Image
On Nov 4, 06:23 PM, (Link)
Pooch said:

What about the financial info? Is all of that retained from the original order? My issue is that whenever an order is made and product is out of stock we need to edit the order to cancel that item, and in order to do that now you effectively have to create a new order and re-enter the credit card info which we do not have since it goes straight to the gateway.

Gravatar Image
On Nov 4, 09:57 PM, (Link)
Evan Johnson said:

@pooch

No, this does not save the financial info. This works exactly the same way editing orders in Magento normally works, except it doesn’t throw out the configurable products.

There are lots of problems with Magento’s "order edit" features, but because of the security issues with storing credit card info I think this "problem" is actually a "feature". It’s certainly inconvenient though!

Gravatar Image
On Nov 4, 10:23 PM, (Link)
Pooch said:

Well then maybe I’m not seeing how this is a useable feature… or perhaps due to the way I’m using magento it’s not a useable feature. I suppose if you were just using the Saved Credit card option it’s not a big deal but I would imagine the vast majority of users are using gateways. In my situation anytime we need to edit an order we’re forced to call the customer to get the credit card info again because PayflowPro doesn’t even let you see the credit card info. Most customers don’t want to give their card number to someone calling them on the phone so what option do we have for editing or creating new orders? This is a huge miss in my opinion. In the real world orders need to be edited. Any ideas? My only thought is to override Magento and capture the CCard data before sending it to the Gateway. How do other carts handle this situation?

Gravatar Image
On Nov 4, 11:00 PM, (Link)
Evan Johnson said:

@pooch

It’s true, the Edit Order feature is not very useful in most cases – especially if you have the payment gateway set to "capture and process". It really does just create a "new" order based on the old one, with the original payment finished and gone from the system. Editing an order has problems anyway (e.g. configurable products), even if you do use the "Saved Credit Card" method (which, for security reasons, I don’t think you are supposed to do unless your whole web store/server is PCI compliant).

There are some uses though: for instance, even if you have to call and re-run an order payment, at least the customer doesn’t have to go and add all the products to the cart again!

If you are curious about where this code came from and why it was useful for me, it is a small piece of the custom code I have had to write for a client who creates (and edits) large wholesale orders in the backend. They don’t actually use Magento to process payments, they just invoice them on "Net 30" terms (and do some stock tracking). Credit cards were not an issue in this case.

Gravatar Image
On Dec 16, 11:06 AM, (Link)
Sean Siegel said:

This is a great solution but I am still running into a few issues.

I would really like to use this with bundle products. I added bundle to sales->order->edit-> in the config.xml. Now when i go to edit an order with a bundle product it works besides two side effects

Problem 1: The product is successfully broken into multiple simple products and they are added to the cart. If you have two separate options available within your bundle product, and they are the same products, the quantity will not reflect the number of items. For instance we sell 4 channel video surveillance dvrs. The customer has 4 options for 4 cameras, they can choose between a few models or choose all 4 of the same.

$this->initFromOrderItem($orderItem, $qty); successfully adds the first item but then doesn’t seem to add any more.

Problem 2: After you edit an order, the bundle product its self is added on the order as a sku along with all the simple products selected. The bundle product its self has the ima original price associated with its sku which would actually charge the customer twice.

Problem 1 is the most important. Any help would be greatly appreciated.

Here are two screenshots of the issues:
http://img52.imageshack.us/img52/1383/placedbycustomer.jpg
http://img52.imageshack.us/img52/5664/placedbycustomeradmined.jpg

Thanks!

Gravatar Image
On Jun 2, 05:47 AM, (Link)
Dr Greenthumb said:

Hi, do you know if this will work for editing grouped products or how to implement a solution for grouped products?

Thanks

Gravatar Image
On Jun 9, 10:06 AM, (Link)
Evan Johnson said:

Hello everyone, just a friendly reminder:

This is a hacky bit of code, and has not been tested with Magento 1.4. Also, I am not currently working with Magento and so will not be able to provide much (if any) help troubleshooting issues. Hopefully Varien will just fix this eventually!

I will try to continue to approve comments periodically so if you have questions or answers, still feel free to post them but I, personally, will not have much useful to say.

Thanks for stopping by!

Gravatar Image
On Oct 29, 08:26 AM, (Link)
Mike said:

Hello,
I got the same problem like Sean Siegel. The new order takes the price of the simple product.
The client updated the configurable product. But there won´t be updated the prices of the simple products, so we need the proce of the main product.
Has anyone a solution for that?

  Subscribe to article comments