/** * Schedule all orders for a payment plan. * This is important because the tax point is when the order is placed. * @param WC_Deposits_Plan $payment_plan * @param int $original_order_id */ public static function schedule_orders_for_plan($payment_plan, $original_order_id, $item) { $schedule = $payment_plan->get_schedule(); $current_timestamp = current_time('timestamp'); $payment_number = 2; $line_price = self::_get_normalized_price_before_plan($payment_plan, $item); $plan_type = $payment_plan->get_type(); // Track the amount of each payment (start with deposit) $previous_payments = $schedule[0]->amount; // Skip first payment - that was taken already array_shift($schedule); foreach ($schedule as $schedule_row) { // Work out relative timestamp $current_timestamp = strtotime("+{$schedule_row->interval_amount} {$schedule_row->interval_unit}", $current_timestamp); // Work out how much the payment will be for if ('fixed' === $plan_type) { if ('remainder' === $schedule_row->amount) { $item['amount'] = $line_price - $previous_payments; } else { $item['amount'] = $schedule_row->amount; } } else { $item['amount'] = $line_price / 100 * $schedule_row->amount; } $item = apply_filters('woocommerce_deposits_before_schedule_plan_order', $item, $schedule_row, $payment_plan, $original_order_id); $previous_payments += $item['amount']; // Create order WC_Deposits_Order_Manager::create_order($current_timestamp, $original_order_id, $payment_number, $item, 'scheduled-payment'); $payment_number++; } }
/** * Deposit amount for a product based on fixed or % using actual prices * @param WC_Product|int $product * @param int $plan_id * @param string $context of display Valid values display or order * @param float $product_price If the price differs from that set in the product * @return float|bool */ public static function get_deposit_amount($product, $plan_id = 0, $context = 'display', $product_price = null) { if (is_numeric($product)) { $product = wc_get_product($product); } $item_id = $product->variation_id ? $product->variation_id : $product->id; $type = self::get_deposit_type($item_id); $percentage = false; if (in_array($type, array('fixed', 'percent'))) { $amount = get_post_meta($item_id, '_wc_deposit_amount', true); if (!$amount) { $amount = get_option('wc_deposits_default_amount'); } if (!$amount) { return false; } if ('percent' === $type) { $percentage = true; } } else { if (!$plan_id) { return false; } $plan = new WC_Deposits_Plan($plan_id); $schedule = $plan->get_schedule(); $first_payment = current($schedule); $amount = $first_payment->amount; $percentage = 'percentage' === $plan->get_type(); } if ($percentage) { $product_price = is_null($product_price) ? $product->get_price() : $product_price; $amount = $product_price / 100 * $amount; } if ('display' === $context) { $tax_display_mode = get_option('woocommerce_tax_display_shop'); $price = $tax_display_mode == 'incl' ? $product->get_price_including_tax(1, $amount) : $product->get_price_excluding_tax(1, $amount); } else { $price = $amount; } return wc_format_decimal($price); }