Add support for package payment methods and basic checkout proccess

This commit is contained in:
Almira Krdzic
2018-08-29 07:56:37 +02:00
parent 5ac3bfff36
commit 438d92e16e
35 changed files with 1600 additions and 309 deletions

View File

@@ -0,0 +1,92 @@
#wiaas_product_price {
padding: 20px;
}
#wiaas_product_price label {
display:block;
line-height: 24px;
float: left;
width: 270px;
padding: 0;
margin: 0;
}
#wiaas_product_price_controls {
float: right;
}
#wiaas_product_price_controls > select {
margin-right: 20px;
}
#wiaas_product_pricing_rules h4 {
border-bottom: 1px solid #E5E5E5;
padding-bottom: 6px;
font-size: 1.2em;
margin: 1em 0 1em;
text-transform: none;
margin-top: 0px;
cursor: move;
}
#wiaas_product_pricing_rules > .wiaas-pricing-rule {
border-color:#dfdfdf;
border-width:1px;
border-style:solid;
-moz-border-radius:3px;
-khtml-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
padding: 0;
border-style:solid;
border-spacing:0;
background-color:#F9F9F9;
margin-bottom: 12px;
margin-left: 12px;
}
#wiaas_product_pricing_rules > .wiaas-pricing-rule .section {
display: block;
overflow: hidden;
zoom: 1;
margin: 9px 0 0;
font-size: 12px;
padding: 5px 9px;
}
#wiaas_product_pricing_rules > .wiaas-pricing-rule input, #wiaas_product_pricing_rules .wiaas_input_container {
width: 160px;
}
#wiaas_product_pricing_rules .wiaas_label_container {
width: 270px;
}
.delete_wiaas_pricing_rule {
color: #a00;
float: right;
font-size: 10px;
font-weight: 400;
letter-spacing: 0.5px;
}
.wiaas_hidden {
display: none;
}
.wiaas_separator {
display: inline-block;
width: 1px;
background-color: #727272;
opacity: .25;
height: 28px;
margin: 0 10px;
}
.wiaas-pricing-rule table {
padding: 5px 9px;
}
.wiaas-pricing-rule tr {
font-size: 12px;
height: 36px;
}

View File

@@ -0,0 +1,356 @@
<?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();

View File

