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,78 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Wiaas_Package_Pricing
*/
class Wiaas_Admin_Package_Pricing {
public static function init() {
add_action( 'woocommerce_product_data_tabs', array( __CLASS__, 'package_data_tabs' ) );
add_action( 'woocommerce_product_data_panels', array( __CLASS__, 'package_data_panel' ) );
add_action( 'wp_ajax_create_empty_pricing_rule', array(__CLASS__, 'create_empty_pricing_rule') );
add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'process_meta_box' ), 1, 2 );
}
/**
* Creates and renders new empty package pricing rule
*/
public static function create_empty_pricing_rule() {
$pay_type = $_POST['pay_type'];
$pricing_rules = array();
$pricing_rules[ $pay_type ] = Wiaas_Package_Pricing::get_empty_pricing_rule();
require 'views/html-package-pricing-rules-list.php';
die();
}
/**
* Registers wiaas pricing tab for package data
* @param $tabs
*
* @return array
*/
public static function package_data_tabs($tabs) {
$tabs[ 'bundled_packages_price' ] = array(
'label' => __( 'Pricing', 'wiaas' ),
'target' => 'wiaas_package_price',
'class' => array( 'show_if_bundle', 'bundled_package_tab' ),
'priority' => 50
);
return $tabs;
}
/**
* Renderes wiaas pricing tab content for package
*/
public static function package_data_panel() {
global $post;
$package = wc_get_product( $post->ID );
$pricing_rules = Wiaas_Package_Pricing::get_package_prices($package);
$commission = Wiaas_Package_Pricing::get_package_pricing_commission($package);
include 'views/html-package-pricing.php';
}
/**
* Saves posted package pricing rules
* @param $post_id
* @param $post
*/
public function process_meta_box( $post_id, $post ) {
Wiaas_Package_Pricing::set_package_prices(
wc_get_product( $post_id ),
$_POST['wiaas_pricing_rules'],
$_POST['wiaas_pricing_rules_commision']);
}
}
Wiaas_Admin_Package_Pricing::init();

View File

@@ -0,0 +1,50 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Wiaas_Admin_Product_Pricing
*/
class Wiaas_Admin_Product_Pricing {
public static function init() {
add_action( 'woocommerce_product_options_pricing', array( __CLASS__, 'product_pricing' ) );
add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'process_meta_box' ), 1, 2 );
}
/**
* Renders wiaas pricing options for product
*/
public static function product_pricing() {
global $post;
$product = wc_get_product( $post->ID );
$product_pricing = Wiaas_Product_Pricing::get_product_price($product);
include 'views/html-product-pricing.php';
}
/**
* Saves posted product pricing settings
* @param $post_id
* @param $post
*/
public function process_meta_box( $post_id, $post ) {
$product = wc_get_product( $post_id );
if ($product->get_type() === 'simple') {
$is_recurring = $_POST['_wiaas_recurring_price'] === 'yes';
$pay_period = isset($_POST['_wiaas_recurring_pay_period']) ? $_POST['_wiaas_recurring_pay_period'] : 0;
Wiaas_Product_Pricing::set_product_price($product, $product->get_price(), $is_recurring, $pay_period);
}
}
}
Wiaas_Admin_Product_Pricing::init();

View File

@@ -0,0 +1,50 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<?php
if ($pay_type['package_pay_period'] === 0) {
return;
}
$value_per_month = round( wiaas_PMT(
Wiaas_Pricing::INTEREST_RATE,
$pay_type['package_pay_period'],
$pricing_rule['principal_amount']), 0);
?>
<tr>
<td><?php _e( 'Principal amount:', 'wiaas' ); ?></td>
<td class="wiaas_input_container">
<input
id="wiaas_principal_amount_<?php echo $name; ?>"
class="wiaas_principal_amount"
data-name="<?php echo $name; ?>"
data-period="<?php echo $pay_type['package_pay_period']; ?>"
name="wiaas_pricing_rules[<?php echo $name; ?>][principal_amount]"
value="<?php echo $pricing_rule['principal_amount'] ?>"
type="text" />
</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
<?php _e( 'Minimal recurrent package price ( ' .
$pay_type['services_contract_period'] .
' ' . $pay_type['period_unit'] .
'): ', 'wiaas' ); ?>
</td>
<td>
<span id="wiaas_minimal_recurrent_package_price_<?php echo $name; ?>">
<?php echo $value_per_month ?>
</span>
<?php echo ' / ' . $pay_type['period_unit'] ?>
</td>
</tr>
<?php
?>

