product details

This commit is contained in:
Almira Krdzic
2018-09-12 16:42:21 +02:00
parent 35484c6d4f
commit e53b243d96
65 changed files with 3327 additions and 1520 deletions

View File

@@ -0,0 +1,159 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Wiaas_Package_Pricing
*/
class Wiaas_Package_Pricing {
/**
* Available pay types for wiaas packages
* @var array
*/
private static $pay_types = array(
'purchase' => array(
'title' => 'Purchase',
'package_pay_period' => 0,
'services_contract_period' => 0,
'max_contract_period' => 36,
'period_unit' => 'month',
'labe'
),
'purchase_24' => array(
'title' => 'Purchase with 24M commitment',
'package_pay_period' => 0,
'services_contract_period' => 24,
'max_contract_period' => 36,
'period_unit' => 'month'
),
'managed_36' => array(
'title' => 'Managed service 36M rent',
'package_pay_period' => 36,
'services_contract_period'=> 36,
'max_contract_period' => 36,
'period_unit' => 'month'
)
);
/**
* Payment fields for pricing rules
* @var array
*/
private static $payment_fields = array(
'minimal_fixed_price' => 0,
'principal_amount' => 0,
'minimal_services_price' => 0
);
public static function init() {
add_filter('woocommerce_bundle_price_html', array( __CLASS__, 'get_package_price_html' ), 10, 2);
}
public static function get_package_price_html($price_html, $package) {
$bundled_items = $package->get_bundled_items();
$recurring_price = 0;
foreach ($bundled_items as $bundled_item) {
$product = $bundled_item->product;
$product_price = Wiaas_Product_Pricing::get_product_price($product);
if ($product_price['is_recurring']) {
$recurring_price += $product_price['price'] * $bundled_item->get_quantity();
}
}
$price_html = 'Fixed: ' . $price_html . ' and ' . $recurring_price . ' / month';
return $price_html;
}
/**
* Get configuration for available payment types
* @return array
*/
public static function get_available_pay_types() {
return self::$pay_types;
}
/**
* Get empty payment rule with initialized fields to default values
* @return array
*/
public static function get_empty_pricing_rule() {
return self::$payment_fields;
}
/**
* Retrieve configured payment prices for package
* @param $package
*
* @return array
*/
public static function get_package_prices($package) {
return self::_get_package_prices($package);
}
public static function get_package_pricing_commission($package) {
return self::_get_package_pricing_commision($package);
}
/**
* Persist payment prices configuration for package
* @param $package
* @param $pricing_rules
*/
public static function set_package_prices($package, $pricing_rules, $commision) {
if ( isset( $pricing_rules ) ) {
$package->update_meta_data( '_wiaas_pricing_rules', $pricing_rules );
$package->update_meta_data('_package_pricing_commision', $commision, true);
} else {
$package->delete_meta_data( '_wiaas_pricing_rules' );
}
$package->save_meta_data();
}
// PRIVATE
private static function _get_package_prices($package) {
$pricing_rules = $package->get_meta( '_wiaas_pricing_rules' );
$commision = self::_get_package_pricing_commision($package);
$prices = array();
foreach ($pricing_rules as $type => $pricing_rule) {
$pay_type = self::$pay_types[$type];
$prices[$type] = array(
'id' => $type,
'payment_type' => $pay_type['title'],
'max_contract_period' => $pay_type['max_contract_period'],
'package_pay_period' => floatval($pay_type['package_pay_period']),
'period_unit' => $pay_type['period_unit'],
'services_contract_period' => $pay_type['services_contract_period'],
'commision_split' => $commision / 100.00,
'minimal_fixed_price' => floatval($pricing_rule['minimal_fixed_price']),
'principal_amount' => floatval($pricing_rule['principal_amount']),
'minimal_services_price' => floatval($pricing_rule['minimal_services_price']),
);
}
return $prices;
}
private static function _get_package_pricing_commision($package) {
$commision = $package->get_meta( '_package_pricing_commision', true);
if (!isset($commision) || $commision === '') {
return 50.00;
}
return (float) $commision;
}
}
Wiaas_Package_Pricing::init();

View File

