1
votes

Mage v1.6

i'd like to modify the checkmo payment method to allow an order to be promoted to the processing state upon creation. I've found that by changing the /app/code/core/Mage/Payment/etc/system.xml (yes, just fiddling with core now on test server, will modify it properly if it works) file here:

<checkmo translate="label">
  <fields>
    <order_status translate="label">
      <source_model>adminhtml/system_config_source_order_status_new</source_model>

by removing the "new" in the source_model you can select an order status in the configuration that belongs to the processing state.

However, the orders are not really in the processing state. They remain in the new/pending state, but the status is one from the processing state. A weird hybrid.

This doesn't quite work, since my goal is to be able to easily switch an order between the 4 custom order statuses i have made for the processing state. the reason is to get all orders, check or CC to be in the same state so they can be treated similarly. the Authnet module puts the CC orders in the processing state, and I want the checkmo orders to join them. (conversely, if I could make the CC orders go to the new/pending state, I can assign my custom statuses to that state) Either way, I need all new orders to be in the same state upon creation regardless of invoice or shipment existence.

thanks

note: these related questions do not quite address this issue : 6095096, 6415547, 4170628)

1

1 Answers

3
votes

The important action of setting the order status is handled in Mage_Sales_Model_Order_Payment::Place():

...
$orderState = Mage_Sales_Model_Order::STATE_NEW;
...
$orderStatus = $methodInstance->getConfigData('order_status');
...
$order->setState($orderState, $orderStatus, $message, $isCustomerNotified);
...

However, in Mage_Sales_Model_Order::setState(), it doesn't check for relationship between order state and order status. Hence, the weird hybrid.

There are many ways to work around this, one is to add observer for event 'checkout_type_onepage_save_order_after' and reset the status there. My preferred method is to add a callback in the payment method model:

class Celera_AaCredit_Model_Payment extends Mage_Payment_Model_Method_Abstract
{
protected $_code  = 'aa_credit'; 
protected $_isInitializeNeeded = true; //Required for the initialize() callback to happen 

/* Workaround to assign the correct order state to the corresponding status set in system config */

/**
 * Invoke in Mage_Sales_Model_Order_Payment
 * Required for the initialize() callback to happen     
 *      
 * @return string
 */                 
public function getConfigPaymentAction()
{
    return 'init'; //set flag to initialize $this after order is created and the payment is placed
}

/**
 * Update order state to system configuration 
 *
 * @return Mage_Payment_Model_Method_Abstract
*/ 
public function initialize($action, $stateObject)
{                          
    if ($status = $this->getConfigData('order_status')) {
        $stateObject->setStatus($status);
        $state = $this->_getAssignedState($status);
        $stateObject->setState($state);
        $stateObject->setIsNotified(true);            
    }
    return $this;
}

/**
 * Get the assigned state of an order status
 *     
 * @param string order_status
 */              
protected function _getAssignedState($status)
{
    $item = Mage::getResourceModel('sales/order_status_collection')
              ->joinStates()
              ->addFieldToFilter('main_table.status', $status)
              ->getFirstItem();
    return $item->getState();
}
}