0

I want to pass the product info after is ordered and i have created this file for testing. The foreach is not working because it show back just one product and not all of them.

Here is the code:

    $dirPath = Mage::getBaseDir('var') . DS . 'export';

    if (!is_dir($dirPath)) {
        mkdir($dirPath, 0777, true);
    }

    foreach ($order->getAllItems() as $item){

        $data = array(
            'ID'    => $item->getProductId(),
            'QTY'   => $item->getTotalQtyOrdered(),
            'PRICE' => $item->getSubtotal()

            );

    }

    $xml = new SimpleXMLElement('<root/>');
    array_walk_recursive($data, array ($xml, 'addChild'));
    file_put_contents(
    $dirPath. DS .$order->getIncrementId().'.xml', 
    $xml->asXML()

    );

2 Answers 2

0

To answer Daniel's additional question in the comment to Marius' answer: use the following code to add items to your XML

$xml = new SimpleXMLElement('<root/>');
foreach ($data as $item) {
    $xmlItem = $xml->addChild('item');
    foreach ($item as $key => $value) {
        $xmlItem->addChild($key, $value);
    }
}
// now you can use file_put_contents()
0

Wanna bet it works? :) This code

$data = array(
            'ID'    => $item->getProductId(),
            'QTY'   => $item->getTotalQtyOrdered(),
            'PRICE' => $item->getSubtotal()

            );

Should be

$data[] = array(
            'ID'    => $item->getProductId(),
            'QTY'   => $item->getTotalQtyOrdered(),
            'PRICE' => $item->getSubtotal()

            );

It shows you only one product because you keep overwriting the $data variable.

2
  • That did the trick but what i am getting now is this <?xml version="1.0"?> <root> <10000002>ID</10000002> <171.34>PRICE</171.34> <10000002>ID</10000002> <0>PRICE</0> <10000006>ID</10000006> <555>PRICE</555> <10000006>ID</10000006> <0>PRICE</0> </root> looks like dublicating
    – DanielDake
    Commented Oct 21, 2013 at 19:43
  • 2
    @DanielDake are you selling configurable products? If yes you can try with getAllVisibleItems(). Commented Oct 22, 2013 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.