@@ -0,0 +1,139 @@
<?php
defined( 'ABSPATH' ) || exit;
/**
* Class Wiaas_Product_Pricing
*/
class Wiaas_Product_Pricing {
public static function init() {
add_filter('woocommerce_get_price_html', array(__CLASS__, 'get_product_price_html'), 10, 2);
add_filter('woocommerce_bundled_item_price', array(__CLASS__, 'get_bundled_product_fixed_price'), 10, 4);
add_filter('woocommerce_bundled_item_is_priced_individually', array(__CLASS__, 'is_bundled_product_price_fixed'), 10, 2);
add_filter('woocommerce_bundles_process_bundled_item_admin_data', array(__CLASS__, 'handle_bundled_product_pricing'));
}
/**
* Configures each bundled product to be priced individually as default setting
* @param $bundled_item_data
*
* @return mixed
*/
public static function handle_bundled_product_pricing($bundled_item_data) {
$bundled_item_data[ 'priced_individually' ] = 'yes';
return $bundled_item_data;
}
/**
* Checks if bunded product has fixed price
* @param $is_priced_individually
* @param $bundled_item
*
* @return bool
*/
public static function is_bundled_product_price_fixed($is_priced_individually, $bundled_item) {
$bundled_product = $bundled_item->product;
return isset($bundled_product) ?
!self::_get_is_product_price_recurring($bundled_product) :
false;
}
/**
* Retrives fixed price for bundled product
* @param $price
* @param $product
* @param $discount
* @param $bundled_item
*
* @return int
*/
public static function get_bundled_product_fixed_price($price, $product, $discount, $bundled_item) {
if (self::_get_is_product_price_recurring($product)) {
return 0;
}
return $price;
}
/**
* Builds html for product
* @param $price
* @param $object
*
* @return string
*/
public static function get_product_price_html($price, $object) {
if ($object instanceof WC_Product_Simple) {
$is_recurring = self::_get_is_product_price_recurring($object);
if ($is_recurring) {
$price .= ' / month';
}
}
return $price;
}
/**
* Retrieves product configured price
* @param $product
*
* @return array
*/
public static function get_product_price($product) {
$is_recurring = self::_get_is_product_price_recurring($product);
$pay_period = self::_get_product_price_pay_period($product);
return array(
'price' => $product->get_price(),
'is_recurring' => $is_recurring,
'pay_period' => $pay_period
);
}
/**
* Sets configured product price
* @param $product
* @param $price
* @param bool $is_recurring
* @param int $pay_period
*/
public static function set_product_price($product, $price, $is_recurring = false, $pay_period = 0) {
$product->set_price($price);
self::_set_is_product_price_recurring($product, $is_recurring);
self::_set_product_price_pay_period($product, $pay_period);
$product->save_meta_data();
}
// PRIVATE
private static function _get_is_product_price_recurring($product) {
return $product->get_meta( '_wiaas_is_product_price_recurring', true) === 'yes';
}
private static function _get_product_price_pay_period($product) {
return (int) $product->get_meta( '_wiaas_product_pay_period', true);
}
private static function _set_is_product_price_recurring($product, $is_recurring) {
$product->update_meta_data('_wiaas_is_product_price_recurring', $is_recurring ? 'yes' : 'no');
}
private static function _set_product_price_pay_period($product, $pay_period) {
$product->update_meta_data('_wiaas_product_pay_period', (int) $pay_period);
}
}
Wiaas_Product_Pricing::init();

View File

@@ -0,0 +1,51 @@
<?php
/**
* Copy of Excel's PMT function.
* Credit: http://thoughts-of-laszlo.blogspot.nl/2012/08/complete-formula-behind-excels-pmt.html
*
* @param double $interest The interest rate for the loan.
* @param int $num_of_payments The total number of payments for the loan in months.
* @param double $PV The present value, or the total amount that a series of future payments is worth now;
* Also known as the principal.
* @param double $FV The future value, or a cash balance you want to attain after the last payment is made.
* If fv is omitted, it is assumed to be 0 (zero), that is, the future value of a loan is 0.
* @param int $Type Optional, defaults to 0. The number 0 (zero) or 1 and indicates when payments are due.
* 0 = At the end of period
* 1 = At the beginning of the period
*
* @return float
*/
function wiaas_PMT($interest, $num_of_payments, $PV, $FV = 0.00, $Type = 0){
/*$interest = $interest / 12;
$xp=pow((1+$interest),$num_of_payments);
return
($PV* $interest*$xp/($xp-1)+$interest/($xp-1)*$FV)*
($Type==0 ? 1 : 1/($interest+1));*/
$rates = [
24 => 4.282,
30 => 3.451,
36 => 2.896,
42 => 2.500,
48 => 2.223,
54 => 2.025,
60 => 1.834
];
$interest = isset($rates[$num_of_payments]) ? $rates[$num_of_payments] : 10;
return round($PV * ($interest / 100));
}
function wiaas_get_recurrent_price_mortage($principal_amount, $pay_period, $margin, $interest_rate) {
$new_principal_amount = $principal_amount - $margin;
$interest_rate = $interest_rate / 100;
$fixed_mortage = wiaas_PMT($interest_rate, $pay_period, $new_principal_amount);
return round($fixed_mortage, 2);
}
function wiaas_get_price_margin($fixed_price, $principal_amount, $total_cost) {
$total_gain = $fixed_price + $principal_amount;
return $total_gain - $total_cost;
}