View File

@@ -0,0 +1,28 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<?php
$value = $pricing_rule['minimal_services_price'];
if ($pricing_rule['principal_amount'] > 0 && $pay_type['package_pay_period'] > 0) {
$value += wiaas_PMT(
Wiaas_Pricing::INTEREST_RATE,
$pay_type['package_pay_period'],
$pricing_rule['principal_amount']);
}
?>
<tr>
<td><?php _e( 'Minimal recurrent price:', 'wiaas' ); ?></td>
<td>
<span id="wiaas_minimal_recurrent_price_<?php echo $name; ?>">
<?php echo round($value, 0) ?>
</span>
</td>
</tr>
<?php
?>

View File

@@ -0,0 +1,57 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<?php
$label = 'Minimal services and support price ';
if ($pay_type['services_contract_period'] > 0) {
$label .= '(' . $pay_type['services_contract_period'] . ' ' . $pay_type['period_unit'] . ')';
} else {
$label .= '(Unbound)';
}
?>
<tr>
<td class="wiaas_label_container">
<?php _e( $label . ':', 'wiaas' ); ?>
</td>
<td class="wiaas_input_container">
<input
id="wiaas_minimal_services_price_<?php echo $name; ?>"
class="wiaas_minimal_services_price"
data-name="<?php echo $name; ?>"
data-period="<?php echo $pay_type['services_contract_period']; ?>"
name="wiaas_pricing_rules[<?php echo $name; ?>][minimal_services_price]"
value="<?php echo $pricing_rule['minimal_services_price'] ?>"
type="text" />
</td>
<td>
<span style="line-height: 28px; margin-left: 10px;">
<?php echo ' / ' . $pay_type['period_unit'] ?>
</span>
</td>
</tr>
<?php
if ($pay_type['services_contract_period'] > 0) {
?>
<tr>
<td></td>
<td>Final total:</td>
<td>
<span id="wiaas_minimal_services_price_<?php echo $name; ?>_final">
<?php echo round(
$pricing_rule['minimal_services_price'] * $pay_type['services_contract_period'],
2) ?>
</span>
</td>
</tr>
<?php
}
?>

View File

@@ -0,0 +1,59 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$available_pay_types = Wiaas_Package_Pricing::get_available_pay_types();
foreach ( $pricing_rules as $name => $pricing_rule ) {
$pay_type = $available_pay_types[$name];
?>
<div id="wiaas-pricing-rule-<?php echo $name; ?>" class="wiaas-pricing-rule">
<div class="section">
<h4 class="first"><?php echo $pay_type['title']; ?>
<a href="#" data-name="<?php echo $name; ?>"
class="delete_wiaas_pricing_rule">
REMOVE
</a>
</h4>
</div>
<div class="clear"></div>
<table>
<tbody>
<tr>
<td class="wiaas_label_container">
<?php _e( 'Minimal fixed price:', 'wiaas' ); ?>
</td>
<td>
<input
id="wiaas_minimal_fixed_price_<?php echo $name; ?>"
data-name="<?php echo $name; ?>"
name="wiaas_pricing_rules[<?php echo $name; ?>][minimal_fixed_price]"
value="<?php echo $pricing_rule['minimal_fixed_price'] ?>"
type="text" />
</td>
<td></td>
</tr>
<?php require 'html-package-pricing-rule-recurrent-price.php'; ?>
<?php require 'html-package-pricing-rule-principal-amount.php'; ?>
<?php require 'html-package-pricing-rule-services-price.php'; ?>
<tr>
<td class="wiaas_label_container">
<?php _e( 'Max contract period:', 'wiaas' ); ?>
</td>
<td>
<span id="wiaas_max_contract_period_<?php echo $name; ?>">
<?php echo $pay_type['max_contract_period'] . ' ' . $pay_type['period_unit'] ?>
</span>
</td>
<td></td>
</tr>
</tbody>
</table>
</div>
<?php
}
?>

