0

I have an observer for the event "sales_order_place_after" that needs to take the order and transform it into a JSON object to be sent to an external system (over REST). However the JSON representation should be identical to the same order rendered by the REST API V2 (ie, only showing the attributes defined in api2.xml).

Is there any helper method that can do this?

0

first you will have to build an array as per your requirement than you can encode it be using it.

you can use below method for encoding array into json

$json = Mage::helper('core')->jsonEncode($array);
  • This will return all the order attributes but I only want to return the attributes defined in api2.xml. – Saul Karl Jun 27 '14 at 10:38
0

After looking at the API2 relevant code, it seems that there's no simple way to reuse any of the utility methods in Mage_Sales_Model_Api2_Order.

The simplest thing to do is the read api2.xml, get the attributes including those that are prefixed with an underscore:

private function getAvailableAttributes($objType) {
    $result = array();
    $resource = Mage::getSingleton('api2/config');
    if ($objType == 'order') {
        $orderAttrs = $resource->getResourceAttributes('mymodule');
        foreach ($orderAttrs as $attrName=>$attrValue) {
            preg_match('/^_([\w]+)$/', $attrName, $matches);
            if (!empty($matches)) {
                array_push($result, $matches[1]);
            }
            else {
                array_push($result, $attrName);
            }
        }
    }

    return $result;
}

Then filter the order with these attributes:

$result = array();
$orderData = $order->getData();
$attrs = $this->getAvailableAttributes('order');
foreach ($attrs as $itemName) {
    if ($itemName == 'payment_method') {
        $result['payment_method'] = $order->getPayment()->getMethodInstance()->getTitle();
    }
    else {
        $result[$itemName] = $orderData[$itemName];
    }
}

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.