简体   繁体   中英

Loop and write out order items in XML - Woocommerce

Writing a plugin for Woocommerce that write out order information in XML. Have everything in place except the order item information that i need to loop and write out.

Have been trying for hours now but guess my lack of php skill somehow make me miss the obvious.

This is the code to return the data

  /**
  * Return an array of items/products within this order.
  *
  * @param string|array $type Types of line items to get (array or string)
  * @return array
  */
       function get_items( $type = '' ) {
     global $wpdb;

     if ( empty( $type ) ) {
         $type = array( 'line_item' );
     }

     if ( ! is_array( $type ) ) {
         $type = array( $type );
     }

     $type = array_map( 'esc_attr', $type );

     $line_items = $wpdb->get_results( $wpdb->prepare( "
         SELECT      order_item_id, order_item_name, order_item_type
         FROM        {$wpdb->prefix}woocommerce_order_items
         WHERE       order_id = %d
         AND         order_item_type IN ( '" . implode( "','", $type ) . "' )
         ORDER BY    order_item_id
     ", $this->id ) );

     $items = array();

     // Reserved meta keys
     $reserved_item_meta_keys = array(
         'name',
         'type',
         'item_meta',
         'qty',
         'tax_class',
         'product_id',
         'variation_id',
         'line_subtotal',
         'line_total',
         'line_tax',
         'line_subtotal_tax'
     );

     // Loop items
     foreach ( $line_items as $item ) {

         // Place line item into array to return
         $items[ $item->order_item_id ]['name']      = $item->order_item_name;
         $items[ $item->order_item_id ]['type']      = $item->order_item_type;
         $items[ $item->order_item_id ]['item_meta'] = $this->get_item_meta( $item->order_item_id );

         // Expand meta data into the array
         if ( $items[ $item->order_item_id ]['item_meta'] ) {
             foreach ( $items[ $item->order_item_id ]['item_meta'] as $name => $value ) {

                 if ( in_array( $name, $reserved_item_meta_keys ) ) {
                     continue;
                 }

                 if ( '_' === substr( $name, 0, 1 ) ) {
                     $items[ $item->order_item_id ][ substr( $name, 1 ) ] = $value[0];
                 } elseif ( ! in_array( $name, $reserved_item_meta_keys ) ) {
                     $items[ $item->order_item_id ][ $name ] = make_clickable( $value[0] );
                 }
             }
         }
     }

     return apply_filters( 'woocommerce_order_get_items', $items, $this );

 }

And then simply trying output loop and display displaying name and quantity.

 foreach {
 $this->xml->writeElement('Name', $item->name);
 $this->xml->writeElement('Quantity', $item->qty);
 }

Would be happy if someone could give me a hand on this so that i will understand it.

Solved it and this was my solution

    $order = new WC_Order( $order_id ); 
    $items = $order->get_items();


    foreach ( $items as $item ) {
    $product_name = $item['name'];
    $product_id = $item['product_id'];
    $product_variation_id = $item['variation_id'];

    // Output XML
    $this->xml->writeElement('Shi3pmentDate', $product_name);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM