将XML添加到PHP插件中

时间:2015-11-05 04:44:49

标签: php xml wordpress woocommerce

我完全迷失了XML,我需要使用Woocommerce插件生成XML。

例如,我正在尝试添加此行:

<?xml-stylesheet type="text/xsl" href="commande.xsl"?>

进入这个php脚本:

/**
 * Adjust the root-level XML format
 *
 * @since 1.0
 * @param array $orders_format existing order array format
 * @param array $orders array \WC_Order
 * @return array
 */

function wc_sample_xml_order_format( $orders_format, $orders ) {
$orders_format = array(

'COMMANDE' => array(
        '@attributes' => array(

            ),
        'ENTETE' => $orders,

        ),
    );
    return $orders_format;
}
add_filter( 'wc_customer_order_xml_export_suite_order_export_format', 'wc_sample_xml_order_format', 10, 2 );
/**
 * Adjust the individual order format
 *
 * @since 1.0
 * @param array $order_format existing order array format
 * @param object $order \WC_Order instance
 * @return array
 */
function wc_sample_xml_order_list_format( $order_format, $order ) {
    return array(
        'NO_COMMANDE' => $order->id,
        'NumericTime' => strtotime( $order->order_date ),
        'Origin'      => 'Local',
        'AddressInfo' => array(
            0 => array(
                '@attributes' => array( 'type' => 'ship' ),
                'NOM_CLIENT'        => $order->shipping_first_name . ' ' . $order->shipping_last_name,
                'ADRESSE_1'    => $order->shipping_address_1,
                'ADRESSE_2'    => $order->shipping_address_2,
                'VILLE'        => $order->shipping_city,
                'PROVINCE'       => $order->shipping_state,
                'PAYS'     => $order->shipping_country,
                'CODE_POSTAL'         => $order->shipping_postcode,
                'TELEPHONE'       => $order->billing_phone,
                'COURRIEL'       => $order->billing_email,
            ),
            1 => array(
                '@attributes' => array( 'type' => 'bill' ),
                'Name'        => array(
                    'First' => $order->billing_first_name,
                    'Last'  => $order->billing_last_name,
                    'Full'  => $order->billing_first_name . ' ' . $order->billing_last_name,
                ),
                'Address1'    => $order->billing_address_1,
                'Address2'    => $order->billing_address_2,
                'City'        => $order->billing_city,
                'State'       => $order->billing_state,
                'Country'     => $order->billing_country,
                'Zip'         => $order->billing_postcode,
                'Phone'       => $order->billing_phone,
                'Email'       => $order->billing_email,
            ),
        ),
        'Shipping'    => $order->get_shipping_method(),
        'Comments'    => $order->customer_note,
        'Items'       => wc_sample_xml_get_line_items( $order ),
        'Total'       => array(
            'Line' => array(
                0 => array(
                    '@attributes' => array( 'type' => 'Coupon', 'name' => 'Discount' ),
                    '@value'      => $order->get_total_discount()
                ),
                1 => array(
                    '@attributes' => array( 'type' => 'Shipping', 'name' => 'Shipping' ),
                    '@value'      => $order->get_total_shipping()
                ),
                2 => array(
                    '@attributes' => array( 'type' => 'Tax', 'name' => 'Tax' ),
                    '@value'      => $order->get_total_tax()
                ),
                3 => array(
                    '@attributes' => array( 'type' => 'Total', 'name' => 'Total' ),
                    '@value'      => $order->get_total()
                )
            ),
        ),
        'PONumber' => get_post_meta( $order->id, 'PO Number', true ), // add your own order meta here
    );
}
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 );
        $items[] = array(
            'Id'                => $product->get_sku(),
            'Quantity'          => $item_data['qty'],
            'Unit-Price'        => $product->get_price(),
            'Description'       => $product->get_title(),
            'Cost'              => woocommerce_get_order_item_meta( $item_id, '_wc_cog_cost', true ),
            'Url'               => set_url_scheme( get_permalink( $product->id ), 'http' ),
            'Taxable'           => ( $product->is_taxable() ) ? 'YES' : 'NO'
        );
    }
    return $items;
}

这是一个插件,我们将用它来生成订单并通过我们用来处理订单和发货的软件发送它们。系统需要以非常特定的格式接收XML文件。

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

快速而肮脏适用于此处,被盗:Need to write at beginning of file with PHP

ga:contentGroupXX