/**
  * Make sure the expiration date is calculated from the synced start date for products where the start date
  * will be synced.
  *
  * @param string $expiration_date MySQL formatted date on which the subscription is set to expire
  * @param mixed $product_id The product/post ID of the subscription
  * @param mixed $from_date A MySQL formatted date/time string from which to calculate the expiration date, or empty (default), which will use today's date/time.
  * @since 1.5
  */
 public static function recalculate_product_expiration_date($expiration_date, $product_id, $from_date)
 {
     if (self::is_product_synced($product_id) && ($subscription_length = WC_Subscriptions_Product::get_length($product_id)) > 0) {
         $subscription_period = WC_Subscriptions_Product::get_period($product_id);
         $first_payment_date = self::calculate_first_payment_date($product_id, 'timestamp');
         $expiration_date = date('Y-m-d H:i:s', wcs_add_time($subscription_length, $subscription_period, $first_payment_date));
     }
     return $expiration_date;
 }
 /**
  * When a new order is inserted, add the subscriptions period to the order. 
  * 
  * It's important that the period is tied to the order so that changing the products
  * period does not change the past. 
  *
  * @since 1.0
  */
 public static function add_order_meta($order_id)
 {
     global $woocommerce;
     if (WC_Subscriptions_Order::order_contains_subscription($order_id)) {
         $order = new WC_Order($order_id);
         $order_subscription_periods = array();
         $order_subscription_intervals = array();
         $order_subscription_lengths = array();
         $order_subscription_trial_lengths = array();
         foreach ($order->get_items() as $item) {
             $period = WC_Subscriptions_Product::get_period($item['id']);
             if (!empty($period)) {
                 $order_subscription_periods[$item['id']] = $period;
             }
             $interval = WC_Subscriptions_Product::get_interval($item['id']);
             if (!empty($interval)) {
                 $order_subscription_intervals[$item['id']] = $interval;
             }
             $length = WC_Subscriptions_Product::get_length($item['id']);
             if (!empty($length)) {
                 $order_subscription_lengths[$item['id']] = $length;
             }
             $trial_length = WC_Subscriptions_Product::get_trial_length($item['id']);
             if (!empty($trial_length)) {
                 $order_subscription_trial_lengths[$item['id']] = $trial_length;
             }
         }
         update_post_meta($order_id, '_order_subscription_periods', $order_subscription_periods);
         update_post_meta($order_id, '_order_subscription_intervals', $order_subscription_intervals);
         update_post_meta($order_id, '_order_subscription_lengths', $order_subscription_lengths);
         update_post_meta($order_id, '_order_subscription_trial_lengths', $order_subscription_trial_lengths);
         // Store sign-up fee details
         foreach (WC_Subscriptions_Cart::get_sign_up_fee_fields() as $field_name) {
             update_post_meta($order_id, "_{$field_name}", $woocommerce->cart->{$field_name});
         }
         // Prepare sign up fee taxes to store in same format as order taxes
         $sign_up_fee_taxes = array();
         foreach (array_keys($woocommerce->cart->sign_up_fee_taxes) as $key) {
             $is_compound = $woocommerce->cart->tax->is_compound($key) ? 1 : 0;
             $sign_up_fee_taxes[] = array('label' => $woocommerce->cart->tax->get_rate_label($key), 'compound' => $is_compound, 'cart_tax' => number_format($woocommerce->cart->sign_up_fee_taxes[$key], 2, '.', ''));
         }
         update_post_meta($order_id, '_sign_up_fee_taxes', $sign_up_fee_taxes);
     }
 }
 /**
  * Gets the subscription length from the cart and returns it as an array (eg. array( 'month', 'day' ) )
  *
  * @since 1.0
  */
 public static function get_cart_subscription_length()
 {
     global $woocommerce;
     $length = 0;
     foreach ($woocommerce->cart->cart_contents as $cart_item) {
         $item_id = empty($cart_item['variation_id']) ? $cart_item['product_id'] : $cart_item['variation_id'];
         if (isset($cart_item['data']->subscription_length)) {
             $length = $cart_item['data']->subscription_length;
             break;
         } elseif (WC_Subscriptions_Product::is_subscription($item_id)) {
             $length = WC_Subscriptions_Product::get_length($item_id);
             break;
         }
     }
     return apply_filters('woocommerce_subscriptions_cart_length', $length);
 }
 /**
  * Set the subscription prices to be used in calculating totals by @see WC_Subscriptions_Cart::calculate_subscription_totals()
  *
  * @since 1.4
  */
 public static function maybe_set_apporitioned_totals($total)
 {
     global $woocommerce;
     if (false === self::cart_contains_subscription_switch()) {
         return $total;
     }
     // Maybe charge an initial amount to account for upgrading from a cheaper subscription
     $apportion_recurring_price = get_option(WC_Subscriptions_Admin::$option_prefix . '_apportion_recurring_price', 'no');
     $apportion_sign_up_fee = get_option(WC_Subscriptions_Admin::$option_prefix . '_apportion_sign_up_fee', 'no');
     $apportion_length = get_option(WC_Subscriptions_Admin::$option_prefix . '_apportion_length', 'no');
     foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
         if (!isset($cart_item['subscription_switch']['subscription_key'])) {
             continue;
         }
         $old_subscription_key = $cart_item['subscription_switch']['subscription_key'];
         $item_data = $cart_item['data'];
         $product_id = empty($cart_item['variation_id']) ? $cart_item['product_id'] : $cart_item['variation_id'];
         $product = get_product($product_id);
         // Set the date on which the first payment for the new subscription should be charged
         $woocommerce->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] = $cart_item['subscription_switch']['next_payment_timestamp'];
         $is_virtual_product = 'no' != $item_data->virtual ? true : false;
         // Add any extra sign up fees required to switch to the new subscription
         if ('yes' == $apportion_sign_up_fee) {
             $old_subscription = WC_Subscriptions_Manager::get_subscription($old_subscription_key);
             $sign_up_fee_due = $product->subscription_sign_up_fee;
             if (self::order_contains_subscription_switch($old_subscription['order_id'])) {
                 $old_order = new WC_Order($old_subscription['order_id']);
                 $sign_up_fee_paid = $old_order->get_total();
             } else {
                 $sign_up_fee_paid = WC_Subscriptions_Order::get_item_sign_up_fee($old_subscription['order_id'], $old_subscription['product_id']);
             }
             $woocommerce->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee = max($sign_up_fee_due - $sign_up_fee_paid, 0);
         } elseif ('no' == $apportion_sign_up_fee) {
             // $0 the initial sign-up fee
             $woocommerce->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee = 0;
         }
         // Now lets see if we should add an apportioned amount to the sign-up fee (for upgrades) or extend the next payment date (for downgrades)
         if ('yes' == $apportion_recurring_price || 'virtual' == $apportion_recurring_price && $is_virtual_product) {
             // Get the current subscription
             $old_subscription = isset($old_subscription) ? $old_subscription : WC_Subscriptions_Manager::get_subscription($old_subscription_key);
             // Get the current subscription's original order
             $old_order = isset($old_order) ? $old_order : new WC_Order($old_subscription['order_id']);
             // Get the current subscription's last payment date
             $last_payment_timestamp = WC_Subscriptions_Manager::get_last_payment_date($old_subscription_key, get_current_user_id(), 'timestamp');
             $days_since_last_payment = round((gmdate('U') - $last_payment_timestamp) / (60 * 60 * 24));
             // Get the current subscription's next payment date
             $next_payment_timestamp = $cart_item['subscription_switch']['next_payment_timestamp'];
             $days_until_next_payment = round(($next_payment_timestamp - gmdate('U')) / (60 * 60 * 24));
             // Find the number of days between the two
             $days_in_old_cycle = $days_until_next_payment + $days_since_last_payment;
             // Find the $price per day for the old subscription's recurring total
             $old_recurring_total = WC_Subscriptions_Order::get_item_recurring_amount($old_subscription['order_id'], $old_subscription['product_id']);
             $old_price_per_day = $old_recurring_total / $days_in_old_cycle;
             // Find the price per day for the new subscription's recurring total
             // If the subscription uses the same billing interval & cycle as the old subscription,
             if ($item_data->subscription_period == $old_subscription['period'] && $item_data->subscription_period_interval == $old_subscription['interval']) {
                 $days_in_new_cycle = $days_in_old_cycle;
                 // Use $days_in_old_cycle to make sure they're consistent
                 $new_price_per_day = $product->subscription_price / $days_in_old_cycle;
             } else {
                 // We need to figure out the price per day for the new subscription based on its billing schedule
                 switch ($item_data->subscription_period) {
                     case 'day':
                         $days_in_new_cycle = $item_data->subscription_period_interval;
                         break;
                     case 'week':
                         $days_in_new_cycle = $item_data->subscription_period_interval * 7;
                         break;
                     case 'month':
                         $days_in_new_cycle = $item_data->subscription_period_interval * 30.4375;
                         // Average days per month over 4 year period
                         break;
                     case 'year':
                         $days_in_new_cycle = $item_data->subscription_period_interval * 365.25;
                         // Average days per year over 4 year period
                         break;
                 }
                 $new_price_per_day = $product->subscription_price / $days_in_new_cycle;
             }
             // If the customer is upgrading, we may need to add a gap payment to the sign-up fee or to reduce the pre-paid period (or both)
             if ($old_price_per_day < $new_price_per_day) {
                 // The new subscription may be more expensive, but it's also on a shorter billing cycle, so reduce the next pre-paid term
                 if ($days_in_old_cycle > $days_in_new_cycle) {
                     // Find out how many days at the new price per day the customer would receive for the total amount already paid
                     // (e.g. if the customer paid $10 / month previously, and was switching to a $5 / week subscription, she has pre-paid 14 days at the new price)
                     $pre_paid_days = 0;
                     do {
                         $pre_paid_days++;
                         $new_total_paid = $pre_paid_days * $new_price_per_day;
                     } while ($new_total_paid < $old_recurring_total);
                     // If the total amount the customer has paid entitles her to more days at the new price than she has received, there is no gap payment, just shorten the pre-paid term the appropriate number of days
                     if ($days_since_last_payment < $pre_paid_days) {
                         $woocommerce->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] = $last_payment_timestamp + $pre_paid_days * 60 * 60 * 24;
                         // If the total amount the customer has paid entitles her to the same or less days at the new price then start the new subscription from today
                     } else {
                         $woocommerce->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] = 0;
                     }
                 } else {
                     $extra_to_pay = $days_until_next_payment * ($new_price_per_day - $old_price_per_day);
                     $woocommerce->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee += round($extra_to_pay, 2);
                 }
                 $woocommerce->cart->cart_contents[$cart_item_key]['subscription_switch']['upgraded_or_downgraded'] = 'upgraded';
                 // If the customer is downgrading, we need to extend the next payment date for n more days
             } elseif ($old_price_per_day > $new_price_per_day && $new_price_per_day > 0) {
                 $old_total_paid = $old_price_per_day * $days_until_next_payment;
                 $new_total_paid = $new_price_per_day;
                 // Find how many more days at the new lower price it takes to exceed the amount already paid
                 for ($days_to_add = 1; $new_total_paid <= $old_total_paid; $days_to_add++) {
                     $new_total_paid = $days_to_add * $new_price_per_day;
                 }
                 $days_to_add -= $days_until_next_payment;
                 $woocommerce->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] = $next_payment_timestamp + $days_to_add * 60 * 60 * 24;
                 $woocommerce->cart->cart_contents[$cart_item_key]['subscription_switch']['upgraded_or_downgraded'] = 'downgraded';
             }
             // The old price per day == the new price per day, no need to change anything
         }
         // Finally, if we need to make sure the initial total doesn't include any recurring amount, we can by spoofing a free trial
         if (0 != $woocommerce->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp']) {
             $woocommerce->cart->cart_contents[$cart_item_key]['data']->subscription_trial_length = 1;
         }
         if ('yes' == $apportion_length || 'virtual' == $apportion_length && $is_virtual_product) {
             // Maybe charge an initial amount to account for upgrading from a cheaper subscription
             $base_length = WC_Subscriptions_Product::get_length($product_id);
             $completed_payments = WC_Subscriptions_Manager::get_subscriptions_completed_payment_count($old_subscription_key);
             $length_remaining = $base_length - $completed_payments;
             // Default to the base length if more payments have already been made than this subscription requires
             if ($length_remaining < 0) {
                 $length_remaining = $base_length;
             }
             $woocommerce->cart->cart_contents[$cart_item_key]['data']->subscription_length = $length_remaining;
         }
     }
     return $total;
 }
 /**
  * Add subscription related order item meta when a subscription product is added as an item to an order via Ajax.
  *
  * @param item_id int An order_item_id as returned by the insert statement of @see woocommerce_add_order_item()
  * @since 1.2.5
  * @version 1.4
  * @return void
  */
 public static function prefill_order_item_meta($item, $item_id)
 {
     $order_id = $_POST['order_id'];
     $product_id = $item['variation_id'] ? $item['variation_id'] : $item['product_id'];
     if ($item_id && WC_Subscriptions_Product::is_subscription($product_id)) {
         $order = new WC_Order($order_id);
         $_product = get_product($product_id);
         $recurring_amount = $_product->get_price_excluding_tax();
         $sign_up_fee = $_product->get_sign_up_fee_excluding_tax();
         $free_trial_length = WC_Subscriptions_Product::get_trial_length($product_id);
         woocommerce_add_order_item_meta($item_id, '_subscription_period', WC_Subscriptions_Product::get_period($product_id));
         woocommerce_add_order_item_meta($item_id, '_subscription_interval', WC_Subscriptions_Product::get_interval($product_id));
         woocommerce_add_order_item_meta($item_id, '_subscription_length', WC_Subscriptions_Product::get_length($product_id));
         woocommerce_add_order_item_meta($item_id, '_subscription_trial_length', $free_trial_length);
         woocommerce_add_order_item_meta($item_id, '_subscription_trial_period', WC_Subscriptions_Product::get_trial_period($product_id));
         woocommerce_add_order_item_meta($item_id, '_subscription_recurring_amount', $recurring_amount);
         woocommerce_add_order_item_meta($item_id, '_subscription_sign_up_fee', $sign_up_fee);
         woocommerce_add_order_item_meta($item_id, '_recurring_line_total', $recurring_amount);
         woocommerce_add_order_item_meta($item_id, '_recurring_line_tax', 0);
         woocommerce_add_order_item_meta($item_id, '_recurring_line_subtotal', $recurring_amount);
         woocommerce_add_order_item_meta($item_id, '_recurring_line_subtotal_tax', 0);
         WC_Subscriptions_Manager::create_pending_subscription_for_order($order_id, $item['product_id']);
         switch ($order->status) {
             case 'completed':
             case 'processing':
                 woocommerce_update_order_item_meta($item_id, '_subscription_status', 'active');
                 break;
             case 'on-hold':
                 woocommerce_update_order_item_meta($item_id, '_subscription_status', 'on-hold');
                 break;
             case 'failed':
             case 'cancelled':
                 woocommerce_add_order_item_meta($item_id, '_subscription_status', 'cancelled');
                 break;
         }
         // We need to override the line totals to $0 when there is a free trial
         if ($free_trial_length > 0 || $sign_up_fee > 0) {
             $line_total_keys = array('line_subtotal', 'line_total');
             // Make sure sign up fees are included in the total (or $0 if no sign up fee and a free trial)
             foreach ($line_total_keys as $line_total_key) {
                 $item[$line_total_key] = $sign_up_fee;
             }
             // If there is no free trial, make sure line totals include sign up fee and recurring fees
             if (0 == $free_trial_length) {
                 foreach ($line_total_keys as $line_total_key) {
                     $item[$line_total_key] += $recurring_amount;
                 }
             }
             foreach ($line_total_keys as $line_total_key) {
                 $item[$line_total_key] = WC_Subscriptions::format_total($item[$line_total_key], 2);
             }
         } else {
             $item['line_subtotal'] = $recurring_amount;
             $item['line_total'] = $recurring_amount;
         }
         woocommerce_update_order_item_meta($item_id, '_line_subtotal', $item['line_subtotal']);
         woocommerce_update_order_item_meta($item_id, '_line_total', $item['line_total']);
     }
     return $item;
 }
 /**
  * Set the subscription prices to be used in calculating totals by @see WC_Subscriptions_Cart::calculate_subscription_totals()
  *
  * @since 2.0
  */
 public static function calculate_prorated_totals($cart)
 {
     if (false === self::cart_contains_switches()) {
         return;
     }
     // Maybe charge an initial amount to account for upgrading from a cheaper subscription
     $apportion_recurring_price = get_option(WC_Subscriptions_Admin::$option_prefix . '_apportion_recurring_price', 'no');
     $apportion_sign_up_fee = get_option(WC_Subscriptions_Admin::$option_prefix . '_apportion_sign_up_fee', 'no');
     $apportion_length = get_option(WC_Subscriptions_Admin::$option_prefix . '_apportion_length', 'no');
     foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
         if (!isset($cart_item['subscription_switch']['subscription_id'])) {
             continue;
         }
         $subscription = wcs_get_subscription($cart_item['subscription_switch']['subscription_id']);
         $existing_item = wcs_get_order_item($cart_item['subscription_switch']['item_id'], $subscription);
         if (empty($existing_item)) {
             WC()->cart->remove_cart_item($cart_item_key);
             continue;
         }
         $item_data = $cart_item['data'];
         $product_id = wcs_get_canonical_product_id($cart_item);
         $product = wc_get_product($product_id);
         $is_virtual_product = $product->is_virtual();
         // Set when the first payment and end date for the new subscription should occur
         WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] = $cart_item['subscription_switch']['next_payment_timestamp'];
         WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['end_timestamp'] = $end_timestamp = strtotime(WC_Subscriptions_Product::get_expiration_date($product_id, $subscription->get_date('last_payment')));
         // Add any extra sign up fees required to switch to the new subscription
         if ('yes' == $apportion_sign_up_fee) {
             // Because product add-ons etc. don't apply to sign-up fees, it's safe to use the product's sign-up fee value rather than the cart item's
             $sign_up_fee_due = $product->subscription_sign_up_fee;
             $sign_up_fee_paid = $subscription->get_items_sign_up_fee($existing_item, 'inclusive_of_tax');
             // Make sure total prorated sign-up fee is prorated across total amount of sign-up fee so that customer doesn't get extra discounts
             if ($cart_item['quantity'] > $existing_item['qty']) {
                 $sign_up_fee_paid = $sign_up_fee_paid * $existing_item['qty'] / $cart_item['quantity'];
             }
             WC()->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee = max($sign_up_fee_due - $sign_up_fee_paid, 0);
             WC()->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee_prorated = WC()->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee;
         } elseif ('no' == $apportion_sign_up_fee) {
             // $0 the initial sign-up fee
             WC()->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee = 0;
         }
         // Get the current subscription's last payment date
         $last_payment_timestamp = $subscription->get_time('last_payment');
         $days_since_last_payment = floor((gmdate('U') - $last_payment_timestamp) / (60 * 60 * 24));
         // Get the current subscription's next payment date
         $next_payment_timestamp = $cart_item['subscription_switch']['next_payment_timestamp'];
         $days_until_next_payment = ceil(($next_payment_timestamp - gmdate('U')) / (60 * 60 * 24));
         // If the subscription contains a synced product and the next payment is actually the first payment, determine the days in the "old" cycle from the subscription object
         if (WC_Subscriptions_Synchroniser::subscription_contains_synced_product($subscription->id) && WC_Subscriptions_Synchroniser::calculate_first_payment_date($product, 'timestamp', $subscription->get_date('start')) == $next_payment_timestamp) {
             $days_in_old_cycle = wcs_get_days_in_cycle($subscription->billing_period, $subscription->billing_interval);
         } else {
             // Find the number of days between the two
             $days_in_old_cycle = $days_until_next_payment + $days_since_last_payment;
         }
         // Find the actual recurring amount charged for the old subscription (we need to use the '_recurring_line_total' meta here rather than '_subscription_recurring_amount' because we want the recurring amount to include extra from extensions, like Product Add-ons etc.)
         $old_recurring_total = $existing_item['line_total'];
         if ('yes' == $subscription->prices_include_tax || true === $subscription->prices_include_tax) {
             // WC_Abstract_Order::$prices_include_tax can be set to true in __construct() or to 'yes' in populate()
             $old_recurring_total += $existing_item['line_tax'];
         }
         // Find the $price per day for the old subscription's recurring total
         $old_price_per_day = $old_recurring_total / $days_in_old_cycle;
         // Find the price per day for the new subscription's recurring total
         // If the subscription uses the same billing interval & cycle as the old subscription,
         if ($item_data->subscription_period == $subscription->billing_period && $item_data->subscription_period_interval == $subscription->billing_interval) {
             $days_in_new_cycle = $days_in_old_cycle;
             // Use $days_in_old_cycle to make sure they're consistent
         } else {
             // We need to figure out the price per day for the new subscription based on its billing schedule
             $days_in_new_cycle = wcs_get_days_in_cycle($item_data->subscription_period, $item_data->subscription_period_interval);
         }
         // We need to use the cart items price to ensure we include extras added by extensions like Product Add-ons
         $new_price_per_day = $item_data->price * $cart_item['quantity'] / $days_in_new_cycle;
         if ($old_price_per_day < $new_price_per_day) {
             WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['upgraded_or_downgraded'] = 'upgraded';
         } elseif ($old_price_per_day > $new_price_per_day && $new_price_per_day >= 0) {
             WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['upgraded_or_downgraded'] = 'downgraded';
         }
         // Now lets see if we should add a prorated amount to the sign-up fee (for upgrades) or extend the next payment date (for downgrades)
         if (in_array($apportion_recurring_price, array('yes', 'yes-upgrade')) || in_array($apportion_recurring_price, array('virtual', 'virtual-upgrade')) && $is_virtual_product) {
             // If the customer is upgrading, we may need to add a gap payment to the sign-up fee or to reduce the pre-paid period (or both)
             if ($old_price_per_day < $new_price_per_day) {
                 // The new subscription may be more expensive, but it's also on a shorter billing cycle, so reduce the next pre-paid term
                 if ($days_in_old_cycle > $days_in_new_cycle) {
                     // Find out how many days at the new price per day the customer would receive for the total amount already paid
                     // (e.g. if the customer paid $10 / month previously, and was switching to a $5 / week subscription, she has pre-paid 14 days at the new price)
                     $pre_paid_days = 0;
                     do {
                         $pre_paid_days++;
                         $new_total_paid = $pre_paid_days * $new_price_per_day;
                     } while ($new_total_paid < $old_recurring_total);
                     // If the total amount the customer has paid entitles her to more days at the new price than she has received, there is no gap payment, just shorten the pre-paid term the appropriate number of days
                     if ($days_since_last_payment < $pre_paid_days) {
                         WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] = $last_payment_timestamp + $pre_paid_days * 60 * 60 * 24;
                         // If the total amount the customer has paid entitles her to the same or less days at the new price then start the new subscription from today
                     } else {
                         WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] = 0;
                     }
                 } else {
                     $extra_to_pay = $days_until_next_payment * ($new_price_per_day - $old_price_per_day);
                     // when calculating a subscription with one length (no more next payment date and the end date may have been pushed back) we need to pay for those extra days at the new price per day between the old next payment date and new end date
                     if (1 == $item_data->subscription_length) {
                         $days_to_new_end = floor(($end_timestamp - $next_payment_timestamp) / (60 * 60 * 24));
                         if ($days_to_new_end > 0) {
                             $extra_to_pay += $days_to_new_end * $new_price_per_day;
                         }
                     }
                     // We need to find the per item extra to pay so we can set it as the sign-up fee (WC will then multiply it by the quantity)
                     $extra_to_pay = $extra_to_pay / $cart_item['quantity'];
                     // Keep a record of the two separate amounts so we store these and calculate future switch amounts correctly
                     WC()->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee_prorated = WC()->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee;
                     WC()->cart->cart_contents[$cart_item_key]['data']->subscription_price_prorated = round($extra_to_pay, 2);
                     WC()->cart->cart_contents[$cart_item_key]['data']->subscription_sign_up_fee += round($extra_to_pay, 2);
                 }
                 // If the customer is downgrading, set the next payment date and maybe extend it if downgrades are prorated
             } elseif ($old_price_per_day > $new_price_per_day && $new_price_per_day > 0) {
                 $old_total_paid = $old_price_per_day * $days_until_next_payment;
                 $new_total_paid = $new_price_per_day;
                 // if downgrades are apportioned, extend the next payment date for n more days
                 if (in_array($apportion_recurring_price, array('virtual', 'yes'))) {
                     // Find how many more days at the new lower price it takes to exceed the amount already paid
                     for ($days_to_add = 0; $new_total_paid <= $old_total_paid; $days_to_add++) {
                         $new_total_paid = $days_to_add * $new_price_per_day;
                     }
                     $days_to_add -= $days_until_next_payment;
                 } else {
                     $days_to_add = 0;
                 }
                 WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] = $next_payment_timestamp + $days_to_add * 60 * 60 * 24;
             }
             // The old price per day == the new price per day, no need to change anything
             if (WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp'] != $cart_item['subscription_switch']['next_payment_timestamp']) {
                 WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['recurring_payment_prorated'] = true;
             }
         }
         // Finally, if we need to make sure the initial total doesn't include any recurring amount, we can by spoofing a free trial
         if (0 != WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['first_payment_timestamp']) {
             WC()->cart->cart_contents[$cart_item_key]['data']->subscription_trial_length = 1;
         }
         if ('yes' == $apportion_length || 'virtual' == $apportion_length && $is_virtual_product) {
             $base_length = WC_Subscriptions_Product::get_length($product_id);
             $completed_payments = $subscription->get_completed_payment_count();
             $length_remaining = $base_length - $completed_payments;
             // Default to the base length if more payments have already been made than this subscription requires
             if ($length_remaining <= 0) {
                 $length_remaining = $base_length;
             }
             WC()->cart->cart_contents[$cart_item_key]['data']->subscription_length = $length_remaining;
         }
     }
 }
 /**
  * Add each subscription product's details to an order so that the state of the subscription persists even when a product is changed
  *
  * @since 1.2.5
  */
 public static function add_order_item_meta($item_id, $values)
 {
     global $woocommerce;
     if (!WC_Subscriptions_Cart::cart_contains_subscription_renewal('child') && WC_Subscriptions_Product::is_subscription($values['product_id'])) {
         $cart_item = $values['data'];
         $product_id = empty($values['variation_id']) ? $values['product_id'] : $values['variation_id'];
         // Add subscription details so order state persists even when a product is changed
         $period = isset($cart_item->subscription_period) ? $cart_item->subscription_period : WC_Subscriptions_Product::get_period($product_id);
         $interval = isset($cart_item->subscription_period_interval) ? $cart_item->subscription_period_interval : WC_Subscriptions_Product::get_interval($product_id);
         $length = isset($cart_item->subscription_length) ? $cart_item->subscription_length : WC_Subscriptions_Product::get_length($product_id);
         $trial_length = isset($cart_item->subscription_trial_length) ? $cart_item->subscription_trial_length : WC_Subscriptions_Product::get_trial_length($product_id);
         $trial_period = isset($cart_item->subscription_trial_period) ? $cart_item->subscription_trial_period : WC_Subscriptions_Product::get_trial_period($product_id);
         $sign_up_fee = isset($cart_item->subscription_sign_up_fee) ? $cart_item->subscription_sign_up_fee : WC_Subscriptions_Product::get_sign_up_fee($product_id);
         woocommerce_add_order_item_meta($item_id, '_subscription_period', $period);
         woocommerce_add_order_item_meta($item_id, '_subscription_interval', $interval);
         woocommerce_add_order_item_meta($item_id, '_subscription_length', $length);
         woocommerce_add_order_item_meta($item_id, '_subscription_trial_length', $trial_length);
         woocommerce_add_order_item_meta($item_id, '_subscription_trial_period', $trial_period);
         woocommerce_add_order_item_meta($item_id, '_subscription_recurring_amount', $woocommerce->cart->base_recurring_prices[$product_id]);
         // WC_Subscriptions_Product::get_price() would return a price without filters applied
         woocommerce_add_order_item_meta($item_id, '_subscription_sign_up_fee', $sign_up_fee);
         // Calculated recurring amounts for the item
         woocommerce_add_order_item_meta($item_id, '_recurring_line_total', $woocommerce->cart->recurring_cart_contents[$values['product_id']]['recurring_line_total']);
         woocommerce_add_order_item_meta($item_id, '_recurring_line_tax', $woocommerce->cart->recurring_cart_contents[$values['product_id']]['recurring_line_tax']);
         woocommerce_add_order_item_meta($item_id, '_recurring_line_subtotal', $woocommerce->cart->recurring_cart_contents[$values['product_id']]['recurring_line_subtotal']);
         woocommerce_add_order_item_meta($item_id, '_recurring_line_subtotal_tax', $woocommerce->cart->recurring_cart_contents[$values['product_id']]['recurring_line_subtotal_tax']);
         // Add recurring line tax data (for WC 2.2+)
         $raw_tax_data = $woocommerce->cart->recurring_cart_contents[$values['product_id']]['recurring_line_tax_data'];
         $recurring_tax_data = array();
         $recurring_tax_data['total'] = array_map('wc_format_decimal', $raw_tax_data['total']);
         $recurring_tax_data['subtotal'] = array_map('wc_format_decimal', $raw_tax_data['subtotal']);
         woocommerce_add_order_item_meta($item_id, '_recurring_line_tax_data', $recurring_tax_data);
     }
 }
