/** * 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); }
/** * Get payment plans for a product * @param int $product_id * @param bool $inc_variations Merge variation plans with parent product? * @return array of WC_Deposits_Plan */ public static function get_plans_for_product($product_id, $inc_variations = false) { global $wpdb; $plans = array(); $product = get_post($product_id); $plan_ids = array_merge(array(0), self::get_plan_ids_for_product($product_id)); $db_plans = $wpdb->get_results("SELECT * FROM {$wpdb->wc_deposits_payment_plans} WHERE ID IN (" . implode(',', $plan_ids) . ")"); foreach ($db_plans as $result) { $plan = new WC_Deposits_Plan($result); // additional details to distinguish variations $plan->item_ids = array($product_id); $plan->item_type = $product->post_type; $plans[$plan->get_id()] = $plan; } // If using variations, merge child variations into array so there's no duplicate plans if ($inc_variations) { $children = get_children(array('post_parent' => $product_id, 'post_type' => 'product_variation')); foreach ($children as $child) { $child_plans = self::get_plans_for_product($child->ID); foreach ($child_plans as $child_plan) { if (array_key_exists($child_plan->get_id(), $plans)) { $plans[$child_plan->get_id()]->item_ids[] = $child_plan->item_ids[0]; } else { $plans[$child_plan->get_id()] = $child_plan; } } } } return $plans; }