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>

这是我现在得到的:

MyOrder.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。该函数的返回值保存在数组中:

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

该函数的结果是返回值

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也被返回到UnitPrice中设置,但实际发生的是你已经创建了$ItemDetail,然而,一旦函数完成,该变量就消失了,因为它没有作为返回值传递…你的函数已经有了其他东西的返回值。

您需要的是一个类似于第一个函数的新函数,但是返回money detail而不是ItemID detail:

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,所以我不确定当您在订单中有多个行项目时,它的结构是否正确。如果这种方法有效,那么一定要用多个行项测试订单,并根据需要进行调整。