Exemplo n.º 8
0
 /**
  * Gets the subscription length from the cart and returns it as an array (eg. array( 'month', 'day' ) )
  *
  * Deprecated because a cart can now contain multiple subscription products, so there is no single length for the entire cart.
  *
  * @since 1.1
  * @deprecated 2.0
  */
 public static function get_cart_subscription_length()
 {
     _deprecated_function(__METHOD__, '2.0', 'values from WC()->cart->recurring_carts');
     $length = 0;
     if (self::cart_contains_subscription()) {
         foreach (WC()->cart->cart_contents as $cart_item) {
             if (isset($cart_item['data']->subscription_length)) {
                 $length = $cart_item['data']->subscription_length;
                 break;
             } elseif (WC_Subscriptions_Product::is_subscription($cart_item['data'])) {
                 $length = WC_Subscriptions_Product::get_length($cart_item['data']);
                 break;
             }
         }
     }
     return apply_filters('woocommerce_subscriptions_cart_length', $length);
 }
 /**
  * Return an associative array of a given subscriptions details (if it exists).
  *
  * @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
  * @param deprecated don't use
  * @return array Subscription details
  * @since 1.1
  */
 public static function get_subscription($subscription_key, $deprecated = null)
 {
     if (null != $deprecated) {
         _deprecated_argument(__METHOD__, '1.4', __('Second parameter is deprecated', 'woocommerce-subscriptions'));
     }
     $item = WC_Subscriptions_Order::get_item_by_subscription_key($subscription_key);
     if (!empty($item)) {
         $subscription = array('order_id' => $item['order_id'], 'product_id' => $item['product_id'], 'variation_id' => $item['variation_id'], 'status' => isset($item['subscription_status']) ? $item['subscription_status'] : 'pending', 'period' => isset($item['subscription_period']) ? $item['subscription_period'] : WC_Subscriptions_Product::get_period($item['product_id']), 'interval' => isset($item['subscription_interval']) ? $item['subscription_interval'] : WC_Subscriptions_Product::get_interval($item['product_id']), 'length' => isset($item['subscription_length']) ? $item['subscription_length'] : WC_Subscriptions_Product::get_length($item['product_id']), 'start_date' => isset($item['subscription_start_date']) ? $item['subscription_start_date'] : 0, 'expiry_date' => isset($item['subscription_expiry_date']) ? $item['subscription_expiry_date'] : 0, 'end_date' => isset($item['subscription_end_date']) ? $item['subscription_end_date'] : 0, 'trial_expiry_date' => isset($item['subscription_trial_expiry_date']) ? $item['subscription_trial_expiry_date'] : 0, 'failed_payments' => isset($item['subscription_failed_payments']) ? $item['subscription_failed_payments'] : 0, 'completed_payments' => isset($item['subscription_completed_payments']) ? $item['subscription_completed_payments'] : array(), 'suspension_count' => isset($item['subscription_suspension_count']) ? $item['subscription_suspension_count'] : 0, 'last_payment_date' => isset($item['subscription_completed_payments']) ? end($item['subscription_completed_payments']) : '');
     } else {
         $subscription = array();
     }
     return apply_filters('woocommerce_get_subscription', $subscription, $subscription_key, $deprecated);
 }
 /**
  * Add each subscription product's details to an order so that the state of the subscription persists even when a product is changed
  *
  * @since 1.2
  */
 public static function add_order_item_meta($order_item)
 {
     global $woocommerce;
     if (WC_Subscriptions_Product::is_subscription($order_item['id'])) {
         // Make sure existing meta persists
         $item_meta = new WC_Order_Item_Meta($order_item['item_meta']);
         // Add subscription details so order state persists even when a product is changed
         $item_meta->add('_subscription_period', WC_Subscriptions_Product::get_period($order_item['id']));
         $item_meta->add('_subscription_interval', WC_Subscriptions_Product::get_interval($order_item['id']));
         $item_meta->add('_subscription_length', WC_Subscriptions_Product::get_length($order_item['id']));
         $item_meta->add('_subscription_trial_length', WC_Subscriptions_Product::get_trial_length($order_item['id']));
         $item_meta->add('_subscription_trial_period', WC_Subscriptions_Product::get_trial_period($order_item['id']));
         $item_meta->add('_subscription_recurring_amount', $woocommerce->cart->base_recurring_prices[$order_item['id']]);
         // WC_Subscriptions_Product::get_price() would return a price without filters applied
         $item_meta->add('_subscription_sign_up_fee', WC_Subscriptions_Product::get_sign_up_fee($order_item['id']));
         // Calculated recurring amounts for the item
         $item_meta->add('_recurring_line_total', $woocommerce->cart->recurring_cart_contents[$order_item['id']]['recurring_line_total']);
         $item_meta->add('_recurring_line_tax', $woocommerce->cart->recurring_cart_contents[$order_item['id']]['recurring_line_tax']);
         $item_meta->add('_recurring_line_subtotal', $woocommerce->cart->recurring_cart_contents[$order_item['id']]['recurring_line_subtotal']);
         $item_meta->add('_recurring_line_subtotal_tax', $woocommerce->cart->recurring_cart_contents[$order_item['id']]['recurring_line_subtotal_tax']);
         $order_item['item_meta'] = $item_meta->meta;
     }
     return $order_item;
 }
 /**
  * Gets the subscription length from the cart and returns it as an array (eg. array( 'month', 'day' ) )
  * 
  * @since 1.0
  */
 public static function get_cart_subscription_length()
 {
     global $woocommerce;
     $length = 0;
     foreach ($woocommerce->cart->cart_contents as $cart_item) {
         if (WC_Subscriptions_Product::is_subscription($cart_item['product_id'])) {
             $length = WC_Subscriptions_Product::get_length($cart_item['product_id']);
             break;
         }
     }
     return $length;
 }
