/**
  * 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.1
  */
 public static function get_cart_subscription_trial_length()
 {
     global $woocommerce;
     $trial_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_trial_length)) {
             $trial_length = $cart_item['data']->subscription_trial_length;
             break;
         } elseif (WC_Subscriptions_Product::is_subscription($item_id)) {
             $trial_length = WC_Subscriptions_Product::get_trial_length($item_id);
             break;
         }
     }
     return apply_filters('woocommerce_subscriptions_cart_trial_length', $trial_length);
 }
 /**
  * Removes the "set_subscription_prices_for_calculation" filter from the WC Product's woocommerce_get_price hook once
  *
  * @since 1.5.10
  */
 public static function set_prorated_price_for_calculation($price, $product)
 {
     if (WC_Subscriptions_Product::is_subscription($product) && self::is_product_prorated($product) && in_array(WC_Subscriptions_Cart::get_calculation_type(), array('combined_total', 'none'))) {
         $next_payment_date = self::calculate_first_payment_date($product, 'timestamp');
         if (self::is_today($next_payment_date)) {
             return $price;
         }
         switch ($product->subscription_period) {
             case 'week':
                 $days_in_cycle = 7 * $product->subscription_period_interval;
                 break;
             case 'month':
                 $days_in_cycle = date('t') * $product->subscription_period_interval;
                 break;
             case 'year':
                 $days_in_cycle = (365 + date('L')) * $product->subscription_period_interval;
                 break;
         }
         $days_until_next_payment = ceil(($next_payment_date - gmdate('U')) / (60 * 60 * 24));
         if ('combined_total' == WC_Subscriptions_Cart::get_calculation_type()) {
             $sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee($product);
             if ($sign_up_fee > 0 && 0 == WC_Subscriptions_Product::get_trial_length($product)) {
                 $price = $sign_up_fee + $days_until_next_payment * (($price - $sign_up_fee) / $days_in_cycle);
             }
         } elseif ('none' == WC_Subscriptions_Cart::get_calculation_type()) {
             $price = $days_until_next_payment * ($price / $days_in_cycle);
         }
     }
     return $price;
 }
 /**
  * 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;
 }
 /**
  * Removes the "set_subscription_prices_for_calculation" filter from the WC Product's woocommerce_get_price hook once
  *
  * @since 1.5.10
  */
 public static function set_prorated_price_for_calculation($price, $product)
 {
     if (WC_Subscriptions_Product::is_subscription($product) && self::is_product_prorated($product) && 'none' == WC_Subscriptions_Cart::get_calculation_type()) {
         $next_payment_date = self::calculate_first_payment_date($product, 'timestamp');
         if (self::is_today($next_payment_date)) {
             return $price;
         }
         switch ($product->subscription_period) {
             case 'week':
                 $days_in_cycle = 7 * $product->subscription_period_interval;
                 break;
             case 'month':
                 $days_in_cycle = date('t') * $product->subscription_period_interval;
                 break;
             case 'year':
                 $days_in_cycle = (365 + date('L')) * $product->subscription_period_interval;
                 break;
         }
         $days_until_next_payment = ceil(($next_payment_date - gmdate('U')) / (60 * 60 * 24));
         $sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee($product);
         if ($sign_up_fee > 0 && 0 == WC_Subscriptions_Product::get_trial_length($product)) {
             $price = $sign_up_fee + $days_until_next_payment * (($price - $sign_up_fee) / $days_in_cycle);
         } else {
             $price = $days_until_next_payment * ($price / $days_in_cycle);
         }
         // Now round the amount to the number of decimals displayed for prices to avoid rounding errors in the total calculations (we don't want to use WC_DISCOUNT_ROUNDING_PRECISION here because it can still lead to rounding errors). For full details, see: https://github.com/Prospress/woocommerce-subscriptions/pull/1134#issuecomment-178395062
         $price = round($price, wc_get_price_decimals());
     }
     return $price;
 }
 /**
  * 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.º 7
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 trial length for the entire cart.
  *
  * @since 1.1
  * @deprecated 2.0
  */
 public static function get_cart_subscription_trial_length()
 {
     _deprecated_function(__METHOD__, '2.0', 'values from WC()->cart->recurring_carts');
     $trial_length = 0;
     if (self::cart_contains_subscription()) {
         foreach (WC()->cart->cart_contents as $cart_item) {
             if (isset($cart_item['data']->subscription_trial_length)) {
                 $trial_length = $cart_item['data']->subscription_trial_length;
                 break;
             } elseif (WC_Subscriptions_Product::is_subscription($cart_item['data'])) {
                 $trial_length = WC_Subscriptions_Product::get_trial_length($cart_item['data']);
                 break;
             }
         }
     }
     return apply_filters('woocommerce_subscriptions_cart_trial_length', $trial_length);
 }
 /**
  * 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;
 }
 /**
  * Add a cart item to a subscription.
  *
  * @since 2.0
  */
 public static function add_cart_item($subscription, $cart_item, $cart_item_key)
 {
     $item_id = $subscription->add_product($cart_item['data'], $cart_item['quantity'], array('variation' => $cart_item['variation'], 'totals' => array('subtotal' => $cart_item['line_subtotal'], 'subtotal_tax' => $cart_item['line_subtotal_tax'], 'total' => $cart_item['line_total'], 'tax' => $cart_item['line_tax'], 'tax_data' => $cart_item['line_tax_data'])));
     if (!$item_id) {
         // translators: placeholder is an internal error number
         throw new Exception(sprintf(__('Error %d: Unable to create subscription. Please try again.', 'woocommerce-subscriptions'), 402));
     }
     $cart_item_product_id = 0 != $cart_item['variation_id'] ? $cart_item['variation_id'] : $cart_item['product_id'];
     if (WC_Subscriptions_Product::get_trial_length(wcs_get_canonical_product_id($cart_item)) > 0) {
         wc_add_order_item_meta($item_id, '_has_trial', 'true');
     }
     // Allow plugins to add order item meta
     do_action('woocommerce_add_order_item_meta', $item_id, $cart_item, $cart_item_key);
     do_action('woocommerce_add_subscription_item_meta', $item_id, $cart_item, $cart_item_key);
     return $item_id;
 }
 /**
  * Gets the subscription length from the cart and returns it as an array (eg. array( 'month', 'day' ) )
  * 
  * @since 1.1
  */
 public static function get_cart_subscription_trial_length()
 {
     global $woocommerce;
     $trial_length = 0;
     foreach ($woocommerce->cart->cart_contents as $cart_item) {
         if (WC_Subscriptions_Product::is_subscription($cart_item['product_id'])) {
             $trial_length = WC_Subscriptions_Product::get_trial_length($cart_item['product_id']);
             break;
         }
     }
     return $trial_length;
 }
