/**
  * Create a simple subscription product object.
  *
  * @access public
  * @param mixed $product
  */
 public function __construct($product, $args = array())
 {
     parent::__construct($product, $args = array());
     $this->parent_product_type = $this->product_type;
     $this->product_type = 'subscription_variation';
     $this->subscription_variation_level_meta_data = array('subscription_price' => 0, 'subscription_period' => '', 'subscription_period_interval' => 'day', 'subscription_length' => 0, 'subscription_trial_length' => 0, 'subscription_trial_period' => 'day', 'subscription_sign_up_fee' => 0, 'subscription_payment_sync_date' => 0);
     if (WC_Subscriptions::is_woocommerce_pre_2_2()) {
         // WC 2.1
         // Convert selected subscription meta fields for easy access
         foreach ($this->subscription_variation_level_meta_data as $meta_key => $default_value) {
             if (!empty($this->product_custom_fields['_' . $meta_key][0])) {
                 $this->{$meta_key} = maybe_unserialize($this->product_custom_fields['_' . $meta_key][0]);
             } else {
                 $this->{$meta_key} = $this->subscription_variation_level_meta_data[$meta_key];
             }
         }
     } else {
         $this->variation_level_meta_data = array_merge($this->variation_level_meta_data, $this->subscription_variation_level_meta_data);
     }
 }
 /**
  * Settings are saved on a new instance of the PayPal payment gateway, not the global
  * instance, so our admin fields are not set (nor saved). As a result, we need to run
  * the save routine @see WC_Settings_API::process_admin_options() again to save our fields.
  *
  * @since 1.2.5
  */
 public static function save_subscription_form_fields()
 {
     // WC 2.2 added its own API fields so this is no longer necessary
     if (WC_Subscriptions::is_woocommerce_pre_2_2()) {
         $paypal_gateway = WC_Subscriptions_Payment_Gateways::get_payment_gateway('paypal');
         $paypal_gateway->process_admin_options();
     }
 }
 /**
  * Restore renewal flag when cart is reset and modify Product object with
  * renewal order related info
  *
  * @since 1.3
  */
 public static function get_cart_item_from_session($session_data, $values, $key)
 {
     if (isset($values['subscription_renewal'])) {
         $session_data['subscription_renewal'] = $values['subscription_renewal'];
         // Need to get the original order price, not the current price
         $original_order_id = $values['subscription_renewal']['original_order'];
         $order_items = WC_Subscriptions_Order::get_recurring_items($original_order_id);
         $first_order_item = reset($order_items);
         $price = $first_order_item['subscription_recurring_amount'];
         /*
          * Modify the Cart $_product object. 
          * All the cart calculations and cart/checkout/mini-cart displays will use this object.
          * So by modifying it here, we take care of all those cases.
          */
         $_product = $session_data['data'];
         $_product->price = $price;
         // Don't carry over any sign up fee
         $_product->subscription_sign_up_fee = 0;
         if (WC_Subscriptions::is_woocommerce_pre_2_2()) {
             $_product->product_custom_fields['_subscription_sign_up_fee'][0] = 0;
         }
         // Make sure the original subscription terms perisist
         if ('parent' == $session_data['subscription_renewal']['role']) {
             $_product->subscription_price = $price;
             $_product->subscription_period = $first_order_item['subscription_period'];
             $_product->subscription_period_interval = $first_order_item['subscription_interval'];
             $_product->subscription_trial_period = $first_order_item['subscription_trial_period'];
             $_product->subscription_length = $first_order_item['subscription_length'];
             // Never give a free trial period again
             $_product->subscription_trial_length = 0;
             if (WC_Subscriptions::is_woocommerce_pre_2_2()) {
                 $_product->product_custom_fields['_subscription_price'][0] = $price;
                 $_product->product_custom_fields['_subscription_period'][0] = $first_order_item['subscription_period'];
                 $_product->product_custom_fields['_subscription_period_interval'][0] = $first_order_item['subscription_interval'];
                 $_product->product_custom_fields['_subscription_trial_period'][0] = $first_order_item['subscription_trial_period'];
                 $_product->product_custom_fields['_subscription_length'][0] = $first_order_item['subscription_length'];
                 $_product->product_custom_fields['_subscription_trial_length'][0] = 0;
             }
         }
         $_product->post->post_title = apply_filters('woocommerce_subscriptions_renewal_product_title', $_product->get_title(), $_product);
         // Make sure the same quantity is renewed
         $session_data['quantity'] = $first_order_item['qty'];
     }
     return $session_data;
 }
 /**
  * Registers the "Renewal Orders" meta box for the "Edit Order" page.
  */
 public static function add_meta_boxes()
 {
     global $current_screen, $post_id;
     // Only display the meta box if an order relates to a subscription
     if ('shop_order' == $current_screen->id) {
         $order_contains_subscription = WC_Subscriptions_Order::order_contains_subscription($post_id);
         if ($order_contains_subscription || WC_Subscriptions_Renewal_Order::is_renewal($post_id, array('order_role' => 'child'))) {
             add_meta_box('subscription_renewal_orders', __('Related Subscription Orders', 'woocommerce-subscriptions'), __CLASS__ . '::related_orders_meta_box', 'shop_order', 'normal', 'default');
         }
         if ($order_contains_subscription || 'add' == $current_screen->action) {
             if (!WC_Subscriptions::is_woocommerce_pre_2_2()) {
                 add_meta_box('woocommerce-order-totals', __('Recurring Totals', 'woocommerce-subscriptions'), __CLASS__ . '::recurring_totals_meta_box', 'shop_order', 'side', 'high');
             } else {
                 // WC 2.1 compatibility
                 add_filter('woocommerce_admin_order_totals_after_shipping', 'WC_Subscriptions_Order::recurring_order_totals_meta_box_section', 100, 1);
             }
         }
     }
 }
 /**
  * Creates a new order for renewing a subscription product based on the details of a previous order.
  *
  * No trial periods or sign up fees are applied to the renewal order. However, if the order has failed
  * payments and the store manager has set failed payments to be added to renewal orders, then the
  * orders totals will be set to include the outstanding balance.
  *
  * If the $args['new_order_role'] flag is set to 'parent', then the renewal order will supersede the existing 
  * order. The existing order and subscription associated with it will be cancelled. A new order and
  * subscription will be created. 
  *
  * If the $args['new_order_role'] flag is 'child', the $original_order will remain the master order for the
  * subscription and the new order is just for accepting a recurring payment on the subscription.
  *
  * Renewal orders have the same meta data as the original order. If the renewal order is set to be a 'child'
  * then any subscription related meta data will not be stored on the new order. This is to keep subscription
  * meta data associated only with the one master order for the subscription.
  *
  * @param WC_Order|int $order The WC_Order object or ID of the order for which the a new order should be created.
  * @param string $product_id The ID of the subscription product in the order which needs to be added to the new order.
  * @param array $args (optional) An array of name => value flags:
  *         'new_order_role' string A flag to indicate whether the new order should become the master order for the subscription. Accepts either 'parent' or 'child'. Defaults to 'parent' - replace the existing order.
  *         'checkout_renewal' bool Indicates if invoked from an interactive cart/checkout session and certain order items are not set, like taxes, shipping as they need to be set in teh calling function, like @see WC_Subscriptions_Checkout::filter_woocommerce_create_order(). Default false.
  *         'failed_order_id' int For checkout_renewal true, indicates order id being replaced
  * @since 1.2
  */
 public static function generate_renewal_order($original_order, $product_id, $args = array())
 {
     global $wpdb, $woocommerce;
     if (!is_object($original_order)) {
         $original_order = new WC_Order($original_order);
     }
     if (!WC_Subscriptions_Order::order_contains_subscription($original_order) || !WC_Subscriptions_Order::is_item_subscription($original_order, $product_id)) {
         return false;
     }
     if (self::is_renewal($original_order, array('order_role' => 'child'))) {
         $original_order = self::get_parent_order($original_order);
     }
     if (!is_array($args)) {
         _deprecated_argument(__CLASS__ . '::' . __FUNCTION__, '1.3', __('Third parameter is now an array of name => value pairs. Use array( "new_order_role" => "parent" ) instead.', 'woocommerce-subscriptions'));
         $args = array('new_order_role' => $args);
     }
     $args = wp_parse_args($args, array('new_order_role' => 'parent', 'checkout_renewal' => false));
     $renewal_order_key = uniqid('order_');
     // Create the new order
     $renewal_order_data = array('post_type' => 'shop_order', 'post_title' => sprintf(__('Subscription Renewal Order – %s', 'woocommerce-subscriptions'), strftime(_x('%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce-subscriptions'))), 'ping_status' => 'closed', 'post_excerpt' => $original_order->customer_note, 'post_author' => 1, 'post_password' => $renewal_order_key);
     $create_new_order = true;
     if (!WC_Subscriptions::is_woocommerce_pre_2_2()) {
         // WC 2.2 order status
         $renewal_order_data['post_status'] = 'wc-pending';
     } else {
         $renewal_order_data['post_status'] = 'publish';
     }
     if ('child' == $args['new_order_role']) {
         $renewal_order_data['post_parent'] = $original_order->id;
     }
     if (true === $args['checkout_renewal']) {
         $renewal_order_id = null;
         if ($woocommerce->session->order_awaiting_payment > 0) {
             $renewal_order_id = absint($woocommerce->session->order_awaiting_payment);
         } elseif (isset($args['failed_order_id'])) {
             $failed_order_id = $args['failed_order_id'];
             if (WC_Subscriptions::is_woocommerce_pre_2_2()) {
                 // WC 2.1  - need to use taxonomy
                 /* Check order is unpaid by getting its status */
                 $terms = wp_get_object_terms($failed_order_id, 'shop_order_status', array('fields' => 'slugs'));
                 $order_status = isset($terms[0]) ? $terms[0] : 'pending';
             } else {
                 // WC 2.2+
                 $failed_order = wc_get_order($failed_order_id);
                 $order_status = $failed_order->get_status();
             }
             /* If paying on a pending order, we are resuming */
             if ($order_status == 'pending') {
                 $renewal_order_id = $failed_order_id;
             }
         }
         if ($renewal_order_id) {
             if (WC_Subscriptions::is_woocommerce_pre_2_2()) {
                 // WC 2.1  - need to use taxonomy
                 /* Check order is unpaid by getting its status */
                 $terms = wp_get_object_terms($renewal_order_id, 'shop_order_status', array('fields' => 'slugs'));
                 $order_status = isset($terms[0]) ? $terms[0] : 'pending';
             } else {
                 // WC 2.2+
                 $existing_order = wc_get_order($renewal_order_id);
                 $order_status = $existing_order->get_status();
             }
             // Resume the unpaid order if its pending
             if ($order_status == 'pending' || $order_status == 'failed') {
                 // Update the existing order as we are resuming it
                 $create_new_order = false;
                 $renewal_order_data['ID'] = $renewal_order_id;
                 wp_update_post($renewal_order_data);
                 // Clear the old line items - we'll add these again in case they changed
                 $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d )", $renewal_order_id));
                 $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $renewal_order_id));
             }
         }
     }
     if ($create_new_order) {
         $renewal_order_id = wp_insert_post($renewal_order_data);
     }
     if (WC_Subscriptions::is_woocommerce_pre_2_2()) {
         // WC 2.1 order status
         // Set the order as pending
         wp_set_object_terms($renewal_order_id, 'pending', 'shop_order_status');
     }
     // Set a unique key for this order
     update_post_meta($renewal_order_id, '_order_key', $renewal_order_key);
     $order_meta_query = "SELECT `meta_key`, `meta_value`\n\t\t\t\t\t\t\t FROM {$wpdb->postmeta}\n\t\t\t\t\t\t\t WHERE `post_id` = {$original_order->id}\n\t\t\t\t\t\t\t AND `meta_key` NOT IN ('_paid_date', '_completed_date', '_order_key', '_edit_lock', '_original_order', '_wc_points_earned', '_transaction_id')";
     // Superseding existing order so don't carry over payment details
     if ('parent' == $args['new_order_role'] || true === $args['checkout_renewal']) {
         $order_meta_query .= " AND `meta_key` NOT IN ('_payment_method', '_payment_method_title', '_recurring_payment_method', '_recurring_payment_method_title', '_shipping_method', '_shipping_method_title', '_recurring_shipping_method', '_recurring_shipping_method_title')";
     } else {
         $order_meta_query .= " AND `meta_key` NOT LIKE '_order_recurring_%' AND `meta_key` NOT IN ('_payment_method', '_payment_method_title', '_recurring_payment_method', '_recurring_payment_method_title', '_shipping_method', '_shipping_method_title', '_recurring_shipping_method', '_recurring_shipping_method_title')";
     }
     // Allow extensions to add/remove order meta
     $order_meta_query = apply_filters('woocommerce_subscriptions_renewal_order_meta_query', $order_meta_query, $original_order->id, $renewal_order_id, $args['new_order_role']);
     // Carry all the required meta from the old order over to the new order
     $order_meta = $wpdb->get_results($order_meta_query, 'ARRAY_A');
     $order_meta = apply_filters('woocommerce_subscriptions_renewal_order_meta', $order_meta, $original_order->id, $renewal_order_id, $args['new_order_role']);
     foreach ($order_meta as $meta_item) {
         add_post_meta($renewal_order_id, $meta_item['meta_key'], maybe_unserialize($meta_item['meta_value']), true);
     }
     $outstanding_balance = WC_Subscriptions_Order::get_outstanding_balance($original_order, $product_id);
     $failed_payment_multiplier = 1;
     if (false == $args['checkout_renewal']) {
         // If there are outstanding payment amounts, add them to the order, otherwise set the order details to the values of the recurring totals
         if ($outstanding_balance > 0 && 'yes' == get_option(WC_Subscriptions_Admin::$option_prefix . '_add_outstanding_balance', 'no')) {
             $failed_payment_multiplier = WC_Subscriptions_Order::get_failed_payment_count($original_order, $product_id);
         }
         // Set order totals based on recurring totals from the original order
         $cart_discount = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_discount_cart', true));
         $order_discount = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_discount_total', true));
         $order_shipping_tax = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_shipping_tax_total', true));
         $order_shipping = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_shipping_total', true));
         $order_tax = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_tax_total', true));
         $order_total = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_total', true));
         update_post_meta($renewal_order_id, '_cart_discount', $cart_discount);
         update_post_meta($renewal_order_id, '_order_discount', $order_discount);
         update_post_meta($renewal_order_id, '_order_shipping_tax', $order_shipping_tax);
         update_post_meta($renewal_order_id, '_order_shipping', $order_shipping);
         update_post_meta($renewal_order_id, '_order_tax', $order_tax);
         update_post_meta($renewal_order_id, '_order_total', $order_total);
         // Set shipping for orders created with WC 2.0.n (or when we are using WC 2.0.n)
         if (WC_Subscriptions::is_woocommerce_pre_2_1() || isset($original_order->recurring_shipping_method)) {
             update_post_meta($renewal_order_id, '_shipping_method', $original_order->recurring_shipping_method);
             update_post_meta($renewal_order_id, '_shipping_method_title', $original_order->recurring_shipping_method_title);
             // Also set recurring shipping as it's a parent renewal order
             if ('parent' == $args['new_order_role']) {
                 update_post_meta($renewal_order_id, '_recurring_shipping_method', $original_order->recurring_shipping_method);
                 update_post_meta($renewal_order_id, '_recurring_shipping_method_title', $original_order->recurring_shipping_method_title);
             }
         }
         // Apply the recurring shipping & payment methods to child renewal orders
         if ('child' == $args['new_order_role']) {
             update_post_meta($renewal_order_id, '_payment_method', $original_order->recurring_payment_method);
             update_post_meta($renewal_order_id, '_payment_method_title', $original_order->recurring_payment_method_title);
         }
         // Set order taxes based on recurring taxes from the original order
         $recurring_order_taxes = WC_Subscriptions_Order::get_recurring_taxes($original_order);
         foreach ($recurring_order_taxes as $index => $recurring_order_tax) {
             $item_ids = array();
             $item_ids[] = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $recurring_order_tax['name'], 'order_item_type' => 'tax'));
             // Also set recurring taxes on parent renewal orders
             if ('parent' == $args['new_order_role']) {
                 $item_ids[] = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $recurring_order_tax['name'], 'order_item_type' => 'recurring_tax'));
             }
             // Add line item meta
             foreach ($item_ids as $item_id) {
                 woocommerce_add_order_item_meta($item_id, 'compound', absint(isset($recurring_order_tax['compound']) ? $recurring_order_tax['compound'] : 0));
                 woocommerce_add_order_item_meta($item_id, 'tax_amount', $failed_payment_multiplier * WC_Subscriptions::format_total($recurring_order_tax['tax_amount']));
                 woocommerce_add_order_item_meta($item_id, 'shipping_tax_amount', $failed_payment_multiplier * WC_Subscriptions::format_total($recurring_order_tax['shipping_tax_amount']));
                 if (isset($recurring_order_tax['rate_id'])) {
                     woocommerce_add_order_item_meta($item_id, 'rate_id', $recurring_order_tax['rate_id']);
                 }
                 if (isset($recurring_order_tax['label'])) {
                     woocommerce_add_order_item_meta($item_id, 'label', $recurring_order_tax['label']);
                 }
             }
         }
         // Set up shipping items on renewal order
         $recurring_shipping_items = WC_Subscriptions_Order::get_recurring_shipping_methods($original_order);
         foreach ($recurring_shipping_items as $recurring_shipping_item_id => $recurring_shipping_item) {
             $item_ids = array();
             $item_ids[] = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $recurring_shipping_item['name'], 'order_item_type' => 'shipping'));
             // Also set recurring shipping as it's a parent renewal order
             if ('parent' == $args['new_order_role']) {
                 $item_ids[] = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $recurring_shipping_item['name'], 'order_item_type' => 'recurring_shipping'));
             }
             // Add shipping item meta
             foreach ($item_ids as $item_id) {
                 woocommerce_add_order_item_meta($item_id, 'method_id', $recurring_shipping_item['method_id']);
                 woocommerce_add_order_item_meta($item_id, 'cost', $failed_payment_multiplier * WC_Subscriptions::format_total($recurring_shipping_item['cost']));
                 // Set line taxes for shipping
                 if (isset($recurring_shipping_item['taxes'])) {
                     $taxes = maybe_unserialize($recurring_shipping_item['taxes']);
                 } else {
                     // try to determine recurring shipping taxes from original order's shipping taxes
                     $taxes = array();
                     foreach ($original_order->get_shipping_methods() as $original_shipping_item_id => $original_shipping_item) {
                         // We need to use the amount of the same shipping method as this one
                         if ($recurring_shipping_item['method_id'] != $original_shipping_item['method_id']) {
                             continue;
                         }
                         // If we don't have taxes on the matching shipping item, there's nothing we can do
                         if (!isset($original_shipping_item['taxes'])) {
                             break;
                         }
                         $original_shipping_item['taxes'] = maybe_unserialize($original_shipping_item['taxes']);
                         // Make sure we account for any amount on the original original shipping not applied to renewals by determining what proportion of the initial amount the recurring total represents
                         if (0 != $original_shipping_item['cost'] && $recurring_shipping_item['cost'] != $original_shipping_item['cost']) {
                             $recurring_ratio = $recurring_shipping_item['cost'] / $original_shipping_item['cost'];
                             foreach ($original_shipping_item['taxes'] as $tax_id => $tax_amount) {
                                 $original_shipping_item['taxes'][$tax_id] = $recurring_ratio * $tax_amount;
                             }
                         }
                         foreach ($original_shipping_item['taxes'] as $tax_id => $tax_amount) {
                             $taxes[$tax_id] = WC_Subscriptions::format_total($failed_payment_multiplier * $tax_amount);
                         }
                     }
                 }
                 woocommerce_add_order_item_meta($item_id, 'taxes', $taxes);
             }
         }
     }
     // Set line totals to be recurring line totals and remove the subscription/recurring related item meta from each order item
     $order_items = WC_Subscriptions_Order::get_recurring_items($original_order);
     // Allow extensions to add/remove items or item meta
     $order_items = apply_filters('woocommerce_subscriptions_renewal_order_items', $order_items, $original_order->id, $renewal_order_id, $product_id, $args['new_order_role']);
     if (true === $args['checkout_renewal']) {
         $cart_items = $woocommerce->cart->get_cart();
     }
     foreach ($order_items as $item_index => $order_item) {
         $renewal_order_item_name = apply_filters('woocommerce_subscriptions_renewal_order_item_name', $order_item['name'], $order_item, $original_order);
         // Create order line item on the renewal order
         $recurring_item_id = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $renewal_order_item_name, 'order_item_type' => 'line_item'));
         if (true === $args['checkout_renewal']) {
             $cart_item = array();
             foreach ($cart_items as $item) {
                 if ($item['product_id'] == $order_item['product_id'] && (empty($order_item['variation_id']) || $item['variation_id'] == $order_item['variation_id'])) {
                     $cart_item = $item;
                 }
             }
             if (!empty($cart_item)) {
                 woocommerce_update_order_item_meta($recurring_item_id, '_line_total', woocommerce_format_decimal($cart_item['line_total']));
                 woocommerce_update_order_item_meta($recurring_item_id, '_line_tax', woocommerce_format_decimal($cart_item['line_tax']));
                 woocommerce_update_order_item_meta($recurring_item_id, '_line_subtotal', woocommerce_format_decimal($cart_item['line_subtotal']));
                 woocommerce_update_order_item_meta($recurring_item_id, '_line_subtotal_tax', woocommerce_format_decimal($cart_item['line_subtotal_tax']));
                 if (is_object($cart_item['data'])) {
                     woocommerce_update_order_item_meta($recurring_item_id, '_tax_class', $cart_item['data']->get_tax_class());
                 }
             }
             $cart_items = $woocommerce->cart->get_cart();
         }
         $item_meta = new WC_Order_Item_Meta($order_item['item_meta']);
         // Remove recurring line items and set item totals based on recurring line totals
         foreach ($item_meta->meta as $meta_key => $meta) {
             // $meta is an array, so the item needs to be extracted from $meta[0] (just like order meta on a WC Order)
             $meta_value = maybe_unserialize($meta[0]);
             if (false === $args['checkout_renewal']) {
                 // Already set earlier
                 // Map line item totals based on recurring line totals
                 switch ($meta_key) {
                     case '_recurring_line_total':
                         woocommerce_update_order_item_meta($recurring_item_id, '_line_total', $failed_payment_multiplier * woocommerce_format_decimal($meta_value));
                         break;
                     case '_recurring_line_tax':
                         woocommerce_update_order_item_meta($recurring_item_id, '_line_tax', $failed_payment_multiplier * woocommerce_format_decimal($meta_value));
                         break;
                     case '_recurring_line_subtotal':
                         woocommerce_update_order_item_meta($recurring_item_id, '_line_subtotal', $failed_payment_multiplier * woocommerce_format_decimal($meta_value));
                         break;
                     case '_recurring_line_subtotal_tax':
                         woocommerce_update_order_item_meta($recurring_item_id, '_line_subtotal_tax', $failed_payment_multiplier * woocommerce_format_decimal($meta_value));
                         break;
                     case '_line_tax_data':
                         // Copy line tax data if the order doesn't have a _recurring_line_tax_data (for backward compatibility)
                         if (!array_key_exists('_recurring_line_tax_data', $item_meta->meta)) {
                             $line_total = $item_meta->meta['_line_total'][0];
                             $recurring_line_total = $item_meta->meta['_recurring_line_total'][0];
                             // There will only be recurring tax data if the recurring amount is > 0 and we can only retroactively calculate recurring amount from initial amoutn if it is > 0
                             if ($line_total > 0 && $recurring_line_total > 0) {
                                 // Make sure we account for any sign-up fees by determining what proportion of the initial amount the recurring total represents
                                 $recurring_ratio = $recurring_line_total / $line_total;
                                 $recurring_tax_data = array();
                                 $tax_data_keys = array('total', 'subtotal');
                                 foreach ($tax_data_keys as $tax_data_key) {
                                     foreach ($meta_value[$tax_data_key] as $tax_index => $tax_value) {
                                         // Use total tax amount for both total and subtotal because we don't want any initial discounts to be applied to recurring amounts
                                         $total_tax_amount = $meta_value['total'][$tax_index];
                                         $recurring_tax_data[$tax_data_key][$tax_index] = woocommerce_format_decimal($failed_payment_multiplier * ($recurring_ratio * $total_tax_amount));
                                     }
                                 }
                             } else {
                                 $recurring_tax_data = array('total' => array(), 'subtotal' => array());
                             }
                             woocommerce_update_order_item_meta($recurring_item_id, '_line_tax_data', $recurring_tax_data);
                         }
                         break;
                     case '_recurring_line_tax_data':
                         $recurring_tax_data = array();
                         $tax_data_keys = array('total', 'subtotal');
                         foreach ($tax_data_keys as $tax_data_key) {
                             foreach ($meta_value[$tax_data_key] as $tax_index => $tax_value) {
                                 $recurring_tax_data[$tax_data_key][$tax_index] = woocommerce_format_decimal($failed_payment_multiplier * $tax_value);
                             }
                         }
                         woocommerce_update_order_item_meta($recurring_item_id, '_line_tax_data', $recurring_tax_data);
                         break;
                     default:
                         break;
                 }
             }
             // Copy over line item meta data, with some parent/child role based exceptions for recurring amounts
             $copy_to_renewal_item = true;
             switch ($meta_key) {
                 case '_recurring_line_total':
                 case '_recurring_line_tax':
                 case '_recurring_line_subtotal':
                 case '_recurring_line_subtotal_tax':
                 case '_recurring_line_tax_data':
                 case '_line_tax_data':
                 case '_subscription_recurring_amount':
                 case '_subscription_sign_up_fee':
                 case '_subscription_period':
                 case '_subscription_interval':
                 case '_subscription_length':
                 case '_subscription_trial_period':
                 case '_subscription_end_date':
                 case '_subscription_expiry_date':
                 case '_subscription_start_date':
                 case '_subscription_status':
                 case '_subscription_completed_payments':
                     if ('child' == $args['new_order_role']) {
                         $copy_to_renewal_item = false;
                     }
                     break;
                 case '_subscription_trial_length':
                     // We never want to duplicate free trials on renewal orders
                     $copy_to_renewal_item = false;
                     break;
                 case '_subscription_suspension_count':
                     // We want to reset some values for the new order
                 // We want to reset some values for the new order
                 case '_subscription_trial_expiry_date':
                 case '_subscription_failed_payments':
                     $copy_to_renewal_item = false;
                     $meta_value = 0;
                     break;
                 default:
                     break;
             }
             // Copy existing item over to new recurring order item
             if ($copy_to_renewal_item) {
                 woocommerce_add_order_item_meta($recurring_item_id, $meta_key, $meta_value);
             }
         }
     }
     if (false == $args['checkout_renewal']) {
         // Add fees
         foreach ($original_order->get_fees() as $item_id => $order_fee) {
             if (!isset($order_fee['recurring_line_total'])) {
                 continue;
             }
             $item_id = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $order_fee['name'], 'order_item_type' => 'fee'));
             woocommerce_add_order_item_meta($item_id, '_tax_class', $order_fee['tax_class']);
             woocommerce_add_order_item_meta($item_id, '_line_total', WC_Subscriptions::format_total($order_fee['recurring_line_total']));
             woocommerce_add_order_item_meta($item_id, '_line_tax', WC_Subscriptions::format_total($order_fee['recurring_line_tax']));
         }
     }
     // Keep a record of the original order's ID on the renewal order
     update_post_meta($renewal_order_id, '_original_order', $original_order->id, true);
     $renewal_order = new WC_Order($renewal_order_id);
     if ('parent' == $args['new_order_role']) {
         WC_Subscriptions_Manager::process_subscriptions_on_checkout($renewal_order_id);
         $original_order->add_order_note(sprintf(__('Order superseded by Renewal Order %s.', 'woocommerce-subscriptions'), $renewal_order->get_order_number()));
     }
     do_action('woocommerce_subscriptions_renewal_order_created', $renewal_order, $original_order, $product_id, $args['new_order_role']);
     return apply_filters('woocommerce_subscriptions_renewal_order_id', $renewal_order_id, $original_order, $product_id, $args['new_order_role']);
 }
    /**
     * Display recurring order totals on the "Edit Order" page.
     *
     * @param int $post_id The post ID of the shop_order post object.
     * @since 1.2.4
     * @return void
     */
    public static function recurring_order_totals_meta_box_section($post_id)
    {
        global $woocommerce, $wpdb, $current_screen;
        $order = new WC_Order($post_id);
        $display_none = ' style="display: none"';
        $contains_subscription = self::order_contains_subscription($order) ? true : false;
        $chosen_gateway = WC_Subscriptions_Payment_Gateways::get_payment_gateway(get_post_meta($post_id, '_recurring_payment_method', true));
        $manual_renewal = self::requires_manual_renewal($post_id);
        $changes_supported = $chosen_gateway === false || $manual_renewal == 'true' || $chosen_gateway->supports('subscription_amount_changes') ? 'true' : 'false';
        $data = get_post_meta($post_id);
        if (WC_Subscriptions::is_woocommerce_pre_2_2()) {
            ?>
	<div class="clear"></div>
</div>
		<?php 
        }
        ?>
<div id="gateway_support"<?php 
        if (!$contains_subscription) {
            echo $display_none;
        }
        ?>
>
	<input type="hidden" name="gateway_supports_subscription_changes" value="<?php 
        echo $changes_supported;
        ?>
">
	<div class="error"<?php 
        if (!$contains_subscription || $changes_supported == 'true') {
            echo $display_none;
        }
        ?>
>
		<p><?php 
        printf(__('The %s payment gateway is used to charge automatic subscription payments for this order. This gateway <strong>does not</strong> support changing a subscription\'s details.', 'woocommerce-subscriptions'), get_post_meta($post_id, '_recurring_payment_method_title', true));
        ?>
</p>
		<p>
			<?php 
        _e('It is strongly recommended you <strong>do not change</strong> any of the recurring totals or subscription item\'s details.', 'woocommerce-subscriptions');
        ?>
			<a href="http://docs.woothemes.com/document/subscriptions/add-or-modify-a-subscription/#section-4"><?php 
        _e('Learn More', 'woocommerce-subscriptions');
        ?>
 &raquo;</a>
		</p>
	</div>
</div>
<div id="recurring_order_totals"<?php 
        if (!$contains_subscription) {
            echo $display_none;
        }
        ?>
>
	<?php 
        if (WC_Subscriptions::is_woocommerce_pre_2_2()) {
            ?>
	<h3><?php 
            _e('Recurring Totals', 'woocommerce-subscriptions');
            ?>
</h3>
	<?php 
        }
        ?>

	<?php 
        if ('add' !== $current_screen->action) {
            // Can't add recurring shipping to a manually added subscription
            ?>
	<div class="totals_group">
		<h4><span class="tax_total_display inline_total"></span><?php 
            _e('Shipping for Renewal Orders', 'woocommerce-subscriptions');
            ?>
</h4>
		<div id="recurring_shipping_rows">
		<?php 
            if (!WC_Subscriptions::is_woocommerce_pre_2_1()) {
                if ($woocommerce->shipping()) {
                    $shipping_methods = $woocommerce->shipping->load_shipping_methods();
                }
                foreach (self::get_recurring_shipping_methods($order) as $item_id => $item) {
                    $chosen_method = $item['method_id'];
                    $shipping_title = $item['name'];
                    $shipping_cost = $item['cost'];
                    include plugin_dir_path(WC_Subscriptions::$plugin_file) . 'templates/admin/post-types/writepanels/order-shipping-html.php';
                }
                // Shipping created pre 2.1
                if (isset($data['_recurring_shipping_method'])) {
                    $item_id = 'old';
                    // so that when saving, we know to delete the data in the old form
                    $chosen_method = !empty($data['_recurring_shipping_method'][0]) ? $data['_recurring_shipping_method'][0] : '';
                    $shipping_title = !empty($data['_recurring_shipping_method_title'][0]) ? $data['_recurring_shipping_method_title'][0] : '';
                    $shipping_cost = !empty($data['_order_recurring_shipping_total'][0]) ? $data['_order_recurring_shipping_total'][0] : '0.00';
                    include plugin_dir_path(WC_Subscriptions::$plugin_file) . 'templates/admin/post-types/writepanels/order-shipping-html.php';
                }
                ?>
		<?php 
            } else {
                // WC < 2.1
                ?>
			<ul class="totals">
				<li class="wide">
					<label><?php 
                _e('Label:', 'woocommerce-subscriptions');
                ?>
</label>
					<input type="text" id="_recurring_shipping_method_title" name="_recurring_shipping_method_title" placeholder="<?php 
                _e('The shipping title for renewal orders', 'woocommerce-subscriptions');
                ?>
" value="<?php 
                echo $order->recurring_shipping_method_title;
                ?>
" class="first" />
				</li>

				<li class="left">
					<label><?php 
                _e('Cost:', 'woocommerce-subscriptions');
                ?>
</label>
					<input type="text" id="_order_recurring_shipping_total" name="_order_recurring_shipping_total" placeholder="0.00 <?php 
                _e('(ex. tax)', 'woocommerce-subscriptions');
                ?>
" value="<?php 
                echo self::get_recurring_shipping_total($order);
                ?>
" class="first" />
				</li>

				<li class="right">
					<label><?php 
                _e('Method:', 'woocommerce-subscriptions');
                ?>
</label>
					<select name="_recurring_shipping_method" id="_recurring_shipping_method" class="first">
						<option value=""><?php 
                _e('N/A', 'woocommerce-subscriptions');
                ?>
</option>
						<?php 
                $chosen_shipping_method = $order->recurring_shipping_method;
                $found_method = false;
                if ($woocommerce->shipping()) {
                    foreach ($woocommerce->shipping->load_shipping_methods() as $method) {
                        if (strpos($chosen_shipping_method, $method->id) === 0) {
                            $value = $chosen_shipping_method;
                        } else {
                            $value = $method->id;
                        }
                        echo '<option value="' . esc_attr($value) . '" ' . selected($chosen_shipping_method == $value, true, false) . '>' . esc_html($method->get_title()) . '</option>';
                        if ($chosen_shipping_method == $value) {
                            $found_method = true;
                        }
                    }
                }
                if (!$found_method && !empty($chosen_shipping_method)) {
                    echo '<option value="' . esc_attr($chosen_shipping_method) . '" selected="selected">' . __('Other', 'woocommerce-subscriptions') . '</option>';
                } else {
                    echo '<option value="other">' . __('Other', 'woocommerce-subscriptions') . '</option>';
                }
                ?>
					</select>
				</li>
			</ul>
		<?php 
            }
            // ! WC_Subscriptions::is_woocommerce_pre_2_1()
            ?>
		</div>
		<div class="clear"></div>
	</div>
	<?php 
        }
        ?>

	<?php 
        if ('yes' == get_option('woocommerce_calc_taxes')) {
            ?>

	<div class="totals_group tax_rows_group">
		<h4>
			<span class="tax_total_display inline_total"></span>
			<?php 
            _e('Recurring Taxes', 'woocommerce-subscriptions');
            ?>
			<a class="tips" data-tip="<?php 
            _e('These rows contain taxes included in each recurring amount for this subscription. This allows you to display multiple or compound taxes rather than a single total on future subscription renewal orders.', 'woocommerce-subscriptions');
            ?>
" href="#">[?]</a>
		</h4>
		<div id="recurring_tax_rows" class="total_rows">
			<?php 
            $loop = 0;
            $taxes = self::get_recurring_taxes($order);
            if (is_array($taxes) && sizeof($taxes) > 0) {
                $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");
                $tax_codes = array();
                foreach ($rates as $rate) {
                    $code = array();
                    $code[] = $rate->tax_rate_country;
                    $code[] = $rate->tax_rate_state;
                    $code[] = $rate->tax_rate_name ? sanitize_title($rate->tax_rate_name) : 'TAX';
                    $code[] = absint($rate->tax_rate_priority);
                    $tax_codes[$rate->tax_rate_id] = strtoupper(implode('-', array_filter($code)));
                }
                foreach ($taxes as $item_id => $item) {
                    include plugin_dir_path(WC_Subscriptions::$plugin_file) . 'templates/admin/post-types/writepanels/order-tax-html.php';
                    $loop++;
                }
            }
            ?>
		</div>
		<h4 style="padding-bottom: 10px;"><a href="#" class="add_recurring_tax_row"><?php 
            _e('+ Add tax row', 'woocommerce-subscriptions');
            ?>
</a></h4>
		<div class="clear"></div>
	</div>

	<?php 
            if (WC_Subscriptions::is_woocommerce_pre_2_1()) {
                ?>
	<div class="totals_group">
		<h4><span class="tax_total_display inline_total"></span><?php 
                _e('Tax Totals', 'woocommerce-subscriptions');
                ?>
</h4>
		<ul class="totals">

			<li class="left">
				<label><?php 
                _e('Recurring Sales Tax:', 'woocommerce-subscriptions');
                ?>
</label>
				<input type="number" step="any" min="0" id="_order_recurring_tax_total" name="_order_recurring_tax_total" placeholder="0.00" value="<?php 
                echo self::get_recurring_total_tax($order);
                ?>
" class="calculated" />
			</li>

			<li class="right">
				<label><?php 
                _e('Shipping Tax:', 'woocommerce-subscriptions');
                ?>
</label>
				<input type="number" step="any" min="0" id="_order_recurring_shipping_tax_total" name="_order_recurring_shipping_tax_total" placeholder="0.00" value="<?php 
                echo self::get_recurring_shipping_tax_total($order);
                ?>
" class="calculated" />
			</li>

		</ul>
		<div class="clear"></div>
	</div>
	<?php 
            }
            // WC_Subscriptions::is_woocommerce_pre_2_1()
            ?>

	<?php 
        }
        // woocommerce_calc_taxes
        ?>

	<div class="totals_group">
		<h4><label for="_order_recurring_discount_total"><?php 
        _e('Recurring Order Discount', 'woocommerce-subscriptions');
        ?>
 <a class="tips" data-tip="<?php 
        _e('The discounts applied to each recurring payment charged in the future.', 'woocommerce-subscriptions');
        ?>
" href="#">[?]</a></label></h4>
		<input type="text" class="wc_input_price"  id="_order_recurring_discount_total" name="_order_recurring_discount_total" placeholder="<?php 
        echo wc_format_localized_price(0);
        ?>
" value="<?php 
        echo esc_attr(wc_format_localized_price(self::get_recurring_discount_total($order)));
        ?>
" style="margin: 6px 0 10px;"/>
	</div>

	<div class="totals_group">
		<h4><label for="_order_recurring_total"><?php 
        _e('Recurring Order Total', 'woocommerce-subscriptions');
        ?>
 <a class="tips" data-tip="<?php 
        _e('The total amounts charged for each future recurring payment.', 'woocommerce-subscriptions');
        ?>
" href="#">[?]</a></label></h4>
		<input type="text" id="_order_recurring_total" name="_order_recurring_total" placeholder="<?php 
        echo wc_format_localized_price(0);
        ?>
" value="<?php 
        echo esc_attr(wc_format_localized_price(self::get_recurring_total($order)));
        ?>
" class="calculated"  style="margin: 6px 0 10px;"/>
	</div>

	<div class="totals_group">
		<h4><?php 
        _e('Recurring Payment Method:', 'woocommerce-subscriptions');
        ?>
</h4>
		<div class="<?php 
        echo $order->recurring_payment_method;
        ?>
" style="padding-top: 4px; font-style: italic; margin: 2px 0 10px;"><?php 
        echo $manual_renewal || empty($order->recurring_payment_method) ? __('Manual', 'woocommerce-subscriptions') : $order->recurring_payment_method_title;
        ?>
</div>
	</div>
		<?php 
        if (!WC_Subscriptions::is_woocommerce_pre_2_2()) {
            ?>
</div>
		<?php 
        }
    }