/** * For subscription renewal via cart, preivously adjust item price by original order discount * * No longer required as of 1.3.5 as totals are calculated correctly internally. * * @since 1.3 */ public static function get_discounted_price_for_renewal($price, $values, $cart) { $cart_item = self::cart_contains_subscription_renewal(); if ($cart_item) { $original_order_id = $cart_item['subscription_renewal']['original_order']; $price -= WC_Subscriptions_Order::get_meta($original_order_id, '_order_recurring_discount_cart', 0); } return $price; }
/** * Keep a record of an order's dates if we're marking it as completed during a request to change the payment method. * * @since 1.4 */ public static function store_original_order_dates($new_order_status, $order_id) { if (self::$is_request_to_change_payment) { $order = new WC_Order($order_id); $post = get_post($order_id); self::$original_order_dates = array('_paid_date' => WC_Subscriptions_Order::get_meta($order, 'paid_date'), '_completed_date' => WC_Subscriptions_Order::get_meta($order, 'completed_date'), 'post_date' => $order->order_date, 'post_date_gmt' => $post->post_date_gmt); } return $new_order_status; }
/** * For subscription renewal via cart, previously adjust item price by original order discount * * No longer required as of 1.3.5 as totals are calculated correctly internally. * * @since 2.0 */ public function get_discounted_price_for_renewal($price, $cart_item, $cart) { $cart_item = wcs_cart_contains_renewal(); if ($cart_item) { $original_order_id = $cart_item[$this->cart_item_key]['subscription_id']; $price -= WC_Subscriptions_Order::get_meta($original_order_id, '_order_recurring_discount_cart', 0); } return $price; }
/** * Adds a renewal orders section to the Related Orders meta box displayed on subscription orders. * * @since 1.4 */ public static function renewal_orders_meta_box_section($order, $post) { if (self::is_renewal($order, array('order_role' => 'child'))) { $parent_id = self::get_parent_order_id($order); } elseif (WC_Subscriptions_Order::order_contains_subscription($order)) { $parent_id = $order->id; } //Find any renewal orders associated with this order. $items = get_posts(array('post_type' => $post->post_type, 'post_parent' => $parent_id, 'numberposts' => -1)); if (self::is_renewal($order, array('order_role' => 'child'))) { $parent_order = new WC_Order($parent_id); printf('<p>%1$s <a href="%2$s">%3$s</a></p>', __('Initial Order:', 'woocommerce-subscriptions'), get_edit_post_link($parent_id), $parent_order->get_order_number()); } elseif (self::is_renewal($order, array('order_role' => 'parent'))) { $original_order_id = WC_Subscriptions_Order::get_meta($order, 'original_order', false); $original_order = new WC_Order($original_order_id); printf('<p>%1$s <a href="%2$s">%3$s</a></p>', __('Renewal of Subscription Purchased in Order:', 'woocommerce-subscriptions'), get_edit_post_link($original_order_id), $original_order->get_order_number()); } else { $original_order_post = get_posts(array('meta_key' => '_original_order', 'meta_value' => $parent_id, 'post_parent' => 0, 'post_type' => 'shop_order')); if (!empty($original_order_post) && isset($original_order_post[0])) { $original_order = new WC_Order($original_order_post[0]->ID); printf('<p>%1$s <a href="%2$s">%3$s</a></p>', __('Superseeded by Subscription Purchased in Order:', 'woocommerce-subscriptions'), get_edit_post_link($original_order->id), $original_order->get_order_number()); } } if (empty($items)) { printf(' <p class="renewal-subtitle">%s</p>', __('No renewal payments yet.', 'woocommerce-subscriptions')); } else { printf('<p class="renewal-subtitle">%s</p>', __('Renewal Orders:', 'woocommerce-subscriptions')); echo '<ul class="renewal-orders">'; foreach ($items as $item) { $renewal_order = new WC_Order($item->ID); if ($item->ID == $post->ID) { printf('<li><strong>%s</strong></li>', $renewal_order->get_order_number()); } else { printf('<li><a href="%1$s">%2$s</a></li>', get_edit_post_link($item->ID), $renewal_order->get_order_number()); } } echo '</ul>'; } }
/** * For subscription renewal via cart, use original order discount * * @since 1.3 */ public static function before_calculate_totals($cart) { $cart_item = WC_Subscriptions_Cart::cart_contains_subscription_renewal(); if ($cart_item) { $original_order_id = $cart_item['subscription_renewal']['original_order']; $cart->discount_cart = WC_Subscriptions_Order::get_meta($original_order_id, '_order_recurring_discount_cart', 0); $cart->discount_total = WC_Subscriptions_Order::get_meta($original_order_id, '_order_recurring_discount_total', 0); } }
/** * Version 1.2 introduced a massive change to the order meta data schema. This function goes * through and upgrades the existing data on all orders to the new schema. * * The upgrade process is timeout safe as it keeps a record of the orders upgraded and only * deletes this record once all orders have been upgraded successfully. If operating on a huge * number of orders and the upgrade process times out, only the orders not already upgraded * will be upgraded in future requests that trigger this function. * * @since 1.2 */ private static function upgrade_database_to_1_2() { global $wpdb; set_transient('wc_subscriptions_is_upgrading', 'true', 60 * 2); // Get IDs only and use a direct DB query for efficiency $orders_to_upgrade = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'shop_order' AND post_parent = 0"); $upgraded_orders = get_option('wcs_1_2_upgraded_order_ids', array()); // Transition deprecated subscription status if we aren't in the middle of updating orders if (empty($upgraded_orders)) { $wpdb->query($wpdb->prepare("UPDATE {$wpdb->usermeta} SET meta_value = replace( meta_value, 's:9:\"suspended\"', 's:7:\"on-hold\"' ) WHERE meta_key LIKE %s", '%_' . WC_Subscriptions_Manager::$users_meta_key)); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->usermeta} SET meta_value = replace( meta_value, 's:6:\"failed\"', 's:9:\"cancelled\"' ) WHERE meta_key LIKE %s", '%_' . WC_Subscriptions_Manager::$users_meta_key)); } $orders_to_upgrade = array_diff($orders_to_upgrade, $upgraded_orders); // Upgrade all _sign_up_{field} order meta to new order data format foreach ($orders_to_upgrade as $order_id) { $order = new WC_Order($order_id); // Manually check if a product in an order is a subscription, we can't use WC_Subscriptions_Order::order_contains_subscription( $order ) because it relies on the new data structure $contains_subscription = false; foreach ($order->get_items() as $order_item) { if (WC_Subscriptions_Product::is_subscription(WC_Subscriptions_Order::get_items_product_id($order_item))) { $contains_subscription = true; break; } } if (!$contains_subscription) { continue; } $trial_lengths = WC_Subscriptions_Order::get_meta($order, '_order_subscription_trial_lengths', array()); $trial_length = array_pop($trial_lengths); $has_trial = !empty($trial_length) && $trial_length > 0 ? true : false; $sign_up_fee_total = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_total', 0); // Create recurring_* meta data from existing cart totals $cart_discount = $order->get_cart_discount(); update_post_meta($order_id, '_order_recurring_discount_cart', $cart_discount); $order_discount = $order->get_order_discount(); update_post_meta($order_id, '_order_recurring_discount_total', $order_discount); $order_shipping_tax = get_post_meta($order_id, '_order_shipping_tax', true); update_post_meta($order_id, '_order_recurring_shipping_tax_total', $order_shipping_tax); $order_tax = get_post_meta($order_id, '_order_tax', true); // $order->get_total_tax() includes shipping tax update_post_meta($order_id, '_order_recurring_tax_total', $order_tax); $order_total = $order->get_total(); update_post_meta($order_id, '_order_recurring_total', $order_total); // Set order totals to include sign up fee fields, if there was a sign up fee on the order and a trial period (other wise, the recurring totals are correct) if ($sign_up_fee_total > 0) { // Order totals need to be changed to be equal to sign up fee totals if ($has_trial) { $cart_discount = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_discount_cart', 0); $order_discount = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_discount_total', 0); $order_tax = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_tax_total', 0); $order_total = $sign_up_fee_total; } else { // No trial, sign up fees need to be added to order totals $cart_discount += WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_discount_cart', 0); $order_discount += WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_discount_total', 0); $order_tax += WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_tax_total', 0); $order_total += $sign_up_fee_total; } update_post_meta($order_id, '_order_total', $order_total); update_post_meta($order_id, '_cart_discount', $cart_discount); update_post_meta($order_id, '_order_discount', $order_discount); update_post_meta($order_id, '_order_tax', $order_tax); } // Make sure we get order taxes in WC 1.x format if (false == self::$is_wc_version_2) { $order_taxes = $order->get_taxes(); } else { $order_tax_row = $wpdb->get_row($wpdb->prepare("\n\t\t\t\t\tSELECT * FROM {$wpdb->postmeta}\n\t\t\t\t\tWHERE meta_key = '_order_taxes_old'\n\t\t\t\t\tAND post_id = %s\n\t\t\t\t\t", $order_id)); $order_taxes = (array) maybe_unserialize($order_tax_row->meta_value); } // Set recurring taxes to order taxes, if using WC 2.0, this will be migrated to the new format in @see self::upgrade_to_latest_wc() update_post_meta($order_id, '_order_recurring_taxes', $order_taxes); $sign_up_fee_taxes = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_taxes', array()); // Update order taxes to include sign up fee taxes foreach ($sign_up_fee_taxes as $index => $sign_up_tax) { if ($has_trial && $sign_up_fee_total > 0) { // Order taxes need to be set to the same as the sign up fee taxes if (isset($sign_up_tax['cart_tax']) && $sign_up_tax['cart_tax'] > 0) { $order_taxes[$index]['cart_tax'] = $sign_up_tax['cart_tax']; } } elseif (!$has_trial && $sign_up_fee_total > 0) { // Sign up fee taxes need to be added to order taxes if (isset($sign_up_tax['cart_tax']) && $sign_up_tax['cart_tax'] > 0) { $order_taxes[$index]['cart_tax'] += $sign_up_tax['cart_tax']; } } } if (false == self::$is_wc_version_2) { // Doing it right: updated Subs *before* updating WooCommerce, the WooCommerce updater will take care of data migration update_post_meta($order_id, '_order_taxes', $order_taxes); } else { // Doing it wrong: updated Subs *after* updating WooCommerce, need to store in WC2.0 tax structure $index = 0; $new_order_taxes = $order->get_taxes(); foreach ($new_order_taxes as $item_id => $order_tax) { $index = $index + 1; if (!isset($order_taxes[$index]['label']) || !isset($order_taxes[$index]['cart_tax']) || !isset($order_taxes[$index]['shipping_tax'])) { continue; } // Add line item meta if ($item_id) { woocommerce_update_order_item_meta($item_id, 'compound', absint(isset($order_taxes[$index]['compound']) ? $order_taxes[$index]['compound'] : 0)); woocommerce_update_order_item_meta($item_id, 'tax_amount', woocommerce_clean($order_taxes[$index]['cart_tax'])); woocommerce_update_order_item_meta($item_id, 'shipping_tax_amount', woocommerce_clean($order_taxes[$index]['shipping_tax'])); } } } /* Upgrade each order item to use new Item Meta schema */ $order_subscription_periods = WC_Subscriptions_Order::get_meta($order_id, '_order_subscription_periods', array()); $order_subscription_intervals = WC_Subscriptions_Order::get_meta($order_id, '_order_subscription_intervals', array()); $order_subscription_lengths = WC_Subscriptions_Order::get_meta($order_id, '_order_subscription_lengths', array()); $order_subscription_trial_lengths = WC_Subscriptions_Order::get_meta($order_id, '_order_subscription_trial_lengths', array()); $order_items = $order->get_items(); foreach ($order_items as $index => $order_item) { $product_id = WC_Subscriptions_Order::get_items_product_id($order_item); $item_meta = new WC_Order_Item_Meta($order_item['item_meta']); $subscription_interval = isset($order_subscription_intervals[$product_id]) ? $order_subscription_intervals[$product_id] : 1; $subscription_length = isset($order_subscription_lengths[$product_id]) ? $order_subscription_lengths[$product_id] : 0; $subscription_trial_length = isset($order_subscription_trial_lengths[$product_id]) ? $order_subscription_trial_lengths[$product_id] : 0; $subscription_sign_up_fee = WC_Subscriptions_Order::get_meta($order, '_cart_contents_sign_up_fee_total', 0); if ($sign_up_fee_total > 0) { // Discounted price * Quantity $sign_up_fee_line_total = WC_Subscriptions_Order::get_meta($order, '_cart_contents_sign_up_fee_total', 0); $sign_up_fee_line_tax = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_tax_total', 0); // Base price * Quantity $sign_up_fee_line_subtotal = WC_Subscriptions_Order::get_meta($order, '_cart_contents_sign_up_fee_total', 0) + WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_discount_cart', 0); $sign_up_fee_propotion = $sign_up_fee_line_total > 0 ? $sign_up_fee_line_subtotal / $sign_up_fee_line_total : 0; $sign_up_fee_line_subtotal_tax = WC_Subscriptions_Manager::get_amount_from_proportion(WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_tax_total', 0), $sign_up_fee_propotion); if ($has_trial) { // Set line item totals equal to sign up fee totals $order_item['line_subtotal'] = $sign_up_fee_line_subtotal; $order_item['line_subtotal_tax'] = $sign_up_fee_line_subtotal_tax; $order_item['line_total'] = $sign_up_fee_line_total; $order_item['line_tax'] = $sign_up_fee_line_tax; } else { // No trial period, sign up fees need to be added to order totals $order_item['line_subtotal'] += $sign_up_fee_line_subtotal; $order_item['line_subtotal_tax'] += $sign_up_fee_line_subtotal_tax; $order_item['line_total'] += $sign_up_fee_line_total; $order_item['line_tax'] += $sign_up_fee_line_tax; } } // Upgrading with WC 1.x if (method_exists($item_meta, 'add')) { $item_meta->add('_subscription_period', $order_subscription_periods[$product_id]); $item_meta->add('_subscription_interval', $subscription_interval); $item_meta->add('_subscription_length', $subscription_length); $item_meta->add('_subscription_trial_length', $subscription_trial_length); $item_meta->add('_subscription_recurring_amount', $order_item['line_subtotal']); // WC_Subscriptions_Product::get_price() would return a price without filters applied $item_meta->add('_subscription_sign_up_fee', $subscription_sign_up_fee); // Set recurring amounts for the item $item_meta->add('_recurring_line_total', $order_item['line_total']); $item_meta->add('_recurring_line_tax', $order_item['line_tax']); $item_meta->add('_recurring_line_subtotal', $order_item['line_subtotal']); $item_meta->add('_recurring_line_subtotal_tax', $order_item['line_subtotal_tax']); $order_item['item_meta'] = $item_meta->meta; $order_items[$index] = $order_item; } else { // Ignoring all advice, upgrading 4 months after version 1.2 was released, and doing it with WC 2.0 installed woocommerce_add_order_item_meta($index, '_subscription_period', $order_subscription_periods[$product_id]); woocommerce_add_order_item_meta($index, '_subscription_interval', $subscription_interval); woocommerce_add_order_item_meta($index, '_subscription_length', $subscription_length); woocommerce_add_order_item_meta($index, '_subscription_trial_length', $subscription_trial_length); woocommerce_add_order_item_meta($index, '_subscription_trial_period', $order_subscription_periods[$product_id]); woocommerce_add_order_item_meta($index, '_subscription_recurring_amount', $order_item['line_subtotal']); woocommerce_add_order_item_meta($index, '_subscription_sign_up_fee', $subscription_sign_up_fee); // Calculated recurring amounts for the item woocommerce_add_order_item_meta($index, '_recurring_line_total', $order_item['line_total']); woocommerce_add_order_item_meta($index, '_recurring_line_tax', $order_item['line_tax']); woocommerce_add_order_item_meta($index, '_recurring_line_subtotal', $order_item['line_subtotal']); woocommerce_add_order_item_meta($index, '_recurring_line_subtotal_tax', $order_item['line_subtotal_tax']); if ($sign_up_fee_total > 0) { // Order totals have changed woocommerce_update_order_item_meta($index, '_line_subtotal', woocommerce_format_decimal($order_item['line_subtotal'])); woocommerce_update_order_item_meta($index, '_line_subtotal_tax', woocommerce_format_decimal($order_item['line_subtotal_tax'])); woocommerce_update_order_item_meta($index, '_line_total', woocommerce_format_decimal($order_item['line_total'])); woocommerce_update_order_item_meta($index, '_line_tax', woocommerce_format_decimal($order_item['line_tax'])); } } } // Save the new meta on the order items for WC 1.x (the API functions already saved the data for WC2.x) if (false == self::$is_wc_version_2) { update_post_meta($order_id, '_order_items', $order_items); } $upgraded_orders[] = $order_id; update_option('wcs_1_2_upgraded_order_ids', $upgraded_orders); } // Remove the lock on upgrading delete_transient('wc_subscriptions_is_upgrading'); }
/** * Version 1.2 introduced a massive change to the order meta data schema. This function goes * through and upgrades the existing data on all orders to the new schema. * * The upgrade process is timeout safe as it keeps a record of the orders upgraded and only * deletes this record once all orders have been upgraded successfully. If operating on a huge * number of orders and the upgrade process times out, only the orders not already upgraded * will be upgraded in future requests that trigger this function. * * @since 1.2 */ private static function upgrade_database() { global $wpdb; set_transient('wc_subscriptions_is_upgrading', 'true', 60 * 2); // Get IDs only and use a direct DB query for efficiency $orders_to_upgrade = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'shop_order' AND post_parent = 0"); $upgraded_orders = get_option('wcs_1_2_upgraded_order_ids', array()); // Transition deprecated subscription status if we aren't in the middle of updating orders if (empty($upgraded_orders)) { $wpdb->query($wpdb->prepare("UPDATE {$wpdb->usermeta} SET meta_value = replace( meta_value, 's:9:\"suspended\"', 's:7:\"on-hold\"' ) WHERE meta_key LIKE %s", '%_' . WC_Subscriptions_Manager::$users_meta_key)); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->usermeta} SET meta_value = replace( meta_value, 's:6:\"failed\"', 's:9:\"cancelled\"' ) WHERE meta_key LIKE %s", '%_' . WC_Subscriptions_Manager::$users_meta_key)); } $orders_to_upgrade = array_diff($orders_to_upgrade, $upgraded_orders); // Upgrade all _sign_up_{field} order meta to new order data format foreach ($orders_to_upgrade as $order_id) { $order = new WC_Order($order_id); // Manually check if a product in an order is a subscription, we can't use WC_Subscriptions_Order::order_contains_subscription( $order ) because it relies on the new data structure $contains_subscription = false; foreach ($order->get_items() as $order_item) { if (WC_Subscriptions_Product::is_subscription($order_item['id'])) { $contains_subscription = true; break; } } if (!$contains_subscription) { continue; } $trial_lengths = WC_Subscriptions_Order::get_meta($order, '_order_subscription_trial_lengths', array()); $trial_length = array_pop($trial_lengths); $has_trial = !empty($trial_length) && $trial_length > 0 ? true : false; $sign_up_fee_total = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_total', 0); // Create recurring_* meta data from existing cart totals $cart_discount = $order->get_cart_discount(); update_post_meta($order_id, '_order_recurring_discount_cart', $cart_discount); $order_discount = $order->get_order_discount(); update_post_meta($order_id, '_order_recurring_discount_total', $order_discount); $order_shipping_tax = get_post_meta($order_id, '_order_shipping_tax', true); update_post_meta($order_id, '_order_recurring_shipping_tax_total', $order_shipping_tax); $order_tax = get_post_meta($order_id, '_order_tax', true); // $order->get_total_tax() includes shipping tax update_post_meta($order_id, '_order_recurring_tax_total', $order_tax); $order_total = $order->get_total(); update_post_meta($order_id, '_order_recurring_total', $order_total); // Set order totals to include sign up fee fields, if there was a sign up fee on the order and a trial period (other wise, the recurring totals are correct) if ($sign_up_fee_total > 0) { // Order totals need to be changed to be equal to sign up fee totals if ($has_trial) { $cart_discount = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_discount_cart', 0); $order_discount = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_discount_total', 0); $order_tax = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_tax_total', 0); $order_total = $sign_up_fee_total; } else { // No trial, sign up fees need to be added to order totals $cart_discount += WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_discount_cart', 0); $order_discount += WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_discount_total', 0); $order_tax += WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_tax_total', 0); $order_total += $sign_up_fee_total; } update_post_meta($order_id, '_order_total', $order_total); update_post_meta($order_id, '_cart_discount', $cart_discount); update_post_meta($order_id, '_order_discount', $order_discount); update_post_meta($order_id, '_order_tax', $order_tax); } $order_taxes = $order->get_taxes(); // Set recurring taxes to order taxes update_post_meta($order_id, '_order_recurring_taxes', $order_taxes); $sign_up_fee_taxes = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_taxes', array()); // Update order taxes to include sign up fee taxes foreach ($sign_up_fee_taxes as $index => $sign_up_tax) { if ($has_trial && $sign_up_fee_total > 0) { // Order taxes need to be set to the same as the sign up fee taxes if (isset($sign_up_tax['cart_tax']) && $sign_up_tax['cart_tax'] > 0) { $order_taxes[$index]['cart_tax'] = $sign_up_tax['cart_tax']; } } elseif (!$has_trial && $sign_up_fee_total > 0) { // Sign up fee taxes need to be added to order taxes if (isset($sign_up_tax['cart_tax']) && $sign_up_tax['cart_tax'] > 0) { $order_taxes[$index]['cart_tax'] += $sign_up_tax['cart_tax']; } } } update_post_meta($order_id, '_order_taxes', $order_taxes); /* Upgrade each order item to use new Item Meta schema */ $order_subscription_periods = WC_Subscriptions_Order::get_meta($order_id, '_order_subscription_periods', array()); $order_subscription_intervals = WC_Subscriptions_Order::get_meta($order_id, '_order_subscription_intervals', array()); $order_subscription_lengths = WC_Subscriptions_Order::get_meta($order_id, '_order_subscription_lengths', array()); $order_subscription_trial_lengths = WC_Subscriptions_Order::get_meta($order_id, '_order_subscription_trial_lengths', array()); $order_items = $order->get_items(); foreach ($order_items as $index => $order_item) { $item_meta = new WC_Order_Item_Meta($order_item['item_meta']); $subscription_interval = isset($order_subscription_intervals[$order_item['id']]) ? $order_subscription_intervals[$order_item['id']] : 1; $subscription_length = isset($order_subscription_lengths[$order_item['id']]) ? $order_subscription_lengths[$order_item['id']] : 0; $subscription_trial_length = isset($order_subscription_trial_lengths[$order_item['id']]) ? $order_subscription_trial_lengths[$order_item['id']] : 0; $item_meta->add('_subscription_period', $order_subscription_periods[$order_item['id']]); $item_meta->add('_subscription_interval', $subscription_interval); $item_meta->add('_subscription_length', $subscription_length); $item_meta->add('_subscription_trial_length', $subscription_trial_length); $item_meta->add('_subscription_recurring_amount', $order_item['line_subtotal']); // WC_Subscriptions_Product::get_price() would return a price without filters applied $item_meta->add('_subscription_sign_up_fee', WC_Subscriptions_Order::get_meta($order, '_cart_contents_sign_up_fee_total', 0)); // Set recurring amounts for the item $item_meta->add('_recurring_line_total', $order_item['line_total']); $item_meta->add('_recurring_line_tax', $order_item['line_tax']); $item_meta->add('_recurring_line_subtotal', $order_item['line_subtotal']); $item_meta->add('_recurring_line_subtotal_tax', $order_item['line_subtotal_tax']); if ($sign_up_fee_total > 0) { // Discounted price * Quantity $sign_up_fee_line_total = WC_Subscriptions_Order::get_meta($order, '_cart_contents_sign_up_fee_total', 0); $sign_up_fee_line_tax = WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_tax_total', 0); // Base price * Quantity $sign_up_fee_line_subtotal = WC_Subscriptions_Order::get_meta($order, '_cart_contents_sign_up_fee_total', 0) + WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_discount_cart', 0); $sign_up_fee_propotion = $sign_up_fee_line_total > 0 ? $sign_up_fee_line_subtotal / $sign_up_fee_line_total : 0; $sign_up_fee_line_subtotal_tax = WC_Subscriptions_Manager::get_amount_from_proportion(WC_Subscriptions_Order::get_meta($order, '_sign_up_fee_tax_total', 0), $sign_up_fee_propotion); if ($has_trial) { // Set line item totals equal to sign up fee totals $order_item['line_subtotal'] = $sign_up_fee_line_subtotal; $order_item['line_subtotal_tax'] = $sign_up_fee_line_subtotal_tax; $order_item['line_total'] = $sign_up_fee_line_total; $order_item['line_tax'] = $sign_up_fee_line_tax; } else { // No trial period, sign up fees need to be added to order totals $order_item['line_subtotal'] += $sign_up_fee_line_subtotal; $order_item['line_subtotal_tax'] += $sign_up_fee_line_subtotal_tax; $order_item['line_total'] += $sign_up_fee_line_total; $order_item['line_tax'] += $sign_up_fee_line_tax; } } $order_item['item_meta'] = $item_meta->meta; $order_items[$index] = $order_item; } // Save the new meta on the order items update_post_meta($order_id, '_order_items', $order_items); $upgraded_orders[] = $order_id; update_option('wcs_1_2_upgraded_order_ids', $upgraded_orders); } // Remove the lock on upgrading delete_transient('wc_subscriptions_is_upgrading'); }