/**
  * Return the sign-up fee for this product
  *
  * @return string
  */
 public function get_sign_up_fee()
 {
     return WC_Subscriptions_Product::get_sign_up_fee($this);
 }
 /**
  * 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;
 }
 /**
  * Gets the subscription sign up fee for the cart and returns it
  *
  * Currently short-circuits to return just the sign-up fee of the first subscription, because only
  * one subscription can be purchased at a time. 
  *
  * @since 1.0
  */
 public static function get_cart_subscription_sign_up_fee()
 {
     global $woocommerce;
     $sign_up_fee = 0;
     if (!self::cart_contains_subscription_renewal()) {
         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_sign_up_fee)) {
                 $sign_up_fee = $cart_item['data']->subscription_sign_up_fee;
                 break;
             } elseif (WC_Subscriptions_Product::is_subscription($item_id)) {
                 $sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee($item_id);
                 break;
             }
         }
     }
     return apply_filters('woocommerce_subscriptions_cart_sign_up_fee', $sign_up_fee);
 }
 /**
  * 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);
     }
 }
 /**
  * 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
  */
 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;
 }
Exemplo n.º 7
0
 /**
  * Gets the subscription sign up fee for the cart and returns it
  *
  * Currently short-circuits to return just the sign-up fee of the first subscription, because only
  * one subscription can be purchased at a time.
  *
  * @since 1.0
  */
 public static function get_cart_subscription_sign_up_fee()
 {
     $sign_up_fee = 0;
     if (self::cart_contains_subscription() || wcs_cart_contains_renewal()) {
         $renewal_item = wcs_cart_contains_renewal();
         foreach (WC()->cart->cart_contents as $cart_item) {
             // Renewal items do not have sign-up fees
             if ($renewal_item == $cart_item) {
                 continue;
             }
             if (isset($cart_item['data']->subscription_sign_up_fee)) {
                 $sign_up_fee += $cart_item['data']->subscription_sign_up_fee;
             } elseif (WC_Subscriptions_Product::is_subscription($cart_item['data'])) {
                 $sign_up_fee += WC_Subscriptions_Product::get_sign_up_fee($cart_item['data']);
             }
         }
     }
     return apply_filters('woocommerce_subscriptions_cart_sign_up_fee', $sign_up_fee);
 }
 /**
  * Function to apply discounts to a product and get the discounted price (before tax is applied)
  * 
  * @param mixed $values
  * @param mixed $price
  * @param bool $add_totals (default: false)
  * @return float price
  * @since 1.0
  */
 public static function get_discounted_price($values, $price, $add_totals = false)
 {
     global $woocommerce;
     if (!$price) {
         return $price;
     }
     if (!empty($woocommerce->cart->applied_coupons)) {
         foreach ($woocommerce->cart->applied_coupons as $code) {
             $coupon = new WC_Coupon($code);
             if ($coupon->apply_before_tax() && $coupon->is_valid()) {
                 switch ($coupon->type) {
                     case "fixed_product":
                     case "percent_product":
                         $this_item_is_discounted = false;
                         $product_cats = wp_get_post_terms($values['product_id'], 'product_cat', array("fields" => "ids"));
                         // Specific products get the discount
                         if (sizeof($coupon->product_ids) > 0) {
                             if (in_array($values['product_id'], $coupon->product_ids) || in_array($values['variation_id'], $coupon->product_ids) || in_array($values['data']->get_parent(), $coupon->product_ids)) {
                                 $this_item_is_discounted = true;
                             }
                             // Category discounts
                         } elseif (sizeof($coupon->product_categories) > 0) {
                             if (sizeof(array_intersect($product_cats, $coupon->product_categories)) > 0) {
                                 $this_item_is_discounted = true;
                             }
                         } else {
                             // No product ids - all items discounted
                             $this_item_is_discounted = true;
                         }
                         // Specific product ID's excluded from the discount
                         if (sizeof($coupon->exclude_product_ids) > 0) {
                             if (in_array($values['product_id'], $coupon->exclude_product_ids) || in_array($values['variation_id'], $coupon->exclude_product_ids) || in_array($values['data']->get_parent(), $coupon->exclude_product_ids)) {
                                 $this_item_is_discounted = false;
                             }
                         }
                         // Specific categories excluded from the discount
                         if (sizeof($coupon->exclude_product_categories) > 0) {
                             if (sizeof(array_intersect($product_cats, $coupon->exclude_product_categories)) > 0) {
                                 $this_item_is_discounted = false;
                             }
                         }
                         // Apply filter
                         $this_item_is_discounted = apply_filters('woocommerce_item_is_discounted', $this_item_is_discounted, $values, $before_tax = true);
                         // Apply the discount
                         if ($this_item_is_discounted) {
                             if ($coupon->type == 'fixed_product') {
                                 if ($price < $coupon->amount) {
                                     $discount_amount = $price;
                                 } else {
                                     $discount_amount = $coupon->amount;
                                 }
                                 $price = $price - $coupon->amount;
                                 if ($price < 0) {
                                     $price = 0;
                                 }
                                 if ($add_totals) {
                                     $woocommerce->cart->sign_up_fee_discount_cart = $woocommerce->cart->sign_up_fee_discount_cart + $discount_amount * $values['quantity'];
                                 }
                             } elseif ($coupon->type == 'percent_product') {
                                 $percent_discount = WC_Subscriptions_Product::get_sign_up_fee_excluding_tax($values['data']) / 100 * $coupon->amount;
                                 if ($add_totals) {
                                     $woocommerce->cart->sign_up_fee_discount_cart = $woocommerce->cart->sign_up_fee_discount_cart + $percent_discount * $values['quantity'];
                                 }
                                 $price = $price - $percent_discount;
                             }
                         }
                         break;
                     case "fixed_cart":
                         /** 
                          * This is the most complex discount - we need to divide the discount between rows based on their price in
                          * proportion to the subtotal. This is so rows with different tax rates get a fair discount, and so rows
                          * with no price (free) don't get discount too.
                          */
                         // Get item discount by dividing item cost by subtotal to get a %
                         if ($woocommerce->cart->sign_up_fee_subtotal_ex_tax) {
                             $discount_percent = WC_Subscriptions_Product::get_sign_up_fee_excluding_tax($values['data']) * $values['quantity'] / $woocommerce->cart->sign_up_fee_subtotal_ex_tax;
                         } else {
                             $discount_percent = 0;
                         }
                         // Use pence to help prevent rounding errors
                         $coupon_amount_pence = $coupon->amount * 100;
                         // Work out the discount for the row
                         $item_discount = $coupon_amount_pence * $discount_percent;
                         // Work out discount per item
                         $item_discount = $item_discount / $values['quantity'];
                         // Pence
                         $price = $price * 100;
                         // Check if discount is more than price
                         if ($price < $item_discount) {
                             $discount_amount = $price;
                         } else {
                             $discount_amount = $item_discount;
                         }
                         // Take discount off of price (in pence)
                         $price = $price - $discount_amount;
                         // Back to pounds
                         $price = $price / 100;
                         // Cannot be below 0
                         if ($price < 0) {
                             $price = 0;
                         }
                         // Add coupon to discount total (once, since this is a fixed cart discount and we don't want rounding issues)
                         if ($add_totals) {
                             $woocommerce->cart->sign_up_fee_discount_cart = $woocommerce->cart->sign_up_fee_discount_cart + $discount_amount * $values['quantity'] / 100;
                         }
                         break;
                     case "percent":
                         $percent_discount = WC_Subscriptions_Product::get_sign_up_fee($values['data']->id) / 100 * $coupon->amount;
                         if ($add_totals) {
                             $woocommerce->cart->sign_up_fee_discount_cart = $woocommerce->cart->sign_up_fee_discount_cart + $percent_discount * $values['quantity'];
                         }
                         $price = $price - $percent_discount;
                         break;
                 }
             }
         }
     }
     return $price;
 }
 /**
  * Gets the subscription sign up fee for the cart and returns it
  * 
  * Currently short-circuits to return just the sign-up fee of the first subscription, because only
  * one subscription can be purchased at a time. 
  * 
  * @since 1.0
  */
 public static function get_cart_subscription_sign_up_fee()
 {
     global $woocommerce;
     $sign_up_fee = 0;
     foreach ($woocommerce->cart->cart_contents as $cart_item) {
         if (WC_Subscriptions_Product::is_subscription($cart_item['product_id'])) {
             $sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee($cart_item['product_id']);
             break;
         }
     }
     return $sign_up_fee;
 }
