繁体   English   中英

以XML循环并写出订单商品-Woocommerce

[英]Loop and write out order items in XML - Woocommerce

为Woocommerce编写一个插件,以XML格式写出订单信息。 除了我需要循环和写出的订购商品信息外,其他所有东西都就位。

现在已经尝试了几个小时,但猜想我缺乏php技能使我很想念。

这是返回数据的代码

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

 }

然后只需尝试输出循环并显示显示名称和数量。

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

如果有人可以帮我解决这个问题,使我理解,那将很高兴。

解决了,这是我的解决方案

    $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);

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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