Saving Custom Attributes to Order Objects During Checkout in Magento
The Challenge
You’ve created a custom organisation_id attribute for your Magento store using an installer script. This attribute is successfully saved in the database (eav_attribute and core_resource tables). However, when you try to add this value to both the customer and order objects during the onepage checkout process, it doesn’t seem to work as expected.
Understanding the Current Flow
During the checkout phase on the ‘Order Review’ section, clicking the ‘Place Order’ button triggers the sales_order_place_after event. This event executes an observer that should set the organisation_id value for both the customer and order objects.
Here’s how the current implementation looks:
public function afterOrderPlaced($observer)
{
// Get organisation_id from session
$organisation_id = Mage::getSingleton('customer/session')->getCustomerOrganisationId();
// Set organisation_id for order object
$this->_order->setOrganisationId($organisation_id)->save();
// Customer stuff
$this->_customer_id = $this->_order->getCustomerId();
$this->_customer = $this->_order->getCustomer();
$this->_customer->setOrganisationId($organisation_id)->save();
}
The code seems correct, but you’re unable to confirm if the organisation_id is actually being saved correctly.
The Solution: Verify the Saved Data
To verify that your custom attribute has been correctly added to both customer and order objects during checkout, follow these steps:
Step 1: Check the Database Directly
After completing a successful order, check the customer_entity_varchar table for the new organisation_id attribute. If it’s present with the expected value (25621), then the data is being saved correctly to the customer object.
Similarly, check the sales_flat_order_item table for the order-related organisation_id. If the value matches, then the data is also being saved correctly to the order object at checkout.
Step 2: Display the Attribute in Admin
To ensure the attribute appears in the admin panel after an order has been placed:
- Go to the Magento backend and navigate to Sales > Orders.
- Find your recently placed order.
- Click on the ‘View’ button next to it.
- Check if the custom
organisation_idattribute is displayed under the ‘Customer Information’ section.
If the attribute appears with the correct value, then you’ve successfully saved the custom organization ID during checkout.
Conclusion
In summary, to save a custom organisation_id attribute to both customer and order objects at checkout in Magento:
- Ensure your installer script has properly created the attribute for all three tables (
customer,quote, andorder). - Implement the
afterOrderPlacedobserver to set the required values. - Verify the saved data by checking the database directly or displaying it in the admin panel.
This approach allows you to confirm if your custom attribute is being correctly added to both customer and order objects during the checkout process, providing a seamless user experience for your customers while maintaining accurate organizational information within your Magento store.