Exemplo n.º 10
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;
}
 /**
  * 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);
         $recurring_amount = WC_Subscriptions_Product::get_price($product_id);
         $sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee($product_id);
         $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':
             case 'refunded':
                 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] = number_format($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;
 }
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;
 }
 /**
  * If the subscription being generated is synced, set the syncing related meta data correctly.
  *
  * @since 1.5
  */
 public static function prefill_order_item_meta($item, $item_id)
 {
     $product_id = $item['variation_id'] ? $item['variation_id'] : $item['product_id'];
     if (self::is_product_synced($product_id)) {
         $order_id = $_POST['order_id'];
         // If the date the subscription is being added is not the sync date, make sure the line total is only equal to the sign up fee (or $0 if no sign up fee)
         if (!self::is_today(self::calculate_first_payment_date($product_id, 'timestamp'))) {
             $line_total_keys = array('line_subtotal', 'line_total');
             foreach ($line_total_keys as $line_total_key) {
                 $item[$line_total_key] = WC_Subscriptions_Product::get_sign_up_fee($product_id);
                 woocommerce_update_order_item_meta($item_id, '_' . $line_total_key, $item[$line_total_key]);
             }
         }
         update_post_meta($order_id, '_order_contains_synced_subscription', 'true');
     }
     return $item;
 }
 private function print_price_fields($product_id = 0, $form_prefix = "")
 {
     if (!$product_id) {
         global $product;
         if ($product) {
             $product_id = $product->id;
         }
     } else {
         $product = wc_get_product($product_id);
     }
     if (!$product_id || empty($product)) {
         if (!empty($this->current_product_id_to_be_displayed)) {
             $product_id = $this->current_product_id_to_be_displayed;
             $product = wc_get_product($product_id);
         } else {
             $this->tm_epo_totals_batch($form_prefix);
             return;
         }
     }
     if (!$product_id || empty($product)) {
         return;
     }
     $cpf_price_array = $this->get_product_tm_epos($product_id);
     if (!$cpf_price_array) {
         return;
     }
     if ($cpf_price_array && $this->tm_epo_enable_final_total_box_all == "no") {
         $global_price_array = $cpf_price_array['global'];
         $local_price_array = $cpf_price_array['local'];
         if (empty($global_price_array) && empty($local_price_array)) {
             return;
         }
     }
     if (!$cpf_price_array && $this->tm_epo_enable_final_total_box_all == "no") {
         return;
     }
     if ($form_prefix) {
         $form_prefix = "_" . $form_prefix;
     }
     // WooCommerce Dynamic Pricing & Discounts
     if (class_exists('RP_WCDPD') && !empty($GLOBALS['RP_WCDPD'])) {
         $price = $this->get_RP_WCDPD($product);
         if ($price) {
             $price['product'] = array();
             if ($price['is_multiprice']) {
                 foreach ($price['rules'] as $variation_id => $variation_rule) {
                     foreach ($variation_rule as $rulekey => $pricerule) {
                         $price['product'][$variation_id][] = array("min" => $pricerule["min"], "max" => $pricerule["max"], "value" => $pricerule["type"] != "percentage" ? apply_filters('woocommerce_tm_epo_price', $pricerule["value"], "", false) : $pricerule["value"], "type" => $pricerule["type"]);
                     }
                 }
             } else {
                 foreach ($price['rules'] as $rulekey => $pricerule) {
                     $price['product'][0][] = array("min" => $pricerule["min"], "max" => $pricerule["max"], "value" => $pricerule["type"] != "percentage" ? apply_filters('woocommerce_tm_epo_price', $pricerule["value"], "", false) : $pricerule["value"], "type" => $pricerule["type"]);
                 }
             }
         } else {
             $price = array();
             $price['product'] = array();
         }
         $price['price'] = apply_filters('woocommerce_tm_epo_price', $product->get_price(), "", false);
     } else {
         if (class_exists('WC_Dynamic_Pricing')) {
             $id = isset($product->variation_id) ? $product->variation_id : $product->id;
             $dp = WC_Dynamic_Pricing::instance();
             if ($dp && is_object($dp) && property_exists($dp, "discounted_products") && isset($dp->discounted_products[$id])) {
                 $_price = $dp->discounted_products[$id];
             } else {
                 $_price = $product->get_price();
             }
         } else {
             $_price = apply_filters('woocommerce_tm_epo_price_compatibility', $product->get_price(), $product);
         }
         $price = array();
         $price['product'] = array();
         $price['price'] = apply_filters('woocommerce_tm_epo_price', $_price, "", false);
     }
     $variations = array();
     $variations_subscription_period = array();
     $variations_subscription_sign_up_fee = array();
     foreach ($product->get_children() as $child_id) {
         $variation = $product->get_child($child_id);
         if (!$variation->exists()) {
             continue;
         }
         if (class_exists('WC_Subscriptions_Product')) {
             $variations_subscription_period[$child_id] = WC_Subscriptions_Product::get_price_string($variation, array('subscription_price' => false, 'sign_up_fee' => false, 'trial_length' => false));
             $variations_subscription_sign_up_fee[$child_id] = $variation->subscription_sign_up_fee;
         } else {
             $variations_subscription_period[$child_id] = '';
             $variations_subscription_sign_up_fee[$child_id] = '';
         }
         $variations[$child_id] = apply_filters('woocommerce_tm_epo_price_compatibility', apply_filters('woocommerce_tm_epo_price', $variation->get_price(), "", false), $variation, $child_id);
     }
     $is_subscription = false;
     $subscription_period = '';
     $subscription_sign_up_fee = 0;
     if (class_exists('WC_Subscriptions_Product')) {
         if (WC_Subscriptions_Product::is_subscription($product)) {
             $is_subscription = true;
             $subscription_period = WC_Subscriptions_Product::get_price_string($product, array('subscription_price' => false, 'sign_up_fee' => false, 'trial_length' => false));
             $subscription_sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee($product);
         }
     }
     global $woocommerce;
     $cart = $woocommerce->cart;
     $_tax = new WC_Tax();
     $taxrates = $_tax->get_rates($product->get_tax_class());
     unset($_tax);
     $tax_rate = 0;
     foreach ($taxrates as $key => $value) {
         $tax_rate = $tax_rate + floatval($value['rate']);
     }
     $taxable = $product->is_taxable();
     $tax_display_mode = get_option('woocommerce_tax_display_shop');
     $tax_string = "";
     if ($taxable && $this->tm_epo_global_tax_string_suffix == "yes") {
         if ($tax_display_mode == 'excl') {
             //if ( $cart->tax_total > 0 && $cart->prices_include_tax ) {
             $tax_string = ' <small>' . WC()->countries->ex_tax_or_vat() . '</small>';
             //}
         } else {
             //if ( $cart->tax_total > 0 && !$cart->prices_include_tax ) {
             $tax_string = ' <small>' . WC()->countries->inc_tax_or_vat() . '</small>';
             //}
         }
     }
     $force_quantity = 0;
     if ($this->cart_edit_key) {
         $cart_item_key = $this->cart_edit_key;
         $cart_item = WC()->cart->get_cart_item($cart_item_key);
         if (isset($cart_item["quantity"])) {
             $force_quantity = $cart_item["quantity"];
         }
     }
     if (!$this->is_bto) {
         if (empty($this->epo_internal_counter) || !isset($this->epo_internal_counter_check["tc" . $this->epo_internal_counter])) {
             // First time displaying totals and fields haven't been displayed
             $this->epo_internal_counter++;
             $this->epo_internal_counter_check["tc" . $this->epo_internal_counter] = $this->epo_internal_counter;
         } else {
             // Fields have already been displayed
             unset($this->epo_internal_counter_check["tc" . $this->epo_internal_counter]);
             $this->current_product_id_to_be_displayed = 0;
         }
         $_epo_internal_counter = $this->epo_internal_counter;
     } else {
         $_epo_internal_counter = 0;
     }
     wc_get_template('tm-totals.php', array('theme_name' => $this->get_theme('Name'), 'variations' => esc_html(json_encode((array) $variations)), 'variations_subscription_period' => esc_html(json_encode((array) $variations_subscription_period)), 'variations_subscription_sign_up_fee' => esc_html(json_encode((array) $variations_subscription_sign_up_fee)), 'subscription_period' => $subscription_period, 'subscription_sign_up_fee' => $subscription_sign_up_fee, 'is_subscription' => $is_subscription, 'is_sold_individually' => $product->is_sold_individually(), 'hidden' => $this->tm_meta_cpf['override_final_total_box'] ? $this->tm_epo_final_total_box == 'hide' ? ' hidden' : '' : ($this->tm_meta_cpf['override_final_total_box'] == 'hide' ? ' hidden' : ''), 'form_prefix' => $form_prefix, 'type' => esc_html($product->product_type), 'price' => esc_html(is_object($product) ? apply_filters('woocommerce_tm_final_price', $price['price'], $product) : ''), 'taxable' => $taxable, 'tax_display_mode' => $tax_display_mode, 'prices_include_tax' => $cart->prices_include_tax, 'tax_rate' => $tax_rate, 'tax_string' => $tax_string, 'product_price_rules' => esc_html(json_encode((array) $price['product'])), 'fields_price_rules' => $this->tm_epo_dpd_enable == "no" ? 0 : 1, 'tm_epo_dpd_suffix' => $this->tm_epo_dpd_suffix, 'tm_epo_dpd_prefix' => $this->tm_epo_dpd_prefix, 'force_quantity' => $force_quantity, 'product_id' => $product_id, 'epo_internal_counter' => $_epo_internal_counter), $this->_namespace, $this->template_path);
 }