1 <?php
  2 /**
  3  * Order Data
  4  *
  5  * Functions for displaying the order items meta box.
  6  *
  7  * @author      WooThemes
  8  * @category    Admin
  9  * @package     WooCommerce/Admin/Meta Boxes
 10  * @version     2.1.0
 11  */
 12 
 13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 14 
 15 class WC_Meta_Box_Order_Items {
 16 
 17     /**
 18      * Output the metabox
 19      */
 20     public static function output( $post ) {
 21         global $wpdb, $thepostid, $theorder, $woocommerce;
 22 
 23         if ( ! is_object( $theorder ) )
 24             $theorder = new WC_Order( $thepostid );
 25 
 26         $order = $theorder;
 27         ?>
 28         <div class="woocommerce_order_items_wrapper">
 29             <table cellpadding="0" cellspacing="0" class="woocommerce_order_items">
 30                 <thead>
 31                     <tr>
 32                         <th><input type="checkbox" class="check-column" /></th>
 33                         <th class="item" colspan="2"><?php _e( 'Item', 'woocommerce' ); ?></th>
 34 
 35                         <?php do_action( 'woocommerce_admin_order_item_headers' ); ?>
 36 
 37                         <?php if ( get_option( 'woocommerce_calc_taxes' ) == 'yes' ) : ?>
 38                             <th class="tax_class"><?php _e( 'Tax&nbsp;Class', 'woocommerce' ); ?></th>
 39                         <?php endif; ?>
 40 
 41                         <th class="quantity"><?php _e( 'Qty', 'woocommerce' ); ?></th>
 42 
 43                         <th class="line_cost"><?php _e( 'Total', 'woocommerce' ); ?></th>
 44 
 45                         <?php if ( get_option( 'woocommerce_calc_taxes' ) == 'yes' ) : ?>
 46                             <th class="line_tax"><?php _e( 'Tax', 'woocommerce' ); ?></th>
 47                         <?php endif; ?>
 48 
 49                         <th width="1%">&nbsp;</th>
 50                     </tr>
 51                 </thead>
 52                 <tbody id="order_items_list">
 53 
 54                     <?php
 55                         // List order items
 56                         $order_items = $order->get_items( apply_filters( 'woocommerce_admin_order_item_types', array( 'line_item', 'fee' ) ) );
 57 
 58                         foreach ( $order_items as $item_id => $item ) {
 59 
 60                             switch ( $item['type'] ) {
 61                                 case 'line_item' :
 62                                     $_product   = $order->get_product_from_item( $item );
 63                                     $item_meta  = $order->get_item_meta( $item_id );
 64 
 65                                     include( 'views/html-order-item.php' );
 66                                 break;
 67                                 case 'fee' :
 68                                     include( 'views/html-order-fee.php' );
 69                                 break;
 70                             }
 71 
 72                             do_action( 'woocommerce_order_item_' . $item['type'] . '_html', $item_id, $item );
 73                         }
 74                     ?>
 75                 </tbody>
 76             </table>
 77         </div>
 78 
 79         <p class="bulk_actions">
 80             <select>
 81                 <option value=""><?php _e( 'Actions', 'woocommerce' ); ?></option>
 82                 <optgroup label="<?php _e( 'Edit', 'woocommerce' ); ?>">
 83                     <option value="delete"><?php _e( 'Delete Lines', 'woocommerce' ); ?></option>
 84                 </optgroup>
 85                 <optgroup label="<?php _e( 'Stock Actions', 'woocommerce' ); ?>">
 86                     <option value="reduce_stock"><?php _e( 'Reduce Line Stock', 'woocommerce' ); ?></option>
 87                     <option value="increase_stock"><?php _e( 'Increase Line Stock', 'woocommerce' ); ?></option>
 88                 </optgroup>
 89             </select>
 90 
 91             <button type="button" class="button do_bulk_action wc-reload" title="<?php _e( 'Apply', 'woocommerce' ); ?>"><span><?php _e( 'Apply', 'woocommerce' ); ?></span></button>
 92         </p>
 93 
 94         <p class="add_items">
 95             <select id="add_item_id" class="ajax_chosen_select_products_and_variations" multiple="multiple" data-placeholder="<?php _e( 'Search for a product&hellip;', 'woocommerce' ); ?>" style="width: 400px"></select>
 96 
 97             <button type="button" class="button add_order_item"><?php _e( 'Add item(s)', 'woocommerce' ); ?></button>
 98             <button type="button" class="button add_order_fee"><?php _e( 'Add fee', 'woocommerce' ); ?></button>
 99         </p>
100         <div class="clear"></div>
101         <?php
102     }
103 
104     /**
105      * Save meta box data
106      */
107     public static function save( $post_id, $post ) {
108         global $wpdb;
109 
110         // Order items + fees
111         $subtotal = 0;
112         $total    = 0;
113 
114         if ( isset( $_POST['order_item_id'] ) ) {
115 
116             $get_values = array( 'order_item_id', 'order_item_name', 'order_item_qty', 'line_subtotal', 'line_subtotal_tax', 'line_total', 'line_tax', 'order_item_tax_class' );
117 
118             foreach( $get_values as $value )
119                 $$value = isset( $_POST[ $value ] ) ? $_POST[ $value ] : array();
120 
121             foreach ( $order_item_id as $item_id ) {
122 
123                 $item_id = absint( $item_id );
124 
125                 if ( isset( $order_item_name[ $item_id ] ) )
126                     $wpdb->update(
127                         $wpdb->prefix . "woocommerce_order_items",
128                         array( 'order_item_name' => wc_clean( $order_item_name[ $item_id ] ) ),
129                         array( 'order_item_id' => $item_id ),
130                         array( '%s' ),
131                         array( '%d' )
132                     );
133 
134                 if ( isset( $order_item_qty[ $item_id ] ) )
135                     wc_update_order_item_meta( $item_id, '_qty', apply_filters( 'woocommerce_stock_amount', $order_item_qty[ $item_id ] ) );
136 
137                 if ( isset( $order_item_tax_class[ $item_id ] ) )
138                     wc_update_order_item_meta( $item_id, '_tax_class', wc_clean( $order_item_tax_class[ $item_id ] ) );
139 
140                 // Get values. Subtotals might not exist, in which case copy value from total field
141                 $line_total[ $item_id ]        = isset( $line_total[ $item_id ] ) ? $line_total[ $item_id ] : 0;
142                 $line_tax[ $item_id ]          = isset( $line_tax[ $item_id ] ) ? $line_tax[ $item_id ] : 0;
143                 $line_subtotal[ $item_id ]     = isset( $line_subtotal[ $item_id ] ) ? $line_subtotal[ $item_id ] : $line_total[ $item_id ];
144                 $line_subtotal_tax[ $item_id ] = isset( $line_subtotal_tax[ $item_id ] ) ? $line_subtotal_tax[ $item_id ] : $line_tax[ $item_id ];
145 
146                 // Update values
147                 wc_update_order_item_meta( $item_id, '_line_subtotal', wc_format_decimal( $line_subtotal[ $item_id ] ) );
148                 wc_update_order_item_meta( $item_id, '_line_subtotal_tax', wc_format_decimal( $line_subtotal_tax[ $item_id ] ) );
149                 wc_update_order_item_meta( $item_id, '_line_total', wc_format_decimal( $line_total[ $item_id ] ) );
150                 wc_update_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $line_tax[ $item_id ] ) );
151 
152                 // Total up
153                 $subtotal += wc_format_decimal( $line_subtotal[ $item_id ] );
154                 $total    += wc_format_decimal( $line_total[ $item_id ] );
155 
156                 // Clear meta cache
157                 wp_cache_delete( $item_id, 'order_item_meta' );
158             }
159         }
160 
161         // Save meta
162         $meta_keys      = isset( $_POST['meta_key'] ) ? $_POST['meta_key'] : array();
163         $meta_values    = isset( $_POST['meta_value'] ) ? $_POST['meta_value'] : array();
164 
165         foreach ( $meta_keys as $id => $meta_key ) {
166             $meta_value = ( empty( $meta_values[ $id ] ) && ! is_numeric( $meta_values[ $id ] ) ) ? '' : $meta_values[ $id ];
167             $meta_value = stripslashes( $meta_value );
168             $wpdb->update(
169                 $wpdb->prefix . "woocommerce_order_itemmeta",
170                 array(
171                     'meta_key'   => $meta_key,
172                     'meta_value' => $meta_value
173                 ),
174                 array( 'meta_id' => $id ),
175                 array( '%s', '%s' ),
176                 array( '%d' )
177             );
178         }
179 
180         // Update cart discount from item totals
181         update_post_meta( $post_id, '_cart_discount', $subtotal - $total );
182     }
183 }
WooCommerce API documentation generated by ApiGen 2.8.0