@@ -0,0 +1,317 @@
<?php
class Wiaas_Cart_API {
/**
* Endpoint namespace.
*
* @var string
*/
private static $namespace = 'wiaas/cart';
public static function register_routes() {
register_rest_route( self::$namespace, 'count', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_cart_count'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
) );
register_rest_route( self::$namespace, 'items', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_cart_items'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
) );
register_rest_route( self::$namespace, 'documents', array(
'methods' => 'GET',
'callback' => array(__CLASS__, 'get_cart_documents'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
) );
register_rest_route( self::$namespace, 'add', array(
'methods' => 'post',
'callback' => array(__CLASS__, 'add_package_to_cart'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
) );
register_rest_route( self::$namespace, 'remove', array(
'methods' => 'post',
'callback' => array(__CLASS__, 'remove_package_from_cart'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
) );
register_rest_route( self::$namespace, 'update-quantity', array(
'methods' => 'post',
'callback' => array(__CLASS__, 'update_package_quantity'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
) );
register_rest_route( self::$namespace, 'place-order', array(
'methods' => 'post',
'callback' => array(__CLASS__, 'place_order'),
'permission_callback' => array( __CLASS__, 'permission_check' ),
) );
}
public static function permission_check() {
if (!is_user_logged_in()) {
return new WP_Error( 'wiaas_rest_authorization_required',
__( 'Sorry, only authorized users can access!', 'wiaas' ),
array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Get cart count
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @return WP_REST_Response
*/
public static function get_cart_count() {
$items = WC()->cart->get_cart_contents();
$count = 0;
foreach ($items as $key => $item) {
if (isset($item['bundled_by'])) {
continue;
}
$count++;
}
return new WP_REST_Response(array(
'count' => $count,
));
}
/**
* Get cart items
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @return WP_REST_Response
*/
public static function get_cart_items() {
$items = WC()->cart->get_cart_contents();
$result = array();
foreach ($items as $key => $item) {
if (isset($item['bundled_by'])) {
continue;
}
$package = wc_get_product($item['product_id']);
$result[] = array(
'idPackage' => $item['product_id'],
'key' => $item['key'],
'packageName' => $package->get_title(),
'additionalPackages' => array(),
'areAdditionalAvailable' => true,
'areOptionsAvailable' => true,
'bids' => array(),
'commercialLead' => 'Coor Service Management',
'country' => array(
'currency' => 'SEK'
),
'options' => array(),
'quantity' => $item['quantity'],
'idPayType' => $item['_wiaas_price']['id'],
'payType' => $item['_wiaas_price']['payment_type'],
'periodUnit' => $item['_wiaas_price']['period_unit'],
'idPrice' => $item['_wiaas_price']['id'],
'fixedPrice' => $item['_wiaas_price']['minimal_fixed_price'],
'recurentPrice' => $item['_wiaas_price']['recurrent_price'],
'servicesPrice' => $item['_wiaas_price']['minimal_services_price'],
'totalPrices' => array(
'fixedPrice' => $item['_wiaas_price']['minimal_fixed_price'],
'recurentPrice' => $item['_wiaas_price']['recurrent_price'],
'servicesPrice' => $item['_wiaas_price']['minimal_services_price'],
),
'status' => 'available',
'idCommercialLead' => 14,
);
}
return new WP_REST_Response(
array(
'cartItems' => $result,
'totalPrice' => ''
)
);
}
/**
* Get cart documents
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @return WP_REST_Response
*/
public static function get_cart_documents() {
return new WP_REST_Response(array(
'areFilesUploaded' => true,
'templates' => array(),
'uploaded' => array()
));
}
/**
* Add package to cart
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @return WP_REST_Response
* @throws Exception
*/
public static function add_package_to_cart() {
$package_id = $_POST['package_id'];
$price_id = $_POST['price_id'];
$package = wc_get_product($package_id);
$prices = Wiaas_Package_Pricing::get_package_prices($package);
$selected_price = $prices[$price_id];
WC()->session->set('wiaas_price_' . $package_id, $price_id);
$success = WC()->cart->add_to_cart(
$package_id,
1,
0,
array(),
array(
'_wiaas_price' => $selected_price,
));
if ($success) {
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'success',
'message' => 'PACKAGE_ADDED'
)
)
));
}
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'error',
'message' => 'PACKAGE_ALREADY_IN_CART'
)
)
));
}
/*
* Remove package from cart
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
*/
public static function remove_package_from_cart() {
$package_cart_key = $_POST['package_item_key'];
$success = WC()->cart->remove_cart_item($package_cart_key);
if ($success) {
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'success',
'message' => 'PACKAGE_REMOVED'
)
)
));
}
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'error',
'message' => 'PACKAGE_NOT_REMOVED'
)
)
));
}
/**
* Update package quantity in cart
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @return WP_REST_Response
*/
public static function update_package_quantity() {
$package_item_key = $_POST['package_item_key'];
$new_quantity = $_POST['quantity'];
$success = WC()->cart->set_quantity($package_item_key, $new_quantity, true);
if ($success) {
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'success',
'message' => 'QUANTITY_UPDATED'
)
)
));
}
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'error',
'message' => 'QUANTITY_NOT_UPDATED'
)
)
));
}
/**
* Placing order as part of checkout process
* TODO: This implementation is temporary to enable flow trough basic checkout process and should be changed
* @return WP_REST_Response
* @throws Exception
*/
public static function place_order() {
$details = $_POST['details'];
$vat_code = $_POST['vat'];
$company_name = $_POST['companyName'];
$delivery_address = $_POST['delivery'];
$billing_address = $_POST['billing'];
$order_id = WC()->checkout()->create_order(array());
$order = wc_get_order( $order_id );
$order->set_shipping_city($delivery_address['city']);
$order->set_shipping_country($delivery_address['countryName']);
$order->set_shipping_address_1($delivery_address['detailedAddress']);
$order->set_shipping_postcode($delivery_address['zipCode']);
$order->set_billing_city($billing_address['city']);
$order->set_billing_country($billing_address['countryName']);
$order->set_billing_address_1($billing_address['detailedAddress']);
$order->set_billing_postcode($billing_address['zipCode']);
$order->set_billing_first_name($billing_address['firstName']);
$order->set_billing_last_name($billing_address['lastName']);
$order->payment_complete();
add_post_meta($order_id, '_wiaas_vat_code', $vat_code);
add_post_meta($order_id, '_wiaas_company_name', $company_name);
add_post_meta($order_id, '_wiaas_project_id', $details['idProject']);
add_post_meta($order_id, '_wiaas_reference', $details['reference']);
add_post_meta($order_id, '_wiaas_tender', $details['tender']);
// $order->get_li
WC()->cart->empty_cart( true );
return new WP_REST_Response(array(
'messages' => array(
array(
'code' => 'success',
'message' => 'ORDER_PLACED'
)
),
'details' => $details
));
}
}

