356 lines
12 KiB
PHP
356 lines
12 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Class Wiaas_Package_Pricing
|
|
*/
|
|
class Wiaas_Admin_Package_Pricing {
|
|
|
|
public static function init() {
|
|
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
|
|
|
|
add_action( 'wp_ajax_create_empty_pricing_rule', array(__CLASS__, 'create_empty_pricing_rule') );
|
|
|
|
add_action( 'woocommerce_product_data_tabs', array( __CLASS__, 'package_data_tabs' ) );
|
|
add_action( 'woocommerce_product_data_panels', array( __CLASS__, 'package_data_panel' ) );
|
|
|
|
add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'process_meta_box' ), 1, 2 );
|
|
}
|
|
|
|
public static function enqueue_scripts() {
|
|
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
|
|
wp_enqueue_style( 'wiaas_admin_styles', $plugin_url . '/assets/css/package.css' );
|
|
}
|
|
|
|
public static function create_empty_pricing_rule() {
|
|
$pay_type = $_POST['pay_type'];
|
|
$pricing_rule_sets = array();
|
|
$pricing_rule_sets[ $pay_type ] = Wiaas_Package_Pricing::get_empty_pricing_rule();
|
|
self::_render_pricing_rules( $pricing_rule_sets );
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public static function package_data_panel() {
|
|
|
|
global $post;
|
|
$package = wc_get_product( $post->ID );
|
|
$pricing_rules = Wiaas_Package_Pricing::get_package_prices($package);
|
|
|
|
?>
|
|
<div id="wiaas_package_price" class="panel woocommerce_options_panel wc_gte_30">
|
|
<div id="wiaas_package_pricing_rules" data-setindex="<?php echo count( $pricing_rules ); ?>">
|
|
<?php self::meta_box_javascript(); ?>
|
|
<?php self::_render_pricing_rules( $pricing_rules ); ?>
|
|
</div>
|
|
<?php self::_render_pricing_controls( $pricing_rules ); ?>
|
|
<div class="clear"></div>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
private static function _render_pricing_rules( $pricing_rule_sets ) {
|
|
$available_pay_types = Wiaas_Package_Pricing::get_available_pay_types();
|
|
foreach ( $pricing_rule_sets as $name => $pricing_rule_set ) {
|
|
$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_set['minimal_fixed_price'] ?>"
|
|
type="text" />
|
|
</td>
|
|
<td></td>
|
|
</tr>
|
|
<?php self::_render_pricing_rule_recurrent_price($name, $pay_type, $pricing_rule_set) ?>
|
|
<?php self::_render_pricing_rule_principal_amount($name, $pay_type, $pricing_rule_set) ?>
|
|
<?php self::_render_pricing_rule_services_price($name, $pay_type, $pricing_rule_set) ?>
|
|
<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
|
|
}
|
|
}
|
|
|
|
private static function _render_pricing_rule_services_price($name, $pay_type, $pricing_rule) {
|
|
$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
|
|
}
|
|
}
|
|
|
|
private static function _render_pricing_rule_principal_amount($name, $pay_type, $pricing_rule) {
|
|
if ($pay_type['package_pay_period'] === 0) {
|
|
return;
|
|
}
|
|
|
|
$value_per_month = round($pricing_rule['principal_amount'] / $pay_type['package_pay_period'], 2);
|
|
|
|
?>
|
|
<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
|
|
}
|
|
|
|
private static function _render_pricing_rule_recurrent_price($name, $pay_type, $pricing_rule) {
|
|
$value = $pricing_rule['minimal_services_price'];
|
|
if ($pricing_rule['principal_amount'] > 0 && $pay_type['package_pay_period'] > 0) {
|
|
$value += $pricing_rule['principal_amount'] / $pay_type['package_pay_period'];
|
|
}
|
|
|
|
?>
|
|
<tr>
|
|
<td><?php _e( 'Minimal recurrent price:', 'wiaas' ); ?></td>
|
|
<td>
|
|
<span id="wiaas_minimal_recurrent_price_<?php echo $name; ?>">
|
|
<?php echo round($value, 2) ?>
|
|
</span>
|
|
</td>
|
|
</tr>
|
|
<?php
|
|
}
|
|
|
|
private static function _render_pricing_controls($pricing_rule_sets) {
|
|
$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="section <?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>
|
|
<?php
|
|
}
|
|
|
|
private static function meta_box_javascript() {
|
|
?>
|
|
<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 principal_amount = parseFloat($(this).val() / $(this).data('period')) || 0;
|
|
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(2));
|
|
$(`#wiaas_minimal_recurrent_package_price_${name}`).text(principal_amount.toFixed(2));
|
|
});
|
|
});
|
|
</script>
|
|
<?php
|
|
}
|
|
|
|
public function process_meta_box( $post_id, $post ) {
|
|
Wiaas_Package_Pricing::set_package_prices(
|
|
wc_get_product( $post_id ),
|
|
$_POST['wiaas_pricing_rules']);
|
|
}
|
|
}
|
|
|
|
Wiaas_Admin_Package_Pricing::init();
|