Exemplo n.º 11
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.º 12
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;
 }
 /**
  * Make sure a synchronised subscription's price includes a free trial, unless it's first payment is today.
  *
  * @since 1.5
  */
 public static function maybe_unset_free_trial($total)
 {
     global $woocommerce;
     foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
         if (self::is_product_synced($cart_item['data'])) {
             $woocommerce->cart->cart_contents[$cart_item_key]['data']->subscription_trial_length = WC_Subscriptions_Product::get_trial_length(WC_Subscriptions_Cart::get_items_product_id($cart_item));
         }
     }
     return $total;
 }
 /**
  * Get prorated sub price.
  *
  * @param  double     $recurring_price
  * @param  WC_Product $product
  * @return double
  */
 public function get_prorated_price_for_subscription($recurring_price, $product = false)
 {
     if (!$product) {
         $product = $this->product;
     }
     $price = 0;
     if (WC_Subscriptions_Product::is_subscription($product)) {
         if (0 == WC_Subscriptions_Product::get_trial_length($product)) {
             if (WC_Subscriptions_Synchroniser::is_product_prorated($product)) {
                 $next_payment_date = WC_Subscriptions_Synchroniser::calculate_first_payment_date($product, 'timestamp');
                 if (WC_Subscriptions_Synchroniser::is_today($next_payment_date)) {
                     return $recurring_price;
                 }
                 switch ($product->subscription_period) {
                     case 'week':
                         $days_in_cycle = 7 * $product->subscription_period_interval;
                         break;
                     case 'month':
                         $days_in_cycle = date('t') * $product->subscription_period_interval;
                         break;
                     case 'year':
                         $days_in_cycle = (365 + date('L')) * $product->subscription_period_interval;
                         break;
                 }
                 $days_until_next_payment = ceil(($next_payment_date - gmdate('U')) / (60 * 60 * 24));
                 $price = $days_until_next_payment * ($recurring_price / $days_in_cycle);
             } else {
                 $price = $recurring_price;
             }
         }
     }
     return $price;
 }