Magento – Magento API adding a custom attribute to the sales_order.list output

apimagento-1.9soap-api-v2

Looked all over the web and have only managed to pull through the api a custom field I’ve added for sales_order.info using api v1 without any problems, however, I can’t seem to get it to pull through that value when calling a LIST of orders rather than a single order – seen nothing on the web specific to that call.

I’ve created my own module fine and have simply extended the Mage_Sales_Model_Order_Api class and inside the info() function I’ve simply added:

$customerId = $order->getCustomerId();
$customer = Mage::getModel('customer/customer')->load($customerId);

$result['sp_user_id'] = $customer->custom('extra_user_id');
$result['sp_client_id'] = $customer->custom('extra_client_id');

Works perfect when calling the sales_order.info method, but on the list method I'm not getting those fields passed back to me afterwards.

EDITING AGAIN TO BUMP – Hopefully someone has an idea I am really struggling!

Best Answer

In your config.xml:

<adminhtml>
    <events>
        <sales_order_collection_load_after>
            <observers>
                <mycompany_mymodule>
                    <type>singleton</type>
                    <model>mycompany_mymodule/observer</model>
                    <method>addCustomFieldsToOrderCollection</method>
                </convert_cert>
            </observers>
        </sales_order_collection_load_after>
    </events>
</adminhtml>

Add your custom attributes in observer:

class Mycompany_Mymodule_Model_Observer
{
     public function addCustomFieldsToOrderCollection(Varien_Event_Observer $observer)
     {
         /** @var Mage_Sales_Model_Resource_Order_Collection $collection */
         $orderCollection = $observer->getEvent()->getOrderCollection();

         foreach ($orderCollection as $order) {
             /** @var Mage_Sales_Model_Order $order */
             $customerId = $order->getCustomerId();
             $customer = Mage::getModel('customer/customer')->load($customerId);

             $order->setData('sp_user_id', 'test1'); // $customer->getData('extra_user_id')
             $order->setData('sp_client_id', 'test2'); // $customer->getData('sp_client_id')
         }

         return $this;
     }
}

As a final stroke register your fields for APIv2 as well.

in you module's wsdl.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<definitions xmlns:typens="urn:{{var wsdl.name}}" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"
             name="{{var wsdl.name}}" targetNamespace="urn:{{var wsdl.name}}">
    <types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Magento">
            <import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" />
            <complexType name="salesOrderEntity">
                <all>
                    <element name="sp_user_id" type="xsd:string" minOccurs="0" />
                    <element name="sp_client_id" type="xsd:string" minOccurs="0" />
                </all>
            </complexType>
            <complexType name="salesOrderListEntity">
                <all>
                    <element name="sp_user_id" type="xsd:string" minOccurs="0" />
                    <element name="sp_client_id" type="xsd:string" minOccurs="0" />
                </all>
            </complexType>
        </schema>
    </types>
</definitions>

And wsi.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions xmlns:typens="urn:{{var wsdl.name}}"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  name="{{var wsdl.name}}"
                  targetNamespace="urn:{{var wsdl.name}}">
    <wsdl:types>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:{{var wsdl.name}}">
            <xsd:complexType name="salesOrderEntity">
                <xsd:sequence>
                    <xsd:element name="sp_user_id" type="xsd:string" minOccurs="0" />
                    <xsd:element name="sp_client_id" type="xsd:string" minOccurs="0" />
                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="salesOrderListEntity">
                <xsd:sequence>
                    <xsd:element name="sp_user_id" type="xsd:string" minOccurs="0" />
                    <xsd:element name="sp_client_id" type="xsd:string" minOccurs="0" />
                </xsd:sequence>
            </xsd:complexType>
    </wsdl:types>
</wsdl:definitions>