我正在创建一个插件,它为每个订单创建一个带有结构的 XML 文件。但我无法将产品信息添加到订单中。我真的不知道我做错了什么。
我当前的代码保存文件并将数据放入其中,但不放入产品。
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;
}
这应该是结构:
<?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>
我用不同的方法得到了解决方案,它似乎有效。我仍然不知道我在第一个代码中做错了什么。但有时重新开始是找到解决方案的好方法。
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
}