Magento 1后台下单时添加自定义订单状态的技术咨询
Got it, let's walk through how to add a custom order status dropdown on the Magento 1 admin order creation page, plus how to set that status when saving the order. I've done this a few times, so here's a reliable approach:
1. First: Create Your Custom Order Status (If You Haven't Already)
Before adding the dropdown, you need to define the status in Magento's system:
- Log into your Magento admin, go to
System > Order Statuses - Click
Create New Status: fill in a status code (e.g.,pending_custom) and a user-friendly label (e.g.,Pending Custom), then save - Next, click
Assign Status to State: select your new status and link it to a corresponding order state (likePendingorProcessing). This step is critical—without it, the status won't appear in any dropdowns.
2. Add the Custom Status Dropdown to the Admin Order Creation Page
2.1 Modify the Order Creation Template
Find the admin order creation form template (adjust the path if you're using a custom theme):app/design/adminhtml/default/default/template/sales/order/create/form.phtml
Add this code in a logical spot (e.g., above the save buttons) to render the dropdown:
<div class="entry-edit"> <div class="entry-edit-head"> <h4 class="icon-head head-payment-method"><?php echo $this->__('Custom Order Status') ?></h4> </div> <fieldset> <select name="order[custom_status]" id="custom_order_status" class="select"> <?php foreach ($this->getAvailableOrderStatuses() as $statusCode => $statusLabel): ?> <option value="<?php echo $statusCode ?>"><?php echo $statusLabel ?></option> <?php endforeach; ?> </select> </fieldset> </div>
2.2 Extend the Order Creation Block to Fetch Statuses
We need to add a method to load valid statuses for the dropdown. Create a custom module to rewrite the core block:
- Module Configuration (
app/code/local/YourNamespace/OrderStatus/etc/config.xml):
<?xml version="1.0"?> <config> <modules> <YourNamespace_OrderStatus> <version>1.0.0</version> </YourNamespace_OrderStatus> </modules> <global> <blocks> <adminhtml> <rewrite> <sales_order_create_form>YourNamespace_OrderStatus_Block_Adminhtml_Sales_Order_Create_Form</sales_order_create_form> </rewrite> </adminhtml> </blocks> </global> </config>
- Block File (
app/code/local/YourNamespace/OrderStatus/Block/Adminhtml/Sales/Order/Create/Form.php):
class YourNamespace_OrderStatus_Block_Adminhtml_Sales_Order_Create_Form extends Mage_Adminhtml_Block_Sales_Order_Create_Form { public function getAvailableOrderStatuses() { // Fetch all statuses linked to visible front-end states (adjust filter if needed) return Mage::getModel('sales/order_status')->getResourceCollection() ->addStateFilter(Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()) ->toOptionHash(); // Optional: Filter to specific statuses only // ->addFieldToFilter('status', array('in' => array('pending', 'pending_custom', 'processing'))) } }
- Enable the Module (
app/etc/modules/YourNamespace_OrderStatus.xml):
<?xml version="1.0"?> <config> <modules> <YourNamespace_OrderStatus> <active>true</active> <codePool>local</codePool> </YourNamespace_OrderStatus> </modules> </config>
3. Set the Custom Status When Saving the Order
Using an observer is safer than rewriting the controller (avoids conflicts with other modules). Here's how:
3.1 Add Observer to Configuration
Update your module's config.xml (inside <global>) to listen for the order save event:
<events> <sales_order_save_before> <observers> <yournamespace_orderstatus_set_custom_status> <type>singleton</type> <class>YourNamespace_OrderStatus_Model_Observer</class> <method>setCustomOrderStatus</method> </yournamespace_orderstatus_set_custom_status> </observers> </sales_order_save_before> </events>
3.2 Create the Observer Model
Make the observer file (app/code/local/YourNamespace/OrderStatus/Model/Observer.php):
class YourNamespace_OrderStatus_Model_Observer { public function setCustomOrderStatus(Varien_Event_Observer $observer) { $order = $observer->getEvent()->getOrder(); // Only apply for admin-created orders with a valid custom status if (Mage::app()->getStore()->isAdmin() && $order->getCustomStatus()) { // Validate the status is allowed $validStatuses = Mage::getModel('sales/order_status')->getResourceCollection() ->addStateFilter(Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()) ->getColumnValues('status'); if (in_array($order->getCustomStatus(), $validStatuses)) { $order->setStatus($order->getCustomStatus()); // Link the status to its corresponding state $status = Mage::getModel('sales/order_status')->load($order->getCustomStatus()); $order->setState($status->getState(), true); } } return $this; } }
Test It Out
- Flush Magento's cache (
System > Cache Management) - Go to the admin and create a new order—you'll see your custom status dropdown
- Select a status, save the order, and check the order details to confirm the status was applied correctly
内容的提问来源于stack exchange,提问作者Sunny Rahevar

