You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce订单转XML插件问题:无法写入商品信息

问题诊断与修复方案

你的代码能生成XML文件但商品信息缺失,核心原因有两个:

  1. 未过滤无效订单项:$order->get_items()默认返回所有类型的订单项(包括优惠券、运费),这类项没有关联产品,会导致后续商品字段无法写入。
  2. 未校验产品对象有效性:当$item->get_product()返回null时,后续添加商品字段的代码会静默失败,直接跳过节点生成。

同时你的代码还缺少期望XML结构中的多个必填字段,以下是修复并补充完整后的代码:

function export_order_to_xml($order_id) {
    $order = wc_get_order($order_id);
    if (!$order) return; // 校验订单对象是否有效

    $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();
    $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><order></order>');
    
    // 填充基础订单信息(完全匹配期望结构)
    $xml->addChild('ordernumber', $order->get_order_number());
    $xml->addChild('lastname', $order_data['billing']['last_name']);
    $xml->addChild('adress', $order_data['billing']['address_1']);
    $xml->addChild('zipcode', $order_data['billing']['postcode']);
    $xml->addChild('city', $order_data['billing']['city']);
    $xml->addChild('phone', $order_data['billing']['phone']);
    $xml->addChild('email', $order_data['billing']['email']);
    $xml->addChild('date', $order_data['date_created']->format('Y-m-d H:i:s'));
    $xml->addChild('location', '1');
    $xml->addChild('customer_id', $order->get_user_id());
    $xml->addChild('payd', $order->is_paid() ? 'ja' : 'nee'); // 可根据实际支付状态动态调整
    $xml->addChild('method', $order->get_payment_method_title() ?: 'cash'); // 取实际支付方式,默认cash
    
    // 生成商品节点:仅处理真实商品项
    $order_items_element = $xml->addChild('orderitems');
    foreach ($order->get_items('line_item') as $item_id => $item) {
        $product = $item->get_product();
        if (!$product) continue; // 跳过无关联产品的无效项
        
        $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', number_format($order->get_item_total($item, false), 2, '.', ''));
        $item_element->addChild('qty', $item->get_quantity());
    }
    
    return $xml->asXML();
}

function generate_filename($order) {
    $order_number = $order->get_order_number();
    $pickup_date = date('Ymd', strtotime($order->get_date_created()));
    $location = '1';
    $customer_id = $order->get_user_id();
    $filename = "{$pickup_date}{$order_number}_{$location}_{$customer_id}.xml";
    return $filename;
}

关键改动说明

  • 过滤订单项类型:用$order->get_items('line_item')仅遍历真实商品,排除优惠券、运费等干扰项。
  • 产品对象校验:添加if (!$product) continue;跳过无效项,确保只生成有效商品节点。
  • 补全缺失字段:完全对齐你期望的XML结构,包括地址、联系方式、支付状态等字段。
  • 细节优化:用get_order_number()获取官方订单号,价格保留两位小数,日期格式标准化。

内容的提问来源于stack exchange,提问作者Bent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 03:37:15