WooCommerce 客户订单 xml 导出套件插件

WooCommerce Customer Order xml export suite plugin

我使用 woocommerce-customer-order-xml-export-suite 插件并尝试自定义输出 xml。我有 90% 的理解,这是给我带来问题的最后一部分。基本上输出 xml 必须如下所示:

Order.xml

            <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>

这是我现在得到的:

我的Order.xml

            <ItemOut Quantity="">
                <ItemID>
                    <SupplierPartID>S1072-35</SupplierPartID>
                    <Quantity>1</Quantity>
                </ItemID>
                <ItemDetail>
                    <UnitPrice>
                        <SupplierPartID>S1072-35</SupplierPartID>
                        <Quantity>1</Quantity>
                    </UnitPrice>
                </ItemDetail>
            </ItemOut>
        </OrderRequest>
    </Request>
</cXML>

这是我当前的 PHP 代码:

                        '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 ),
                                ),
                            ),

                    ), // End OrderRequest Array
                ), //End Main Request Array
            ), // End Request

    );
}
add_filter( 'wc_customer_order_xml_export_suite_order_export_order_list_format', 'wc_sample_xml_order_list_format', 10, 2 );

/**
 * Adjust the individual line item format
 *
 * @since 1.0
 * @param object $order \WC_Order instance
 * @return array
 */
function wc_sample_xml_get_line_items( $order ) {

    foreach( $order->get_items() as $item_id => $item_data ) {

        $product = $order->get_product_from_item( $item_data );

        $ItemID[] = array(
            'SupplierPartID'    => $product->get_sku(),
            'Quantity'          => $item_data['qty'],
        );
        $ItemDetail[] = array(
            'Money'             => $product->get_price(),
        );
    }

    return $ItemID;

}

这是我遇到问题的 php 代码的最后一部分。任何帮助,将不胜感激。

好的,所以您的 ItemID 和 ItemDetail/UnitPrice 都显示相同的内容,因为它们正在调用相同的函数 wc_sample_xml_get_line_items。该函数的 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 ),
    ),

该函数的结果是 return 值,

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
}

我怀疑您希望 $ItemDetail 也被 returned 设置为 UnitPrice,但实际情况是您创建了 $ItemDetail ,但是,一旦函数完成,该变量就消失了,因为它没有作为 return 值传递...并且您的函数已经具有其他值的 return 值。

您需要的是一个类似于第一个的新函数,但 return 是金钱详细信息而不是 ItemID 详细信息:

function wc_sample_xml_get_line_item_unit_prices( $order ) {

    foreach( $order->get_items() as $item_id => $item_data ) {

        $product = $order->get_product_from_item( $item_data );

        $ItemDetail[] = array(
            'Money'             => $product->get_price(),
        );
    }

    return $ItemDetail;    
}

然后您可以在创建数组时使用它:

'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 输出

要让 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_data 甚至 OrderRequest,因此我不确定当您的订单中有多个订单项时它的结构是否正确。如果可行,请务必测试包含多个订单项的订单并根据需要进行调整。