View File

@@ -0,0 +1,14 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Wiaas_Admin {
public static function init() {
require_once dirname( __FILE__ ) . '/admin/package/class-wiaas-admin-package-pricing.php';
}
}
Wiaas_Admin::init();

View File

@@ -32,11 +32,13 @@ class Wiaas_API {
#Delivery process controller
include_once dirname( __FILE__ ) . '/api/class-wiaas-rest-delivery-process-api.php';
include_once dirname( __FILE__ ) . '/api/class-wiaas-cart-api.php';
}
public static function register_rest_routes() {
$controllers = array(
'Wiass_REST_Delivery_Process_API'
'Wiass_REST_Delivery_Process_API',
'Wiaas_Cart_API'
);
foreach ( $controllers as $controller ) {

View File

@@ -93,6 +93,8 @@ class Wiaas_Order {
$data = self::_append_commercial_lead_info($data, $order, $request);
$data = self::_append_wiaas_order_details($data, $order, $request);
$response->set_data($data);
return $response;
@@ -102,6 +104,17 @@ class Wiaas_Order {
* PRIVATE
*/
/**
* Append specific wiaas order details, like reference
* @param $data
* @param $order
* @param $request
*/
private static function _append_wiaas_order_details($data, $order, $request) {
$data['reference'] = get_post_meta($order->get_id(), '_wiaas_reference', true);
return $data;
}
/**
* Appends additional wiaas customer lead info to order json response
* @param $data
@@ -160,18 +173,22 @@ class Wiaas_Order {
foreach ($data['line_items'] as $index => $product_line) {
// add only product lines that represent product bundles
if (empty($product_line['bundled_by'])) {
$item = $order->get_item($product_line['id']);
$payment = $item->get_meta('_wiaas_payment', true);
# lock all products to `Purchase` payment type
$product_line['payment_type'] = 'Purchase';
$product_line['payment_type'] = $payment['payment_type'];
# lock all products to have no service
$product_line['service_price'] = 0;
$product_line['service_contract_period'] = 0;
$product_line['max_contract_period'] = 36;
$product_line['period_unit'] = 'month';
$product_line['service_price'] = $payment['minimal_services_price'];
$product_line['service_contract_period'] = $payment['services_contract_period'];
$product_line['max_contract_period'] = $payment['max_contract_period'];
$product_line['period_unit'] = $payment['period_unit'];
# simplify payment for all products
$product_line['recurring_price'] = 0;
$product_line['pay_period'] = 0;
$product_line['recurring_price'] = $payment['recurrent_price'];
$product_line['pay_period'] = $payment['package_pay_period'];
# collect status from order
if ($data['status'] === 'completed') {

View File

@@ -0,0 +1,46 @@
<?php
class Wiaas_Package {
public static function init() {
require_once dirname( __FILE__ ) . '/package/class-wiaas-package-pricing.php';
add_filter('woocommerce_rest_prepare_product_object', array(__CLASS__, 'transform_rest_package'), 999, 3);
}
/**
* Transform package json response with wiaas information
* @param $response
* @param $package
* @param $request
*
* @return mixed
*/
public static function transform_rest_package($response, $package, $request) {
$data = $response->get_data();
$data = self::_append_package_prices($data, $package, $request);
$response->set_data($data);
return $response;
}
/**
* Append configured package prices on package json response
* @param $data
* @param $package
* @param $request
*
* @return mixed
*/
private static function _append_package_prices($data, $package, $request) {
$package_prices = array_values(Wiaas_Package_Pricing::get_package_prices($package));
$data['prices'] = $package_prices;
return $data;
}
}
Wiaas_Package::init();

View File

@@ -0,0 +1,167 @@
<?php
/**
* 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
);
/**
* Meta key for pricing rules
* @var string
*/
private static $package_prices_meta_key = '_wiaas_pricing_rules';
public static function init() {
add_filter( 'woocommerce_product_get_price', array( __CLASS__, 'on_get_price' ), 10, 2 );
add_filter('woocommerce_checkout_create_order_line_item_object', array( __CLASS__, 'record_price_for_order_line_item' ), 10, 4);
}
/**
* 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 customer selected package price from session when adding package to cart
* @param $base_price
* @param $product
*
* @return mixed
*/
public static function on_get_price($base_price, $package) {
if ( empty( $package ) ||
empty( $base_price ) ||
$package->get_type() !== 'bundle' ||
!isset(WC()->session)) {
return $base_price;
}
$result_price = $base_price;
$preferred_price_id = WC()->session->get('wiaas_price_' . $package->get_id(), null);
if (isset($preferred_price_id)) {
$prices = self::get_package_prices($package);
$preferred_price = $prices[$preferred_price_id];
$result_price = $preferred_price['minimal_fixed_price'];
}
return $result_price;
}
/**
* Persist used payment type informations for package in corresponding order line item
* @param $order_item
* @param $cart_item_key
* @param $cart_item
* @param $order
*
* @return mixed
*/
public static function record_price_for_order_line_item($order_item, $cart_item_key, $cart_item, $order) {
if (wc_pb_is_bundle_container_cart_item($cart_item)) {
$order_item->add_meta_data('_wiaas_payment', $cart_item['_wiaas_price']);
}
return $order_item;
}
/**
* Retrieve configured payment prices for package
* @param $package
*
* @return array
*/
public static function get_package_prices($package) {
$pricing_rules = $package->get_meta( self::$package_prices_meta_key );
$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'],
'minimal_fixed_price' => floatval($pricing_rule['minimal_fixed_price']),
'principal_amount' => floatval($pricing_rule['principal_amount']),
'recurrent_price' => 0,
'minimal_services_price' => floatval($pricing_rule['minimal_services_price']),
);
}
return $prices;
}
/**
* Persist payment prices configuration for package
* @param $package
* @param $pricing_rules
*/
public static function set_package_prices($package, $pricing_rules) {
if ( isset( $pricing_rules ) ) {
$package->update_meta_data( self::$package_prices_meta_key, $pricing_rules );
} else {
$package->delete_meta_data( self::$package_prices_meta_key );
}
$package->save_meta_data();
}
}
Wiaas_Package_Pricing::init();

View File

@@ -26,6 +26,10 @@ include_once WIAAS_DIR . '/includes/class-wiaas-delivery-process.php';
include_once WIAAS_DIR . '/includes/class-wiaas-order.php';
include_once WIAAS_DIR . '/includes/class-wiaas-package.php';
include_once WIAAS_DIR . '/includes/class-wiaas-user.php';
include_once WIAAS_DIR . '/includes/class-wiaas-api.php';
include_once WIAAS_DIR . '/includes/class-wiaas-admin.php';