/** * Get normalized price before plan. * * The price_excluding_tax in order item is calculated with total percents * from payment plan. This method normalize the price again. * * @param WC_Deposits_Plan $plan Plan * @param array $item Order item * * @return float Line price */ private static function _get_normalized_price_before_plan($plan, $item) { $price_after_plan = !empty($item['price_excluding_tax']) ? $item['price_excluding_tax'] : $item['product']->get_price_excluding_tax($item['qty']); if ('percentage' === $plan->get_type()) { $total_percent = $plan->get_total_percent(); $line_price = $price_after_plan * 100 / $total_percent; } else { $line_price = $price_after_plan; } return $line_price; }
/** * 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); }