Displaying Custom Order Attribute in Magento Backend
Understanding Your Current Setup
You’re on the right track with creating an EAV attribute for your custom “Delivery Date” field. You’ve added it to the order entity and have a basic working configuration, including an observer setup. However, you’re facing a couple of key challenges:
- Saving the customer-entered value: When a customer enters a delivery date on the shopping cart page, this information isn’t being saved to the order object.
- Retrieving the value for admin display: Once the information is saved (or not, in your current case), you need to find a way to retrieve it and show it in the Magento backend.
Saving Custom Attribute Data
Step 1: Understanding the Observer Event
The checkout_type_onepage_save_order event isn’t fired at the right moment. This event is triggered when an order is saved during checkout, but before the order object is actually created or populated with data.
For your use case, you want to capture data before it’s passed into the order creation process. The checkout_type_onepage_save_before event would be more suitable as it fires just before the order is created and you can modify the order object then.
Step 2: Modifying Your Observer
Replace your current observer method with this:
public function Deliverydate($observer) {
$event = $observer->getEvent();
$quote = $event->getQuote();
// Get the custom attribute value from the quote item
$deliveryDate = $quote->getData('delivery_date');
if ($deliveryDate !== null && $deliveryDate != '') {
// Set the delivery date to the order object
$order = $observer->getOrder();
$order->setDeliverydate($deliveryDate);
}
}
Step 3: Updating Your Config.xml
Update your config.xml as follows:
<events>
<checkout_type_onepage_save_before>
<observers>
<Deliverydate_observer>
<type>singleton</type>
<class>deliverydate/observer</class>
<method>Deliverydate</method>
</Deliverydate_observer>
</observers>
</checkout_type_onepage_save_before>
</events>
Retrieving and Displaying Custom Attribute Data
Step 1: Enabling the Column in Grid
In your module’s Adminhtml folder, create a system.xml file with this content:
<config>
<sections>
<deliverydate settings>
<groups>
<deliverydate_settings groupId="0" sortOrder="75">
<fields>
<delivery_date_column translate="label">
<arguments>
<column_model>adminhtml/grid_column</column_model>
<text>Delivery Date</text>
<width>120px</width>
<index>deliverydate</index>
<enabled>true</enabled>
<filter_index>deliverydate</filter_index>
</arguments>
</delivery_date_column>
</fields>
</deliverydate_settings>
</groups>
</deliverydate settings>
</sections>
</config>
This tells Magento to enable a “Delivery Date” column in your sales order grid.
Step 2: Updating Your Observer
In addition to setting the delivery date attribute, you also need to ensure it’s saved properly. Update your observer method:
public function Deliverydate($observer) {
$event = $observer->getEvent();
$quote = $event->getQuote();
// Get the custom attribute value from the quote item
$deliveryDate = $quote->getData('delivery_date');
if ($deliveryDate !== null && $deliveryDate != '') {
// Set the delivery date to the order object and save it
$order = $observer->getOrder();
$order->setDeliverydate($deliveryDate);
$order->save();
}
}
Conclusion
You now have a working setup that saves the custom “Delivery Date” attribute entered by the customer during checkout and displays it in the Magento backend. The key was identifying the correct observer event (checkout_type_onepage_save_before) to capture the data before the order is created, and then properly saving this data.
Remember, always make sure your code is well-tested and follows Magento’s best practices for module development. If you’re looking for more advanced solutions or need help with other customizations, feel free to explore our services at Laravel Company.