1 <?php
  2 /**
  3  * Order Totals
  4  *
  5  * Functions for displaying the order totals 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_Totals {
 16 
 17     /**
 18      * Output the metabox
 19      */
 20     public static function output() {
 21         global $woocommerce, $theorder, $wpdb, $post;
 22 
 23         if ( ! is_object( $theorder ) )
 24             $theorder = new WC_Order( $post->ID );
 25 
 26         $order = $theorder;
 27 
 28         $data = get_post_meta( $post->ID );
 29         ?>
 30         <div class="totals_group">
 31             <h4><span class="tax_total_display inline_total"></span><?php _e( 'Shipping', 'woocommerce' ); ?></h4>
 32 
 33             <div id="shipping_rows" class="total_rows">
 34                 <?php
 35                     if ( WC()->shipping() )
 36                         $shipping_methods = WC()->shipping->load_shipping_methods();
 37 
 38                     foreach ( $order->get_shipping_methods() as $item_id => $item ) {
 39                         $chosen_method  = $item['method_id'];
 40                         $shipping_title = $item['name'];
 41                         $shipping_cost  = $item['cost'];
 42 
 43                         include( 'views/html-order-shipping.php' );
 44                     }
 45 
 46                     // Pre 2.1
 47                     if ( isset( $data['_shipping_method'] ) ) {
 48                         $item_id        = '';
 49                         $chosen_method  = ! empty( $data['_shipping_method'][0] ) ? $data['_shipping_method'][0] : '';
 50                         $shipping_title = ! empty( $data['_shipping_method_title'][0] ) ? $data['_shipping_method_title'][0] : '';
 51                         $shipping_cost  = ! empty( $data['_order_shipping'][0] ) ? $data['_order_shipping'][0] : '';
 52 
 53                         include( 'views/html-order-shipping.php' );
 54                     }
 55                 ?>
 56             </div>
 57 
 58             <h4><a href="#" class="add_total_row" data-row="<?php
 59                 $item_id        = '';
 60                 $chosen_method  = '';
 61                 $shipping_cost  = '';
 62                 $shipping_title = __( 'Shipping', 'woocommerce' );
 63                 ob_start();
 64                 include( 'views/html-order-shipping.php' );
 65                 echo esc_attr( ob_get_clean() );
 66             ?>"><?php _e( '+ Add shipping cost', 'woocommerce' ); ?> <span class="tips" data-tip="<?php _e( 'These are the shipping and handling costs for the order.', 'woocommerce' ); ?>">[?]</span></a></a></h4>
 67             <div class="clear"></div>
 68 
 69             <?php do_action( 'woocommerce_admin_order_totals_after_shipping', $post->ID ) ?>
 70         </div>
 71 
 72         <?php if ( get_option( 'woocommerce_calc_taxes' ) == 'yes' ) : ?>
 73 
 74         <div class="totals_group tax_rows_group">
 75             <h4><span class="tax_total_display inline_total"></span><?php _e( 'Taxes', 'woocommerce' ); ?></h4>
 76             <div id="tax_rows" class="total_rows">
 77                 <?php
 78                     global $wpdb;
 79 
 80                     $rates = $wpdb->get_results( "SELECT tax_rate_id, tax_rate_country, tax_rate_state, tax_rate_name, tax_rate_priority FROM {$wpdb->prefix}woocommerce_tax_rates ORDER BY tax_rate_name" );
 81 
 82                     $tax_codes = array();
 83 
 84                     foreach( $rates as $rate ) {
 85                         $code = array();
 86 
 87                         $code[] = $rate->tax_rate_country;
 88                         $code[] = $rate->tax_rate_state;
 89                         $code[] = $rate->tax_rate_name ? sanitize_title( $rate->tax_rate_name ) : 'TAX';
 90                         $code[] = absint( $rate->tax_rate_priority );
 91 
 92                         $tax_codes[ $rate->tax_rate_id ] = strtoupper( implode( '-', array_filter( $code ) ) );
 93                     }
 94 
 95                     foreach ( $order->get_taxes() as $item_id => $item ) {
 96                         include( 'views/html-order-tax.php' );
 97                     }
 98                 ?>
 99             </div>
100             <h4><a href="#" class="add_total_row" data-row="<?php
101                 $item_id = '';
102                 $item    = '';
103                 ob_start();
104                 include( 'views/html-order-tax.php' );
105                 echo esc_attr( ob_get_clean() );
106                 ?>"><?php _e( '+ Add tax row', 'woocommerce' ); ?> <span class="tips" data-tip="<?php _e( 'These rows contain taxes for this order. This allows you to display multiple or compound taxes rather than a single total.', 'woocommerce' ); ?>">[?]</span></a></a></h4>
107             <div class="clear"></div>
108         </div>
109 
110         <?php endif; ?>
111 
112         <div class="totals_group">
113             <h4><label for="_order_discount"><?php _e( 'Order Discount', 'woocommerce' ); ?> <span class="tips" data-tip="<?php _e( 'This is the total discount applied after tax.', 'woocommerce' ); ?>">[?]</span></label></h4>
114             <input type="text" class="wc_input_price" id="_order_discount" name="_order_discount" placeholder="<?php echo wc_format_localized_price( 0 ); ?>" value="<?php
115                 if ( isset( $data['_order_discount'][0] ) )
116                     echo esc_attr( wc_format_localized_price( $data['_order_discount'][0] ) );
117             ?>" />
118         </div>
119         <div class="totals_group">
120             <h4><label for="_order_total"><?php _e( 'Order Total', 'woocommerce' ); ?></label></h4>
121             <input type="text" class="wc_input_price" id="_order_total" name="_order_total" placeholder="<?php echo wc_format_localized_price( 0 ); ?>" value="<?php
122                         if ( isset( $data['_order_total'][0] ) )
123                             echo esc_attr( wc_format_localized_price( $data['_order_total'][0] ) );
124             ?>" />
125         </div>
126         <?php
127         $coupons = $order->get_items( array( 'coupon' ) );
128 
129         if ( $coupons ) {
130             ?>
131             <div class="totals_group">
132                 <ul class="wc_coupon_list"><?php
133                     foreach ( $coupons as $item_id => $item ) {
134 
135                         $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' LIMIT 1;", $item['name'] ) );
136 
137                         $link = $post_id ? add_query_arg( array( 'post' => $post_id, 'action' => 'edit' ), admin_url( 'post.php' ) ) : add_query_arg( array( 's' => $item['name'], 'post_status' => 'all', 'post_type' => 'shop_coupon' ), admin_url( 'edit.php' ) );
138 
139                         echo '<li class="tips code" data-tip="' . esc_attr( wc_price( $item['discount_amount'] ) ) . '"><a href="' . esc_url( $link ) . '"><span>' . esc_html( $item['name'] ). '</span></a></li>';
140                     }
141                 ?></ul>
142             </div>
143             <?php
144         }
145         ?>
146         <p class="buttons">
147             <?php if ( get_option( 'woocommerce_calc_taxes' ) == 'yes' ) : ?>
148                 <button type="button" class="button calc_line_taxes"><?php _e( 'Calculate Tax', 'woocommerce' ); ?></button>
149             <?php endif; ?>
150             <button type="button" class="button calc_totals button-primary"><?php _e( 'Calculate Total', 'woocommerce' ); ?></button>
151         </p>
152         <?php
153     }
154 
155     /**
156      * Save meta box data
157      */
158     public static function save( $post_id, $post ) {
159         global $wpdb;
160 
161         // Save tax rows
162         $total_tax          = 0;
163         $total_shipping_tax = 0;
164 
165         if ( isset( $_POST['order_taxes_id'] ) ) {
166 
167             $get_values = array( 'order_taxes_id', 'order_taxes_rate_id', 'order_taxes_amount', 'order_taxes_shipping_amount' );
168 
169             foreach( $get_values as $value )
170                 $$value = isset( $_POST[ $value ] ) ? $_POST[ $value ] : array();
171 
172             foreach( $order_taxes_id as $item_id => $value ) {
173 
174                 if ( $item_id == 'new' ) {
175 
176                     foreach ( $value as $new_key => $new_value ) {
177                         $rate_id  = absint( $order_taxes_rate_id[ $item_id ][ $new_key ] );
178 
179                         if ( $rate_id ) {
180                             $rate     = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $rate_id ) );
181                             $label    = $rate->tax_rate_name ? $rate->tax_rate_name : WC()->countries->tax_or_vat();
182                             $compound = $rate->tax_rate_compound ? 1 : 0;
183 
184                             $code = array();
185 
186                             $code[] = $rate->tax_rate_country;
187                             $code[] = $rate->tax_rate_state;
188                             $code[] = $rate->tax_rate_name ? $rate->tax_rate_name : 'TAX';
189                             $code[] = absint( $rate->tax_rate_priority );
190                             $code   = strtoupper( implode( '-', array_filter( $code ) ) );
191                         } else {
192                             $code  = '';
193                             $label = WC()->countries->tax_or_vat();
194                         }
195 
196                         // Add line item
197                         $new_id = wc_add_order_item( $post_id, array(
198                                 'order_item_name' => wc_clean( $code ),
199                                 'order_item_type' => 'tax'
200                         ) );
201 
202                         // Add line item meta
203                         if ( $new_id ) {
204                             wc_update_order_item_meta( $new_id, 'rate_id', $rate_id );
205                             wc_update_order_item_meta( $new_id, 'label', $label );
206                             wc_update_order_item_meta( $new_id, 'compound', $compound );
207 
208                             if ( isset( $order_taxes_amount[ $item_id ][ $new_key ] ) ) {
209                                 wc_update_order_item_meta( $new_id, 'tax_amount', wc_format_decimal( $order_taxes_amount[ $item_id ][ $new_key ] ) );
210 
211                                 $total_tax          += wc_format_decimal( $order_taxes_amount[ $item_id ][ $new_key ] );
212                             }
213 
214                             if ( isset( $order_taxes_shipping_amount[ $item_id ][ $new_key ] ) ) {
215                                 wc_update_order_item_meta( $new_id, 'shipping_tax_amount', wc_format_decimal( $order_taxes_shipping_amount[ $item_id ][ $new_key ] ) );
216 
217                                 $total_shipping_tax += wc_format_decimal( $order_taxes_shipping_amount[ $item_id ][ $new_key ] );
218                             }
219                         }
220                     }
221 
222                 } else {
223 
224                     $item_id  = absint( $item_id );
225                     $rate_id  = absint( $order_taxes_rate_id[ $item_id ] );
226 
227                     if ( $rate_id ) {
228                         $rate     = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $rate_id ) );
229                         $label    = $rate->tax_rate_name ? $rate->tax_rate_name : WC()->countries->tax_or_vat();
230                         $compound = $rate->tax_rate_compound ? 1 : 0;
231 
232                         $code = array();
233 
234                         $code[] = $rate->tax_rate_country;
235                         $code[] = $rate->tax_rate_state;
236                         $code[] = $rate->tax_rate_name ? $rate->tax_rate_name : 'TAX';
237                         $code[] = absint( $rate->tax_rate_priority );
238                         $code   = strtoupper( implode( '-', array_filter( $code ) ) );
239                     } else {
240                         $code  = '';
241                         $label = WC()->countries->tax_or_vat();
242                     }
243 
244                     $wpdb->update(
245                         $wpdb->prefix . "woocommerce_order_items",
246                         array( 'order_item_name' => wc_clean( $code ) ),
247                         array( 'order_item_id' => $item_id ),
248                         array( '%s' ),
249                         array( '%d' )
250                     );
251 
252                     wc_update_order_item_meta( $item_id, 'rate_id', $rate_id );
253                     wc_update_order_item_meta( $item_id, 'label', $label );
254                     wc_update_order_item_meta( $item_id, 'compound', $compound );
255 
256                     if ( isset( $order_taxes_amount[ $item_id ] ) ) {
257                         wc_update_order_item_meta( $item_id, 'tax_amount', wc_format_decimal( $order_taxes_amount[ $item_id ] ) );
258 
259                         $total_tax += wc_format_decimal( $order_taxes_amount[ $item_id ] );
260                     }
261 
262                     if ( isset( $order_taxes_shipping_amount[ $item_id ] ) ) {
263                         wc_update_order_item_meta( $item_id, 'shipping_tax_amount', wc_format_decimal( $order_taxes_shipping_amount[ $item_id ] ) );
264 
265                         $total_shipping_tax += wc_format_decimal( $order_taxes_shipping_amount[ $item_id ] );
266                     }
267                 }
268             }
269         }
270 
271         // Update totals
272         update_post_meta( $post_id, '_order_tax', wc_format_decimal( $total_tax ) );
273         update_post_meta( $post_id, '_order_shipping_tax', wc_format_decimal( $total_shipping_tax ) );
274         update_post_meta( $post_id, '_order_discount', wc_format_decimal( $_POST['_order_discount'] ) );
275         update_post_meta( $post_id, '_order_total', wc_format_decimal( $_POST['_order_total'] ) );
276 
277         // Shipping Rows
278         $order_shipping = 0;
279 
280         if ( isset( $_POST['shipping_method_id'] ) ) {
281 
282             $get_values = array( 'shipping_method_id', 'shipping_method_title', 'shipping_method', 'shipping_cost' );
283 
284             foreach( $get_values as $value )
285                 $$value = isset( $_POST[ $value ] ) ? $_POST[ $value ] : array();
286 
287             foreach( $shipping_method_id as $item_id => $value ) {
288 
289                 if ( $item_id == 'new' ) {
290 
291                     foreach ( $value as $new_key => $new_value ) {
292                         $method_id    = wc_clean( $shipping_method[ $item_id ][ $new_key ] );
293                         $method_title = wc_clean( $shipping_method_title[ $item_id ][ $new_key ] );
294                         $cost         = wc_format_decimal( $shipping_cost[ $item_id ][ $new_key ] );
295 
296                         $new_id = wc_add_order_item( $post_id, array(
297                             'order_item_name'       => $method_title,
298                             'order_item_type'       => 'shipping'
299                         ) );
300 
301                         if ( $new_id ) {
302                             wc_add_order_item_meta( $new_id, 'method_id', $method_id );
303                             wc_add_order_item_meta( $new_id, 'cost', $cost );
304                         }
305 
306                         $order_shipping += $cost;
307                     }
308 
309                 } else {
310 
311                     $item_id      = absint( $item_id );
312                     $method_id    = wc_clean( $shipping_method[ $item_id ] );
313                     $method_title = wc_clean( $shipping_method_title[ $item_id ] );
314                     $cost         = wc_format_decimal( $shipping_cost[ $item_id ] );
315 
316                     $wpdb->update(
317                         $wpdb->prefix . "woocommerce_order_items",
318                         array( 'order_item_name' => $method_title ),
319                         array( 'order_item_id' => $item_id ),
320                         array( '%s' ),
321                         array( '%d' )
322                     );
323 
324                     wc_update_order_item_meta( $item_id, 'method_id', $method_id );
325                     wc_update_order_item_meta( $item_id, 'cost', $cost );
326 
327                     $order_shipping += $cost;
328                 }
329             }
330         }
331 
332         // Delete rows
333         if ( isset( $_POST['delete_order_item_id'] ) ) {
334             $delete_ids = $_POST['delete_order_item_id'];
335 
336             foreach ( $delete_ids as $id )
337                 wc_delete_order_item( absint( $id ) );
338         }
339 
340         delete_post_meta( $post_id, '_shipping_method' );
341         delete_post_meta( $post_id, '_shipping_method_title' );
342         update_post_meta( $post_id, '_order_shipping', $order_shipping );
343         add_post_meta( $post_id, '_order_currency', get_woocommerce_currency(), true );
344     }
345 }
WooCommerce API documentation generated by ApiGen 2.8.0