Search code examples
phpxmlwoocommerce

Woocommerce order to a XML file saving each product


I am creating a plugin that creates a XML file with a structure for each order. But I am not able to get the product info in to the order. I dont really know what I am doing wrong.

My current code that saves the file and puts the data in it but not the product.

function export_order_to_xml($order_id) {
    $order = wc_get_order($order_id);
    $xml_data = generate_xml_data($order);

    $filename = generate_filename($order);
    $plugin_dir = plugin_dir_path(__FILE__);
    $xml_directory = $plugin_dir . 'xml_files/';
    if (!file_exists($xml_directory)) {
        mkdir($xml_directory, 0755, true);
    }
    $filepath = $xml_directory . $filename;
    file_put_contents($filepath, $xml_data);
}

function generate_xml_data($order) {
    $order_data = $order->get_data();
    // Generate XML structure
    $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><order></order>');
    $xml->addChild('ordernumber', $order_data['id']); // Order ID
    $xml->addChild('name', $order_data['billing']['first_name']);
    $xml->addChild('lastname', $order_data['billing']['last_name']);
    // Add order items
    $order_items_element = $xml->addChild('orderitems');
    foreach ($order->get_items() as $item_id => $item) {
        $product = $item->get_product();
        $item_element = $order_items_element->addChild('item');
        $item_element->addChild('product_id', $product->get_id());
        $item_element->addChild('name', $product->get_name());
        $item_element->addChild('price', $order->get_item_total($item, false));
        $item_element->addChild('qty', $item->get_quantity());
    }
    return $xml->asXML();
}

function generate_filename($order) {
    $order_data = $order->get_data();
    $order_number = $order_data['id'];
    $pickup_date = date('Ymd', strtotime($order_data['date_created']));
    $location = '1';
    $customer_id = $order->get_user_id();
    $filename = "{$pickup_date}{$order_number}_{$location}_{$customer_id}.xml";
    return $filename;
}

This should be the strucute:

<?xml version="1.0" encoding="UTF-8"?>
<order>
 <ordernumber></ordernumber>
 <lastname></lastname>
 <adress></adress>
 <zipcode></zipcode>
 <city></city>
 <phone></phone>
 <email></email>
 <date></date>
 <location>1</location>
 <customer_id/>
 <payd>nee</payd>
 <method>cash</method>
 <orderitems>
 <item>
 <product_id>252</product_id>
 <name>Item</name>
 <price>2.30</price>
 <qty>3</qty>
 </item>
 </orderitems>
</order>

Solution

  • I got the solution with a different approach it seems to work. I still dont really know what I did wrong in my first code. But sometimes starting over is a great way to find the solution.

    function generate_xml_from_order($order_id) {
        // Get the order object
        $order = wc_get_order($order_id);
    
        // Initialize an empty array to store order items
        $order_items = array();
    
        // Loop through order items
        foreach ($order->get_items() as $item_id => $item) {
            // Get item data
            $product_id = $item->get_product_id();
            $product = $item->get_product();
            $name = $product->get_name();
            $price = $product->get_price();
            $quantity = $item->get_quantity();
            $note = ''; // Add logic to get order item notes if available
    
            // Add item data to the order items array
            $order_items[] = array(
                'product_id' => $product_id,
                'name' => $name,
                'price' => $price,
                'quantity' => $quantity,
                'note' => $note,
            );
        }
    
        // Construct XML
        $xml = new SimpleXMLElement('<order></order>');
        $xml->addChild('bestelnummer', 'BMO.' . $order_id); // Generate a unique order number
        $xml->addChild('voornaam', $order->get_billing_first_name());
        $xml->addChild('achternaam', $order->get_billing_last_name());
        // Add other customer details similarly
        // ...
        $xml->addChild('orderitems');
    
        foreach ($order_items as $item) {
            $order_item = $xml->orderitems->addChild('item');
            $order_item->addChild('product_id', $item['product_id']);
            $order_item->addChild('name', $item['name']);
            $order_item->addChild('price', $item['price']);
            $order_item->addChild('qty', $item['quantity']);
            // Add logic to include notes if available
        }