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>
This looks like a bug to me.
I noticed that the initialize
method in Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper
sets category_ids
to an empty array if category_ids
is not already present in the API request. So not providing category_ids
in the API request has the same effect as setting it to an empty array.
My workaround, until a proper fix is (hopefully) released, is to extend the Magento\Catalog\Model\ProductRepository
class and override the initializeProductData
method, and remove category_ids
altogether if it's empty. Your new ProductRepository
class then needs to be the preferred Magento\Catalog\Api\ProductRepositoryInterface
implementation.
The overriding method in my ProductRepository
class looks like this:
protected function initializeProductData(array $productData, $createNew)
{
$product = parent::initializeProductData($productData, $createNew);
$categoryIds = $product->getData('category_ids');
if ( empty($categoryIds) )
{
$product->unsetData('category_ids');
}
return $product;
}
And my di.xml has:
<preference for="Magento\Catalog\Api\ProductRepositoryInterface" type="My\Namespace\ProductRepository" />
If anyone have a nicer workaround/solution, please share.
(I have opened an issue about this, but have not received any feedback yet: https://github.com/magento/magento2/issues/3354)
Best Answer
We need to add
custom_attributes
:Payload: