1

I'm trying to add a new column to the sales order grid in the admin panel.

I have created the file

app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php and extended the 

class Mage_Adminhtml_Block_Sales_Order_Grid in the file.

I have the class setup and extending works good, as I've managed to retrieve sales_flat_order_address.postcode as a column but I want to also add a column to the grid that is a custom customer attribute.

I can see this custom customer attribute in the table customer_entity_varchar but I have no idea how I get that data in to the _prepareCollection() function within

app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php
1

You should never display in orders grid values that might change over time.
A customer attribute is one of these things.
An order should be a "snapshot" of how things were at the moment the order was placed. (the status may change after that and the billing and shipping address. But the customer data and prices should not change.) That's why the customer name and group and e-mail are cloned on the order instance.
Instead create a new column in the sales_flat_order table and sales_flat_order_grid table that should hold the value for that customer attribute at the moment the order was created.
If your customer attribute is called some_attribute then your column name should be customer_some_attribute.
Clear the cache after you add the column.
Now all you need to do is port the value of that attribute to the order object.
In order to do that, you need first to copy this value to the quote object.
For that you need to add the same column on the sales_flat_quote table using the same name customer_some_attribute.

After you created these columns just add in one of your config.xml this inside the <global> tag:

<fieldsets>
    <customer_account>
        <some_attribute>
            <to_quote>customer_some_attribute</to_quote> <!-- this will copy the value to the quote object from the customer account -->
        </some_attribute>
    <customer_account>
    <sales_convert_quote>
        <some_attribute>
            <to_order>customer_some_attribute</to_order> <!-- this will copy the value from the quote to the order -->
        </some_attribute>
    </sales_convert_quote>
</fieldsets>

Now, because you have it as an order attribute you should be able to easily add it to the grid. An other good side of this approach is that you will see that attribute value in the order details page in the backend under the customer name and email without needing to code anything else.

I know it sounds a bit complicated, but this is the cleanest way to do it. If you take it step by step there shouldn't be any problems.

| improve this answer | |
  • The custom customer attribute has to be editable via the customer information in the backend. Does that make your suggestion unusable? – Ste Oct 14 '14 at 15:20
  • If the value of the attribute can absolutely be editable and the value should not be saved in relation to the order then my answer is kind of useless to you. – Marius Oct 14 '14 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.