Php WooCommerce客户订单xml导出套件插件
我正在使用woocommerce客户订单xml导出套件插件并尝试自定义输出xml。如果90%的人都明白了,这是给我带来问题的最后一部分。基本上,输出xml必须如下所示: Order.xmlPhp WooCommerce客户订单xml导出套件插件,php,xml,wordpress,woocommerce,Php,Xml,Wordpress,Woocommerce,我正在使用woocommerce客户订单xml导出套件插件并尝试自定义输出xml。如果90%的人都明白了,这是给我带来问题的最后一部分。基本上,输出xml必须如下所示: Order.xml <ItemOut quantity="1" lineNumber="1"> <ItemID> <SupplierPartID>GPS-2215RB</SupplierPar
<ItemOut quantity="1" lineNumber="1">
<ItemID>
<SupplierPartID>GPS-2215RB</SupplierPartID>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="USD">105.99</Money>
</UnitPrice>
</ItemDetail>
</ItemOut>
</OrderRequest>
</Request>
</cXML>
这是我遇到问题的php代码的最后一部分。任何帮助都将不胜感激 好的,那么您的ItemID和ItemDetail/UnitPrice都显示了相同的内容,因为它们调用了相同的函数wc\u sample\u xml\u get\u line\u items。该函数的返回值保存在数组中:
'ItemOut' => array(
'@attributes' => array('Quantity' => $item_data['qty'] ),
'ItemID' => wc_sample_xml_get_line_items( $order ),
'ItemDetail' => array(
'UnitPrice' => wc_sample_xml_get_line_items( $order ),
),
'ItemOut' => array(
'@attributes' => array('Quantity' => $item_data['qty'] ),
'ItemID' => wc_sample_xml_get_line_items( $order ),
'ItemDetail' => array(
'UnitPrice' => wc_sample_xml_get_line_item_unit_prices( $order ),
),
该函数的结果是返回值
function wc_sample_xml_get_line_items( $order ) {
<...snipped irrelevant stuff...>
$ItemID[] = array(
'SupplierPartID' => $product->get_sku(),
'Quantity' => $item_data['qty'],
);
$ItemDetail[] = array(
'Money' => $product->get_price(),
);
}
return $ItemID; # This is what's returned
}
然后,您可以在创建阵列时使用它:
'ItemOut' => array(
'@attributes' => array('Quantity' => $item_data['qty'] ),
'ItemID' => wc_sample_xml_get_line_items( $order ),
'ItemDetail' => array(
'UnitPrice' => wc_sample_xml_get_line_items( $order ),
),
'ItemOut' => array(
'@attributes' => array('Quantity' => $item_data['qty'] ),
'ItemID' => wc_sample_xml_get_line_items( $order ),
'ItemDetail' => array(
'UnitPrice' => wc_sample_xml_get_line_item_unit_prices( $order ),
),
编辑:
获取项目输出
要获得ItemOut以生成所需的XML,最好是放弃wc_sample_XML_get_line_item_unit_prices并创建一个函数来生成整个ItemOut,而不是零零碎碎地进行。下面是生成的ItemOut分配的外观:
'ItemOut' => wc_sample_xml_get_item_out( $order ),
注意所有的ItemId、ItemDetails和所有这些都消失了。我们想在wc_sample_xml_get_item_out中创建它。所以,这里是:
function wc_sample_xml_get_line_items( $order ) {
$itemOut = array ();
// Keep track of the line Item number
$lineNumber = 1;
// Build a list of itemOut's from the line items in the order
foreach($order->get_items() as $item_id => $item_data ) {
$product = $order->get_product_from_item( $item_data );
// The idea here is that we'll generate each XML child node individually, then combine them into one ItemOut node afterwards.
// So these will go in <itemout>'s attributes
$newAttributes = array(
'quantity' => $item_data['qty'],
'lineNumber' => $lineNumber
);
// This is ItemId
$newItemId = array(
'SupplierPartID' => $product->get_sku()
);
// and ItemDetail
$newItemDetail = array (
'UnitPrice' => array (
'Money' => $product->get_price()
)
);
// Now we combine these into the ItemOut
$newItemOut = array(
'@attributes' => $newAttributes,
'ItemId' => $newItemId,
'itemDetail' => $newItemDetail
);
// Now we tack this new item out onto the array
$itemOut[] = $newItemOut;
// Set the next line number, and then loop until we've done each line item
$lineNumber++;
}
return $itemOut;
}
我很肯定这会让你达到目的或者非常接近目的。由于我无法访问您的$order和$item_数据,甚至无法访问OrderRequest,因此我不确定当您在一个订单中有多个行项目时,它的结构是否正确。如果可行,一定要用多行项目测试订单,并根据需要进行调整。我建议尽量减少代码和代码片段,以将已经工作的部分与不工作的部分隔离开来。对于那些试图帮助别人的人来说,这会减少开销。看,谢谢,我删除了不必要的代码。希望这个答案不是出于屈尊俯就,但我正试图让答案尽可能清楚。这是可行的,而且它不是出于屈尊俯就。另一个问题是,如何获得显示在属性中的数量和行号?好的,我将编辑答案,添加一个关于如何做的新部分。这是一个多一点的工作,但值得。嘿,谢谢你的编辑,$lineNumber++;正在引发此错误:Parse error:syntax error,意外的“$lineNumber”T_变量遗漏了上一条语句中缺少的分号$itemOut[]=$newItemOut。将来,如果语法错误困扰您,请尝试复制并粘贴到类似的网站。
function wc_sample_xml_get_line_items( $order ) {
$itemOut = array ();
// Keep track of the line Item number
$lineNumber = 1;
// Build a list of itemOut's from the line items in the order
foreach($order->get_items() as $item_id => $item_data ) {
$product = $order->get_product_from_item( $item_data );
// The idea here is that we'll generate each XML child node individually, then combine them into one ItemOut node afterwards.
// So these will go in <itemout>'s attributes
$newAttributes = array(
'quantity' => $item_data['qty'],
'lineNumber' => $lineNumber
);
// This is ItemId
$newItemId = array(
'SupplierPartID' => $product->get_sku()
);
// and ItemDetail
$newItemDetail = array (
'UnitPrice' => array (
'Money' => $product->get_price()
)
);
// Now we combine these into the ItemOut
$newItemOut = array(
'@attributes' => $newAttributes,
'ItemId' => $newItemId,
'itemDetail' => $newItemDetail
);
// Now we tack this new item out onto the array
$itemOut[] = $newItemOut;
// Set the next line number, and then loop until we've done each line item
$lineNumber++;
}
return $itemOut;
}