0

I am completely lost with XML and I need to generate an XML with a Woocommerce plugin.

For example I'm trying to add this line :

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

into this php script :

/**
 * 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;
}

It is a plugin that we would use to generate orders and send them through a software that we use to process the orders and the shipping. The system needs to receive the XML file in a very specific format.

Thanks a lot for your help!

Robert Miller
  • 163
  • 1
  • 4
  • 11
  • What format is that specific format, do you just want to display(echo) xml on your webpage? – Olavi Sau Nov 05 '15 at 04:49
  • Hi Olavi! Actually the plugin generates an XML file, however there are some elements missing in that XML file such as the line . I'm not so sure how to have this line added automatically when generating the file. – Robert Miller Nov 05 '15 at 04:50

1 Answers1

0

The quick and dirty applies to here, stolen from: Need to write at beginning of file with PHP

<?php
$file_data = '<?xml-stylesheet type="text/xsl" href="commande.xsl"?>';
$file_data .= file_get_contents('YOURXMLPATH');
file_put_contents('YOURXMLPATH', $file_data);
?>
Community
  • 1
  • 1
Olavi Sau
  • 1,647
  • 13
  • 20