Exemplo n.º 12
0
/** Update Member Plan **/
function upd_mem_plan()
{
    global $current_user, $woocommerce, $wpdb;
    $mess = array();
    $mess['act'] = 'success';
    parse_str($_POST['form_data'], $d);
    get_currentuserinfo();
    $product_id = $d['meal_plan_type'];
    $arr = array();
    $cur_user_subscription = WC_Subscriptions_Manager::get_users_subscriptions(get_current_user_id());
    /* $current_subscription To Cancel all users previous subscription */
    foreach ($cur_user_subscription as $suk => $suv) {
        $orderID = $suv['order_id'];
        $suv['product_id'] = $d['meal_plan'];
        $arr[$suv['order_id'] . '_' . $d['meal_plan']] = $suv;
        update_user_meta(get_current_user_id(), 'wp_woocommerce_subscriptions', $arr);
        break;
    }
    // Create Order (send cart variable so we can record items and reduce inventory). Only create if this is a new order, not if the payment was rejected.
    $product_id = $d['meal_plan_type'];
    $_product = new WC_Product($product_id);
    // Set values
    $item = array();
    // Add line item
    $srOW = $wpdb->get_row("select * FROM wp_woocommerce_order_items WHERE order_id = " . $orderID);
    $item_id = $srOW->order_item_id;
    $args = array('post_type' => 'product', 'post_status' => 'publish');
    $loop = new WP_Query($args);
    $i = 0;
    if ($loop->have_posts()) {
        while ($loop->have_posts()) {
            $loop->the_post();
            $____product = get_product(get_the_ID());
            $_var[get_the_ID()] = $____product->get_available_variations();
        }
    }
    wp_reset_query();
    foreach ($_var as $_k => $_variations) {
        foreach ($_variations as $_vk => $_vv) {
            if ($_vv['variation_id'] == $product_id) {
                $variation_name = $_vv['attributes']['attribute_servings'];
                $p = strip_tags($_vv['price_html']);
            }
        }
    }
    // update post meta for pricing
    update_post_meta($orderID, '_order_total', get_post_meta($product_id, '_price', true));
    update_post_meta($orderID, '_order_recurring_total', get_post_meta($product_id, '_subscription_price', true));
    // Add subscription details so order state persists even when a product is changed
    $period = WC_Subscriptions_Product::get_period($product_id);
    $interval = WC_Subscriptions_Product::get_interval($product_id);
    $length = WC_Subscriptions_Product::get_length($product_id);
    $trial_length = WC_Subscriptions_Product::get_trial_length($product_id);
    $trial_period = WC_Subscriptions_Product::get_trial_period($product_id);
    $sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee($product_id);
    woocommerce_update_order_item_meta($item_id, '_qty', 1);
    woocommerce_update_order_item_meta($item_id, '_tax_class', '');
    woocommerce_update_order_item_meta($item_id, '_product_id', $d['meal_plan']);
    woocommerce_update_order_item_meta($item_id, '_variation_id', $product_id);
    woocommerce_update_order_item_meta($item_id, '_line_subtotal', get_post_meta($product_id, '_price', true));
    woocommerce_update_order_item_meta($item_id, '_line_total', get_post_meta($product_id, '_subscription_price', true));
    // WC_Subscriptions_Product::get_price() would return a price without filters applied
    woocommerce_update_order_item_meta($item_id, '_line_tax', 0);
    woocommerce_update_order_item_meta($item_id, '_line_subtotal_tax', 0);
    // WC_Subscriptions_Product::get_price() would return a price without filters applied
    woocommerce_update_order_item_meta($item_id, 'Servings:', $variation_name);
    woocommerce_update_order_item_meta($item_id, '_subscription_period', $period);
    woocommerce_update_order_item_meta($item_id, '_subscription_interval', $interval);
    woocommerce_update_order_item_meta($item_id, '_subscription_length', $length);
    woocommerce_update_order_item_meta($item_id, '_subscription_trial_length', $trial_length);
    woocommerce_update_order_item_meta($item_id, '_subscription_trial_period', $trial_period);
    woocommerce_update_order_item_meta($item_id, '_subscription_recurring_amount', get_post_meta($product_id, '_subscription_price', true));
    // WC_Subscriptions_Product::get_price() would return a price without filters applied
    woocommerce_update_order_item_meta($item_id, '_subscription_sign_up_fee', $sign_up_fee);
    // Calculated recurring amounts for the item
    woocommerce_update_order_item_meta($item_id, '_recurring_line_total', number_format((double) get_post_meta($product_id, '_price', true), 2, '.', ''));
    woocommerce_update_order_item_meta($item_id, '_recurring_line_tax', '');
    woocommerce_update_order_item_meta($item_id, '_recurring_line_subtotal', number_format((double) get_post_meta($product_id, '_regular_price', true), 2, '.', ''));
    woocommerce_update_order_item_meta($item_id, '_recurring_line_subtotal_tax', '');
    $odr = $orderID;
    /* Fetch Oder Details */
    //if(($_vv['variation_id'] ==  $vari_ID))
    $mealOrder = woocommerce_get_order_item_meta($srOW->order_item_id, 'Servings:', true);
    $mealType = woocommerce_get_order_item_meta($srOW->order_item_id, '_subscription_period', true);
    $mealTotal = woocommerce_get_order_item_meta($srOW->order_item_id, '_recurring_line_subtotal', true);
    $vari_ID = woocommerce_get_order_item_meta($srOW->order_item_id, '_variation_id', true);
    $_product = get_product($product_id);
    $srOW = $wpdb->get_row("UPDATE wp_woocommerce_order_items SET order_item_name = '" . $_product->post->post_title . "' WHERE order_item_id=" . $item_id);
    $html = '<div class="info_heading">Meal Plan</div>
                <div class="info_values">
                    <p>' . $_product->post->post_title . '</p>
                </div>
                <div class="info_heading">Subscriptions Type</div>
                <div class="info_values">
                    <p>' . $variation_name . '</p>
                </div>
                <div class="info_heading">Price</div>
                <div class="info_values">
                    <p>' . $p . '</p>
                </div>';
    $mess['message'] = $html;
    echo json_encode($mess);
    exit;
}
Exemplo n.º 13
0
 /**
  * Add subscription related order item meta via Ajax when a subscription product is added as an item to an order.
  *
  * This function is hooked to the 'wp_ajax_woocommerce_subscriptions_prefill_order_item_meta' hook which should only fire
  * on WC 1.x (because the admin.js uses a selector which was changed in WC 2.0). For WC 2.0, order item meta is pre-filled
  * via the 'woocommerce_new_order_item' hook in the new @see self::prefill_order_item().
  *
  * @since 1.2.4
  * @return void
  */
 public static function prefill_order_item_meta_old()
 {
     if (function_exists('woocommerce_add_order_item_meta')) {
         // Meta added on the 'woocommerce_new_order_item' hook
         return;
     }
     check_ajax_referer(WC_Subscriptions::$text_domain, 'security');
     $product_id = trim(stripslashes($_POST['item_to_add']));
     $index = trim(stripslashes($_POST['index']));
     $response = array('item_index' => $index, 'html' => '', 'line_totals' => array());
     if (WC_Subscriptions_Product::is_subscription($product_id)) {
         $recurring_amount = WC_Subscriptions_Product::get_price($product_id);
         $item_meta = new WC_Order_Item_Meta();
         // Subscription details so order state persists even when a product is changed
         $item_meta->add('_subscription_period', WC_Subscriptions_Product::get_period($product_id));
         $item_meta->add('_subscription_interval', WC_Subscriptions_Product::get_interval($product_id));
         $item_meta->add('_subscription_length', WC_Subscriptions_Product::get_length($product_id));
         $item_meta->add('_subscription_trial_length', WC_Subscriptions_Product::get_trial_length($product_id));
         $item_meta->add('_subscription_trial_period', WC_Subscriptions_Product::get_trial_period($product_id));
         $item_meta->add('_subscription_recurring_amount', $recurring_amount);
         $item_meta->add('_subscription_sign_up_fee', WC_Subscriptions_Product::get_sign_up_fee($product_id));
         // Recurring totals need to be calcualted
         $item_meta->add('_recurring_line_total', $recurring_amount);
         $item_meta->add('_recurring_line_tax', 0);
         $item_meta->add('_recurring_line_subtotal', $recurring_amount);
         $item_meta->add('_recurring_line_subtotal_tax', 0);
         $item_meta = $item_meta->meta;
         if (isset($item_meta) && is_array($item_meta) && sizeof($item_meta) > 0) {
             foreach ($item_meta as $key => $meta) {
                 // Backwards compatibility
                 if (is_array($meta) && isset($meta['meta_name'])) {
                     $meta_name = $meta['meta_name'];
                     $meta_value = $meta['meta_value'];
                 } else {
                     $meta_name = $key;
                     $meta_value = $meta;
                 }
                 $response['html'] .= '<tr><td><input type="text" name="meta_name[' . $index . '][]" value="' . esc_attr($meta_name) . '" /></td><td><input type="text" name="meta_value[' . $index . '][]" value="' . esc_attr($meta_value) . '" /></td><td width="1%"></td></tr>';
             }
         }
         // Calculate line totals for this item
         if ($sign_up_fee > 0) {
             $line_subtotal = $sign_up_fee;
             $line_total = $sign_up_fee;
             // If there is no free trial, add the recuring amounts
             if ($trial_length == 0) {
                 $line_subtotal += $recurring_amount;
                 $line_total += $recurring_amount;
             }
             $response['line_totals']['line_subtotal'] = esc_attr(number_format((double) $line_subtotal, 2, '.', ''));
             $response['line_totals']['line_total'] = esc_attr(number_format((double) $line_total, 2, '.', ''));
         }
     }
     echo json_encode($response);
     die;
 }