View File

@@ -0,0 +1,209 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<div id="wiaas_package_price" class="panel woocommerce_options_panel">
<script type="text/javascript">
jQuery(document).ready(function ($) {
$("#wiaas-add-pricing").click(function (event) {
event.preventDefault();
var set_index = $("#wiaas_package_pricing_rules").data('setindex') + 1;
$("#wiaas_package_pricing_rules").data('setindex', set_index);
var pay_type = $('#wiaas_pay_type').val();
var data = {
'pay_type': pay_type,
post:<?php echo isset( $_GET['post'] ) ? $_GET['post'] : 0; ?>,
action: 'create_empty_pricing_rule'
};
$.post(ajaxurl, data, function (response) {
$('#wiaas_package_pricing_rules').append(response);
$(`#wiaas_pay_type_${pay_type}`).addClass('wiaas_hidden');
var available_options = $('#wiaas_pay_type option').not('.wiaas_hidden');
if (available_options.length === 0) {
$('#wiaas_package_price_controls').addClass('wiaas_hidden');
} else {
$('#wiaas_pay_type').val(available_options.first().val());
}
});
});
//Remove Pricing Type
$('#wiaas_package_pricing_rules').delegate('.delete_wiaas_pricing_rule', 'click', function (event) {
event.preventDefault();
if (confirm("<?php _e( 'Are you sure you would like to remove this pay type?', 'wiaas' ); ?>")) {
var name = $(this).data('name');
$('#wiaas-pricing-rule-' + name).slideUp().remove();
// append new option to controls
$(`#wiaas_pay_type_${name}`).removeClass('wiaas_hidden');
var available_options = $('#wiaas_pay_type option').not('.wiaas_hidden');
if (available_options.length === 1) {
$('#wiaas_package_price_controls').removeClass('wiaas_hidden');
$('#wiaas_pay_type').val(available_options.first().val());
}
}
});
$('#wiaas_package_pricing_rules').delegate('.wiaas_minimal_services_price', 'change', function (event) {
event.preventDefault();
var minimal_services_price = parseFloat($(this).val()) || 0;
var principal_amount = 0;
var name = $(this).data('name');
var principal_amount_input = $(`#wiaas_principal_amount_${name}`).first();
if (principal_amount_input) {
principal_amount = parseFloat(principal_amount_input.val() / principal_amount_input.data('period')) || 0;
}
$(`#wiaas_minimal_recurrent_price_${name}`).text((minimal_services_price + principal_amount).toFixed(2));
var services_contract_period = $(this).data('period');
if (services_contract_period > 0) {
var final_services_price = minimal_services_price * services_contract_period;
$(`#wiaas_minimal_services_price_${name}_final`).text(final_services_price.toFixed(2));
}
});
$('#wiaas_package_pricing_rules').delegate('.wiaas_principal_amount', 'change', function (event) {
event.preventDefault();
var value = parseFloat($(this).val());
var period = parseFloat($(this).data('period'));
var interestRate = 0.58;
var principal_amount = calculateFinancing(interestRate, period, value);
var minimal_services_price = 0;
var name = $(this).data('name');
var minimal_services_price_input = $(`#wiaas_minimal_services_price_${name}`).first();
if (minimal_services_price_input) {
minimal_services_price = parseFloat(minimal_services_price_input.val()) || 0;
}
$(`#wiaas_minimal_recurrent_price_${name}`).text((minimal_services_price + principal_amount).toFixed(0));
$(`#wiaas_minimal_recurrent_package_price_${name}`).text(principal_amount.toFixed(0));
});
/**
* Copy of Excel's PMT function.
* Credit: http://stackoverflow.com/questions/2094967/excel-pmt-function-in-js
*
* @param ratePerPeriod The interest rate for the loan.
* @param numberOfPayments The total number of payments for the loan in months.
* @param presentValue The present value, or the total amount that a series of future payments is worth now;
* Also known as the principal.
* @param futureValue 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 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
* @returns {number}
*/
function calculateFinancing(ratePerPeriod, numberOfPayments, presentValue, futureValue = 0, type = 0) {
/*var q = 0;
var c = 0;
const monthlyRatePerPeriod = ratePerPeriod / 12;
if (monthlyRatePerPeriod !== 0.0) {
// Interest rate exists
q = Math.pow(1 + monthlyRatePerPeriod, numberOfPayments);
c = (monthlyRatePerPeriod * (futureValue + (q * presentValue))) / ((-1 + q) * (1 + monthlyRatePerPeriod * (type)));
return c.toFixed(2);
} else if (numberOfPayments !== 0.0) {
// No interest rate, but number of payments exists
return -(futureValue + presentValue) / numberOfPayments;
}
return 0;*/
const rates = {
24 : 4.282,
30 : 3.451,
36 : 2.896,
42 : 2.500,
48 : 2.223,
54 : 2.025,
60 : 1.834
};
const interest = rates[numberOfPayments] || 10;
return presentValue * (interest / 100);
}
});
</script>
<div class="options_group">
<?php
woocommerce_wp_text_input(
array(
'id' => '_wiaas_price_commision',
'name' => 'wiaas_pricing_rules_commision',
'value' => $commission,
'label' => __( 'Commision (Percent):', 'wiaas' ),
'type' => 'number',
)
);
?>
</div>
<div class="options_group">
<div class="wc-metaboxes-wrapper">
<?php
$has_available_pay_types = false;
$available_pay_types = Wiaas_Package_Pricing::get_available_pay_types();
foreach ($available_pay_types as $name => $pay_type) {
if (!isset($pricing_rule_sets[$name])) {
$has_available_pay_types = true;
break;
}
}
$class = $has_available_pay_types ? '' : 'wiaas_hidden';
?>
<div id="wiaas_package_price_controls" class="toolbar toolbar-top <?php echo $class ?>">
<select id="wiaas_pay_type" name="wiaas-pay-type" class="pricing_rule_mode">
<?php
foreach ($available_pay_types as $name => $pay_type) {
?>
<option
value="<?php echo $name ?>"
id="wiaas_pay_type_<?php echo $name ?>"
class="<?php echo $class ?>"
>
<?php _e( $pay_type['title'], 'wiaas' ); ?>
</option>
<?php
}
?>
</select>
<button
title="<?php _e( 'Add pricing type.', 'wiaas' ); ?>"
id="wiaas-add-pricing"
type="button"
class="button button-primary">
<?php _e( 'Add Pricing Type', 'wiaas' ); ?>
</button>
</div>
<div id="wiaas_package_pricing_rules" class="wc-metaboxes">
<?php
require 'html-package-pricing-rules-list.php';
?>
</div>
</div>
</div>
</div>

View File

@@ -0,0 +1,36 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<script>
jQuery(document).ready(function($) {
$("#general_product_data").find('.pricing').addClass('hide_if_bundle');
$("#general_product_data").find('.pricing').removeClass('show_if_bundle');
if ($('#product-type').val() === 'bundle') {
$("#general_product_data .pricing").hide();
}
});
</script>
<?php
woocommerce_wp_checkbox(
array(
'id' => '_wiaas_recurring_price',
'value' => $product_pricing['is_recurring'] ? 'yes' : 'no',
'data_type' => 'price',
'label' => __( $product->get_category_ids()[0], 'wiaas' ),
)
);
woocommerce_wp_text_input(
array(
'id' => '_wiaas_recurring_pay_period',
'value' => $product_pricing['pay_period'],
'label' => __( 'Pay period (Months)', 'wiaas' ),
'type' => 'number',
)
);
?>