WP All Export自定义XML订单行循环报错求助
自定义订单XML模板报错问题排查与修复
问题背景
现有自定义订单XML模板,仅导出订单主体信息时可正常生成所有客户订单,但添加订单行详情后出现报错,目标是生成包含多订单行的<ns1:Lines>结构(示例见下文)。
原模板代码
<?xml version="1.0" encoding="UTF-8"?> <ns1:root xmlns:ns1="https://web.one.com"> <ns1:Orders> <!-- BEGIN LOOP --> <ns1:Order ns1:ownCode="{Order ID}"> <ns1:Customer>000000</ns1:Customer> <ns1:Date>[date("Y-m-d")]</ns1:Date> <ns1:ServiceDate>{Completed Date}</ns1:ServiceDate> <ns1:Addressee> <ns1:Name>{Shipping First Name} {Shipping Last Name}</ns1:Name> <ns1:Phone>{Shipping Phone}</ns1:Phone> <ns1:ShippingAddr> <ns1:Address>{Shipping Address 1} {Shipping Address 2}</ns1:Address> <ns1:ZipCode>{Shipping Postcode}</ns1:ZipCode> <ns1:Location>{Shipping City}</ns1:Location> <ns1:Province>{Shipping State}</ns1:Province> <ns1:Country>{Shipping Country}</ns1:Country> </ns1:ShippingAddr> </ns1:Addressee> <ns1:Anonymous>"true"</ns1:Anonymous> <ns1:RemarksLabel>"CompanyLabel"</ns1:RemarksLabel> <ns1:Lines> <ns1:Line ownid="{Order Line ID}"> <ns1:Product id="{SKU}"> <ns1:ProductName>{Product Name}</ns1:ProductName> <ns1:Rate>{Item Cost}</ns1:Rate> <ns1:SupplierRef>{Product ID}</ns1:SupplierRef> </ns1:Product> <ns1:Quantity>{Quantity}</ns1:Quantity> <ns1:Amount>{Item Total}</ns1:Amount> </ns1:Line> </ns1:Lines> </ns1:Order> <!-- END LOOP --> </ns1:Orders> </ns1:root>
期望的订单行格式
<ns1:Lines> <ns1:Line ns1:ownid="19998"> <ns1:Product ns1:id="1"> <ns1:ProductName>Product A</ns1:ProductName> <ns1:Rate>10</ns1:Rate> <ns1:SupplierRef>10000</ns1:SupplierRef> </ns1:Product> <ns1:Quantity>1</ns1:Quantity> <ns1:Amount>10</ns1:Amount> </ns1:Line> <ns1:Line ns1:ownid="20000"> <ns1:Product ns1:id="2"> <ns1:ProductName>Product A</ns1:ProductName> <ns1:Rate>20</ns1:Rate> <ns1:SupplierRef>30000</ns1:SupplierRef> </ns1:Product> <ns1:Quantity>2</ns1:Quantity> <ns1:Amount>40</ns1:Amount> </ns1:Line> </ns1:Lines>
错误点分析
- 缺少订单行循环逻辑:原模板
<ns1:Lines>下仅存在单个<ns1:Line>节点,没有针对多订单行的循环标记,无法遍历生成多个订单行节点。 - 属性未加命名空间前缀:
<ns1:Line>的ownid属性、<ns1:Product>的id属性未添加ns1:前缀,违反XML命名空间规范,会触发解析报错。 - 字符串多余引号:
<ns1:Anonymous>和<ns1:RemarksLabel>节点内的"true"、"CompanyLabel"多了一层引号,会被当作字符串内容的一部分,不符合目标格式要求。
修正后的模板
<?xml version="1.0" encoding="UTF-8"?> <ns1:root xmlns:ns1="https://web.one.com"> <ns1:Orders> <!-- BEGIN ORDER LOOP --> <ns1:Order ns1:ownCode="{Order ID}"> <ns1:Customer>000000</ns1:Customer> <ns1:Date>[date("Y-m-d")]</ns1:Date> <ns1:ServiceDate>{Completed Date}</ns1:ServiceDate> <ns1:Addressee> <ns1:Name>{Shipping First Name} {Shipping Last Name}</ns1:Name> <ns1:Phone>{Shipping Phone}</ns1:Phone> <ns1:ShippingAddr> <ns1:Address>{Shipping Address 1} {Shipping Address 2}</ns1:Address> <ns1:ZipCode>{Shipping Postcode}</ns1:ZipCode> <ns1:Location>{Shipping City}</ns1:Location> <ns1:Province>{Shipping State}</ns1:Province> <ns1:Country>{Shipping Country}</ns1:Country> </ns1:ShippingAddr> </ns1:Addressee> <ns1:Anonymous>true</ns1:Anonymous> <ns1:RemarksLabel>CompanyLabel</ns1:RemarksLabel> <ns1:Lines> <!-- BEGIN ORDER LINE LOOP --> <ns1:Line ns1:ownid="{Order Line ID}"> <ns1:Product ns1:id="{SKU}"> <ns1:ProductName>{Product Name}</ns1:ProductName> <ns1:Rate>{Item Cost}</ns1:Rate> <ns1:SupplierRef>{Product ID}</ns1:SupplierRef> </ns1:Product> <ns1:Quantity>{Quantity}</ns1:Quantity> <ns1:Amount>{Item Total}</ns1:Amount> </ns1:Line> <!-- END ORDER LINE LOOP --> </ns1:Lines> </ns1:Order> <!-- END ORDER LOOP --> </ns1:Orders> </ns1:root>
修正说明
- 添加
<!-- BEGIN ORDER LINE LOOP -->和<!-- END ORDER LINE LOOP -->标记,用于遍历每个订单下的所有订单行,生成多个<ns1:Line>节点。 - 给
<ns1:Line>的ownid、<ns1:Product>的id属性添加ns1:前缀,符合XML命名空间规则。 - 移除
<ns1:Anonymous>和<ns1:RemarksLabel>节点内的多余引号,确保内容格式正确。
内容的提问来源于stack exchange,提问作者Antonio Manu
相关产品推荐
相关产品推荐

