2

We have a scenario where we fulfill orders via an API. Previously the logic has been tied to the invoicing process, so that when an invoice is created, the fulfillment logic is executed. If there were API errors, an order attribute was set which would then display a column in the order grid indicating that an API error occurred during fulfillment.

After refactoring and cleaning up this messy code, we got rid of "API Error" column. We were hoping to set the order status to "Pending Fulfillment" or similar when starting the fulfillment process, and having the order stay in that status in case of an error to indicate to our customer service department that the order had an error and needs manual intervention to attempt fulfillment again.

We attempted to programmatically hold the order in a "Fulfillment Failed" status, but that also did not work due to Magento completing the order. The "Fulfillment Failed" status gets set when we encounter an API error, but Magento still completes the order. Since our fulfillment function is listening for the event sales_order_invoice_pay I'm assuming this is because the invoice has already been created, and Magento thinks the order is now complete even though there were API errors.

enter image description here

The question is: how do we get Magento to stop completing the orders and leave them in the "Fulfillment Failed" status, so they can be manually reviewed?

So far, here are some ideas we've come up with: + Partially invoice each order item as its fulfillment succeeds (not really sure how to accomplish this) + Programmatically change the status AFTER Magento has completed it. Unfortunately, this also doesn't seems possibly after the order is Complete. If I can't change its status to HOLDED after it's complete manually, I'm guessing we won't be able to do it programmatically either. + Bring back the API Errors column

I'm hoping that we're overlooking something and that there may be a much simpler to implement solution for our scenario.

2

We created the status "Fulfillment Error(s)" and assigned to state COMPLETE. We set a "fulfillment error(s)" flag in the session if fulfillment fails for any item in an order. We created an event listener for the checkout_onepage_controller_success_action event to check for the "fulfillment error(s)" flag.

/**
 * Set order status to Fulfillment Failed if an error flag is set in the session
 * Inspiration from: http://www.atwix.com/magento/auto-invoice-and-custom-order-status-upon-checkout/
 *
 * @param Varien_Event_Observer
 * @event checkout_onepage_controller_success_action
 */
public function checkFulfillmentErrors($observer)
{
    // Get order from observer
    $orderId = $observer->getEvent()->getOrderIds();
    $order = Mage::getModel('sales/order')->load($orderId);

    // Get and unset fulfillment errors flag from core session
    $fulfillmentErrors = Mage::getSingleton('core/session')->getFulfillmentErrors();
    Mage::getSingleton('core/session')->unsFulfillmentErrors();

    // Set status to Fulfillment Error(s) if flag was set in session
    if ($fulfillmentErrors) {
        //$order->setStatus('fulfillment_failed');
        $order->addStatusHistoryComment('Fulfillment failed for one or more items', 'fulfillment_errors');
        $order->save();
    }

    return $observer;
}

By this point, Magento has already put the order in Complete status. We set the "Fulfillment Error(s)" status after Magento has "completed" the order, which is now being displayed correctly in the orders grid.

I still don't think this is the best solution since the order status history shows Magento completing the order (because an invoice for it had already been created), then a newer "Fulfillment Error(s)" status, but it'll have to do for now until we find a better solution.

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.