Add package addons and options
This commit is contained in:
@@ -90,3 +90,8 @@
|
||||
font-size: 12px;
|
||||
height: 36px;
|
||||
}
|
||||
|
||||
|
||||
#wiaas_package_option_groups div.expand-close {
|
||||
float: right;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Admin_Package_Addon {
|
||||
|
||||
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( 'woocommerce_process_product_meta', array( __CLASS__, 'process_meta_box' ));
|
||||
}
|
||||
|
||||
public static function package_data_tabs($tabs) {
|
||||
$tabs[ 'bundled_packages_addons' ] = array(
|
||||
'label' => __( 'Add-ons', 'wiaas' ),
|
||||
'target' => 'wiaas_package_addons',
|
||||
'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 );
|
||||
|
||||
$addons = Wiaas_Package_Addon::get_package_addons($package);
|
||||
|
||||
?>
|
||||
<div id="wiaas_package_addons" class="panel woocommerce_options_panel wc_gte_30">
|
||||
<div class="options_group">
|
||||
<p class="form-field">
|
||||
<label for="wiaas_addon_packages"><?php esc_html_e( 'Additional packages:', 'wiaas' ); ?></label>
|
||||
<select
|
||||
class="wc-product-search"
|
||||
multiple="multiple"
|
||||
style="width: 50%;"
|
||||
id="wiaas_addon_packages"
|
||||
name="wiaas_addon_packages[]"
|
||||
data-sortable="true"
|
||||
data-placeholder="<?php esc_attr_e( 'Search for a product…', 'wiaas' ); ?>"
|
||||
data-action="woocommerce_json_search_products"
|
||||
data-exclude="<?php echo intval( $package->get_id() ); ?>">
|
||||
<?php
|
||||
foreach ( $addons as $addon ) {
|
||||
echo '<option value="' . esc_attr( $addon->get_id() ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $addon->get_formatted_name() ) . '</option>';
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
<?php echo wc_help_tip( __( 'This lets you choose which packages will be available as addons.', 'wiaas' ) );?>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
public static function meta_box_javascript() {
|
||||
|
||||
}
|
||||
|
||||
public static function process_meta_box($package_id) {
|
||||
|
||||
$package = wc_get_product($package_id);
|
||||
|
||||
$addons_ids = isset( $_POST['wiaas_addon_packages'] ) ?
|
||||
array_filter( array_map( 'intval', (array) $_POST['wiaas_addon_packages'] ) ) :
|
||||
array();
|
||||
|
||||
Wiaas_Package_Addon::set_package_addons(
|
||||
$package,
|
||||
$addons_ids);
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin_Package_Addon::init();
|
||||
@@ -0,0 +1,242 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Admin_Package_Option_Groups {
|
||||
|
||||
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_wiaas_create_empty_option_group', array(__CLASS__, 'create_empty_option_group') );
|
||||
|
||||
add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'process_meta_box' ));
|
||||
}
|
||||
|
||||
public static function create_empty_option_group() {
|
||||
$option_group = array(
|
||||
'name' => 'Untitled',
|
||||
'id' => uniqid('option_'),
|
||||
'default' => false,
|
||||
'options' => array()
|
||||
);
|
||||
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
$( document.body ).trigger( 'wc-enhanced-select-init' );
|
||||
</script>
|
||||
<?php
|
||||
self::_render_option_group($option_group);
|
||||
?>
|
||||
<?php
|
||||
}
|
||||
|
||||
public static function package_data_tabs($tabs) {
|
||||
$tabs[ 'bundled_packages_options_groups' ] = array(
|
||||
'label' => __( 'Option groups', 'wiaas' ),
|
||||
'target' => 'wiaas_package_option_groups',
|
||||
'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 );
|
||||
|
||||
$option_groups = Wiaas_Package_Option_Groups::get_package_option_groups($package);
|
||||
|
||||
?>
|
||||
<div id="wiaas_package_option_groups" class="panel wc-metaboxes-wrapper hidden">
|
||||
<?php self::meta_box_javascript(); ?>
|
||||
<div class="toolbar toolbar-top">
|
||||
<button type="button" class="button button-primary" id="wiaas_add_option_group">
|
||||
<?php esc_html_e( 'Add new option group', 'wiaas' ); ?>
|
||||
</button>
|
||||
<span class="expand-close">
|
||||
<a href="#" class="expand_all"><?php esc_html_e( 'Expand', 'wiaas' ); ?></a>
|
||||
/
|
||||
<a href="#" class="close_all"><?php esc_html_e( 'Close', 'wiaas' ); ?></a>
|
||||
</span>
|
||||
</div>
|
||||
<div id="wiaas_package_option_groups_list" class="wc-metaboxes">
|
||||
<?php
|
||||
foreach ($option_groups as $option_group) {
|
||||
self::_render_option_group($option_group);
|
||||
}
|
||||
// render empty option group
|
||||
?>
|
||||
</div>
|
||||
<div class="toolbar">
|
||||
<span class="expand-close">
|
||||
<a href="#" class="expand_all">
|
||||
<?php esc_html_e( 'Expand', 'wiaas' ); ?>
|
||||
</a>
|
||||
/
|
||||
<a href="#" class="close_all"><?php esc_html_e( 'Close', 'wiaas' ); ?></a>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
private static function _render_option_group($group) {
|
||||
$group_options = isset($group['options']) ? $group['options'] : array();
|
||||
$id = $group['id'];
|
||||
|
||||
?>
|
||||
<div id="wiaas_option_group_<?php echo esc_attr( $id ); ?>" class="wc-metabox closed">
|
||||
<h3>
|
||||
<a
|
||||
href="#"
|
||||
data-id="<?php echo esc_attr( $id ); ?>"
|
||||
class="delete delete_wiaas_option_group">
|
||||
<?php esc_html_e( 'Remove', 'wiaas' ); ?>
|
||||
</a>
|
||||
<div class="handlediv" title="<?php esc_attr_e( 'Click to toggle', 'wiaas' ); ?>"></div>
|
||||
<strong id="wiaas_option_group_<?php echo esc_attr( $id ); ?>_title">
|
||||
<?php echo $group['name']; ?>
|
||||
</strong>
|
||||
</h3>
|
||||
<div class="wc-metabox-content">
|
||||
<table cellpadding="0" cellspacing="0">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>
|
||||
<label><?php esc_html_e( 'Name', 'wiaas' ); ?>:</label>
|
||||
<input
|
||||
type="text"
|
||||
data-id="<?php echo esc_attr( $id ); ?>"
|
||||
class="wiaas_option_group_name"
|
||||
name="wiaas_option_groups[<?php echo esc_attr( $id ); ?>][name]"
|
||||
value="<?php echo esc_attr( $group['name'] ); ?>" />
|
||||
<input
|
||||
type="hidden"
|
||||
name="wiaas_option_groups[<?php echo esc_attr( $id ); ?>][id]"
|
||||
value="<?php echo esc_attr( $id ); ?>" />
|
||||
</td>
|
||||
<td rowspan="3">
|
||||
<label><?php esc_html_e( 'Options:', 'wiaas' ); ?></label>
|
||||
<select
|
||||
class="wc-product-search"
|
||||
multiple="multiple"
|
||||
style="width: 50%;"
|
||||
name="wiaas_option_groups[<?php echo esc_attr( $id ); ?>][options][]"
|
||||
data-sortable="true"
|
||||
data-placeholder="<?php esc_attr_e( 'Search for a product…', 'wiaas' ); ?>"
|
||||
data-action="woocommerce_json_search_products"
|
||||
data-exclude="">
|
||||
<?php
|
||||
foreach ( $group_options as $group_option ) {
|
||||
echo '<option value="' . esc_attr( $group_option->get_id() ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $group_option->get_formatted_name() ) . '</option>';
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
<label>
|
||||
<input
|
||||
type="checkbox"
|
||||
data-id="<?php echo esc_attr( $id ); ?>"
|
||||
class="checkbox wiaas_option_group_default"
|
||||
<?php checked( $group['default'], true ); ?>
|
||||
name="wiaas_option_groups[<?php echo esc_attr( $id ); ?>][default]"
|
||||
value="1" />
|
||||
<?php esc_html_e( 'Set as default', 'wiaas' ); ?>
|
||||
</label>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
public static function meta_box_javascript() {
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
jQuery(document).ready(function ($) {
|
||||
$("#wiaas_add_option_group").click(function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
var data = {
|
||||
post:<?php echo isset( $_GET['post'] ) ? $_GET['post'] : 0; ?>,
|
||||
action: 'wiaas_create_empty_option_group'
|
||||
};
|
||||
|
||||
$.post(ajaxurl, data, function (response) {
|
||||
$('#wiaas_package_option_groups_list').append(response);
|
||||
});
|
||||
});
|
||||
|
||||
$('#wiaas_package_option_groups').delegate('.delete_wiaas_option_group', 'click', function (event) {
|
||||
event.preventDefault();
|
||||
if (confirm("<?php _e( 'Are you sure you would like to remove this option group?', 'wiaas' ); ?>")) {
|
||||
var id = $(this).data('id');
|
||||
$('#wiaas_option_group_' + id).slideUp().remove();
|
||||
}
|
||||
});
|
||||
|
||||
$('#wiaas_package_option_groups').delegate('.wiaas_option_group_name', 'change', function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
var name = $(this).val();
|
||||
var id = $(this).data('id');
|
||||
|
||||
$(`#wiaas_option_group_${id}_title`).text(name);
|
||||
|
||||
});
|
||||
|
||||
$('#wiaas_package_option_groups').delegate('.wiaas_option_group_default', 'change', function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
var id = $(this).data('id');
|
||||
|
||||
$(`:input.wiaas_option_group_default`).each(function() {
|
||||
var group_id = $(this).data('id');
|
||||
if (group_id !== id) {
|
||||
$(this).prop('checked', false);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
public static function process_meta_box($package_id) {
|
||||
|
||||
$package = wc_get_product($package_id);
|
||||
|
||||
$posted_option_groups = isset( $_POST['wiaas_option_groups'] ) ? $_POST['wiaas_option_groups'] : array();
|
||||
|
||||
error_log(print_r($posted_option_groups, true));
|
||||
|
||||
$option_groups = array();
|
||||
|
||||
foreach ($posted_option_groups as $id => $posted_option_group) {
|
||||
$option_group = array(
|
||||
'id' => $posted_option_group['id'],
|
||||
'name' => $posted_option_group['name'],
|
||||
'default' => $posted_option_group['default'],
|
||||
'options' => array()
|
||||
);
|
||||
$option_group['options'] = isset( $posted_option_group['options'] ) ?
|
||||
array_filter( array_map( 'intval', (array) $posted_option_group['options'] ) ) :
|
||||
array();
|
||||
|
||||
$option_groups[] = $option_group;
|
||||
}
|
||||
|
||||
Wiaas_Package_Option_Groups::set_package_option_groups(
|
||||
$package,
|
||||
$posted_option_groups);
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin_Package_Option_Groups::init();
|
||||
@@ -72,10 +72,9 @@ class Wiaas_Cart_API {
|
||||
$count = 0;
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
if (isset($item['bundled_by'])) {
|
||||
continue;
|
||||
if (isset($item['_wiaas_standard_package'])) {
|
||||
$count++;
|
||||
}
|
||||
$count++;
|
||||
}
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
@@ -94,17 +93,52 @@ class Wiaas_Cart_API {
|
||||
$result = array();
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
if (isset($item['bundled_by'])) {
|
||||
if (!isset($item['_wiaas_standard_package'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$package = wc_get_product($item['product_id']);
|
||||
|
||||
// Retrieve package addons
|
||||
$addon_cart_items = Wiaas_Package_Addon::get_cart_item_addons($item);
|
||||
$additional_packages = array();
|
||||
|
||||
foreach ($addon_cart_items as $addon_cart_item) {
|
||||
$additional_package = wc_get_product($addon_cart_item['product_id']);
|
||||
$additional_packages[] = array(
|
||||
'idAdditionalPackage' => $additional_package->get_id(),
|
||||
'packageName' => $additional_package->get_title(),
|
||||
'prices' => array(
|
||||
'fixedExtra' => $addon_cart_item['_wiaas_payment']['minimal_fixed_price'],
|
||||
'recurentExtra' => $addon_cart_item['_wiaas_payment']['recurrent_price'],
|
||||
'servicesExtra' => $addon_cart_item['_wiaas_payment']['minimal_services_price'],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Retrieve package options
|
||||
$option_cart_items = Wiaas_Package_Option_Groups::get_cart_item_options($item);
|
||||
$package_options = array();
|
||||
foreach ($option_cart_items as $option_cart_item) {
|
||||
$option_package = wc_get_product($option_cart_item['product_id']);
|
||||
$package_options[] = array(
|
||||
'idOptionPackage' => $option_package->get_id(),
|
||||
'groupName' => $option_package->get_title(),
|
||||
'packageName' => $option_package->get_title(),
|
||||
'prices' => array(
|
||||
'fixedExtra' => $item['_wiaas_payment']['minimal_fixed_price'],
|
||||
'recurentExtra' => $item['_wiaas_payment']['recurrent_price'],
|
||||
'servicesExtra' => $item['_wiaas_payment']['minimal_services_price'],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
$result[] = array(
|
||||
'idPackage' => $item['product_id'],
|
||||
'key' => $item['key'],
|
||||
'packageName' => $package->get_title(),
|
||||
'additionalPackages' => array(),
|
||||
'additionalPackages' => $additional_packages,
|
||||
'areAdditionalAvailable' => true,
|
||||
'areOptionsAvailable' => true,
|
||||
'bids' => array(),
|
||||
@@ -112,21 +146,21 @@ class Wiaas_Cart_API {
|
||||
'country' => array(
|
||||
'currency' => 'SEK'
|
||||
),
|
||||
'options' => array(),
|
||||
'options' => $package_options,
|
||||
'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'],
|
||||
'idPayType' => $item['_wiaas_payment']['id'],
|
||||
'payType' => $item['_wiaas_payment']['payment_type'],
|
||||
'periodUnit' => $item['_wiaas_payment']['period_unit'],
|
||||
'idPrice' => $item['_wiaas_payment']['id'],
|
||||
'fixedPrice' => $item['_wiaas_payment']['minimal_fixed_price'],
|
||||
'recurentPrice' => $item['_wiaas_payment']['recurrent_price'],
|
||||
'servicesPrice' => $item['_wiaas_payment']['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'],
|
||||
'fixedPrice' => $item['_wiaas_payment']['minimal_fixed_price'],
|
||||
'recurentPrice' => $item['_wiaas_payment']['recurrent_price'],
|
||||
'servicesPrice' => $item['_wiaas_payment']['minimal_services_price'],
|
||||
),
|
||||
|
||||
'status' => 'available',
|
||||
@@ -136,6 +170,7 @@ class Wiaas_Cart_API {
|
||||
return new WP_REST_Response(
|
||||
array(
|
||||
'cartItems' => $result,
|
||||
'items' => $items,
|
||||
'totalPrice' => ''
|
||||
)
|
||||
);
|
||||
@@ -162,22 +197,10 @@ class Wiaas_Cart_API {
|
||||
*/
|
||||
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,
|
||||
));
|
||||
$success = WC()->cart->add_to_cart($package_id, 1, 0, array(), array(
|
||||
'_wiaas_standard_package' => true
|
||||
));
|
||||
|
||||
if ($success) {
|
||||
return new WP_REST_Response(array(
|
||||
|
||||
@@ -8,6 +8,8 @@ class Wiaas_Admin {
|
||||
|
||||
public static function init() {
|
||||
require_once dirname( __FILE__ ) . '/admin/package/class-wiaas-admin-package-pricing.php';
|
||||
require_once dirname( __FILE__ ) . '/admin/package/class-wiaas-admin-package-addon.php';
|
||||
require_once dirname( __FILE__ ) . '/admin/package/class-wiaas-admin-package-option-groups.php';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ class Wiaas_Order {
|
||||
$data = $response->get_data();
|
||||
|
||||
# apply overrides
|
||||
$data = self::_append_products_info($data, $order, $request);
|
||||
$data = self::_append_packages($data, $order, $request);
|
||||
|
||||
$data = self::_append_order_process($data, $order, $request);
|
||||
|
||||
@@ -168,27 +168,26 @@ class Wiaas_Order {
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private static function _append_products_info($data, $order, $request) {
|
||||
private static function _append_packages($data, $order, $request) {
|
||||
$line_items = array();
|
||||
|
||||
$order_items = $order->get_items( 'line_item' );
|
||||
|
||||
foreach ($data['line_items'] as $index => $product_line) {
|
||||
|
||||
$item = $order->get_item($product_line['id']);
|
||||
|
||||
// add only product lines that represent product bundles
|
||||
if (empty($product_line['bundled_by'])) {
|
||||
if (isset($item['wiaas_standard_package'])) {
|
||||
|
||||
$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'] = $payment['payment_type'];
|
||||
|
||||
# lock all products to have no service
|
||||
$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'] = $payment['recurrent_price'];
|
||||
$product_line['pay_period'] = $payment['package_pay_period'];
|
||||
# get payment type info
|
||||
$product_line['payment_type'] = $item['wiaas_payment_type'];
|
||||
$product_line['service_price'] = floatval($item['wiaas_service_price']);
|
||||
$product_line['service_contract_period'] = floatval($item['wiaas_service_contract_period']);
|
||||
$product_line['max_contract_period'] = floatval($item['wiaas_max_contract_period']);
|
||||
$product_line['period_unit'] = $item['wiaas_period_unit'];
|
||||
$product_line['recurring_price'] = floatval($item['wiaas_recurring_price']);
|
||||
$product_line['pay_period'] = floatval($item['wiaas_pay_period']);
|
||||
|
||||
# collect status from order
|
||||
if ($data['status'] === 'completed') {
|
||||
@@ -198,11 +197,32 @@ class Wiaas_Order {
|
||||
} else {
|
||||
$product_line['status'] = 'processing';
|
||||
}
|
||||
|
||||
$product_line['short_desc'] = $product_line['status'];
|
||||
|
||||
# collect completion data from order
|
||||
$product_line['date_completed'] = $data['date_completed'];
|
||||
|
||||
// collect package addons
|
||||
$product_line['additional_packages'] = array();
|
||||
$addon_items = Wiaas_Package_Addon::get_order_item_addons($order_items, $item);
|
||||
foreach ($addon_items as $addon_item) {
|
||||
$product_line['additional_packages'][] = array(
|
||||
'id' => $addon_item->get_id(),
|
||||
'name' => $addon_item->get_name(),
|
||||
);
|
||||
}
|
||||
|
||||
// collect package options
|
||||
$product_line['options'] = array();
|
||||
$option_items = Wiaas_Package_Option_Groups::get_order_item_options($order_items, $item);
|
||||
foreach ($option_items as $option_item) {
|
||||
$product_line['options'][] = array(
|
||||
'id' => $option_item->get_id(),
|
||||
'name' => $option_item->get_name(),
|
||||
);
|
||||
}
|
||||
|
||||
$line_items[] = $product_line;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,27 @@ class Wiaas_Package {
|
||||
|
||||
public static function init() {
|
||||
require_once dirname( __FILE__ ) . '/package/class-wiaas-package-pricing.php';
|
||||
require_once dirname( __FILE__ ) . '/package/class-wiaas-package-addon.php';
|
||||
require_once dirname( __FILE__ ) . '/package/class-wiaas-package-option-groups.php';
|
||||
|
||||
add_action( 'woocommerce_checkout_create_order_line_item', array( __CLASS__, 'add_order_item_meta' ), 10, 3 );
|
||||
|
||||
add_filter('woocommerce_rest_prepare_product_object', array(__CLASS__, 'transform_rest_package'), 999, 3);
|
||||
|
||||
add_filter( 'woocommerce_hidden_order_itemmeta', array( __CLASS__, 'hidden_order_item_meta' ) );
|
||||
}
|
||||
|
||||
public static function add_order_item_meta( $order_item, $cart_item_key, $cart_item ){
|
||||
if (isset($cart_item['_wiaas_standard_package'])) {
|
||||
$order_item->add_meta_data( '_wiaas_standard_package', $cart_item['_wiaas_standard_package'], true );
|
||||
}
|
||||
}
|
||||
|
||||
public static function hidden_order_item_meta( $hidden ) {
|
||||
|
||||
return array_merge( $hidden, array(
|
||||
'_wiaas_standard_package',
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -21,11 +40,48 @@ class Wiaas_Package {
|
||||
|
||||
$data = self::_append_package_prices($data, $package, $request);
|
||||
|
||||
$data = self::_append_grouped_products($data, $package, $request);
|
||||
|
||||
$response->set_data($data);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
private static function _append_grouped_products($data, $package, $request) {
|
||||
|
||||
$data['additional_packages'] = array();
|
||||
$addons = Wiaas_Package_Addon::get_package_addons($package);
|
||||
foreach ($addons as $addon) {
|
||||
$data['additional_packages'][] = array(
|
||||
'id' => $addon->get_id(),
|
||||
'name' => $addon->get_name(),
|
||||
'description' => $addon->get_description(),
|
||||
'prices' => array_values(Wiaas_Package_Pricing::get_package_prices($addon))
|
||||
);
|
||||
}
|
||||
|
||||
$data['groups'] = array();
|
||||
$option_groups = Wiaas_Package_Option_Groups::get_package_option_groups($package);
|
||||
foreach ($option_groups as $option_group) {
|
||||
$data['groups'][$option_group['id']] = array(
|
||||
'id' => $option_group['id'],
|
||||
'name' => $option_group['name'],
|
||||
'options' => array()
|
||||
);
|
||||
foreach ($option_group['options'] as $option_package) {
|
||||
$data['groups'][$option_group['id']]['options'][] = array(
|
||||
'id' => $option_package->get_id(),
|
||||
'name' => $option_package->get_name(),
|
||||
'description' => $option_package->get_description(),
|
||||
'default' => 0,
|
||||
'prices' => array_values(Wiaas_Package_Pricing::get_package_prices($option_package))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append configured package prices on package json response
|
||||
* @param $data
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Package_Addon {
|
||||
|
||||
private static $package_addons_meta_key = '_wiaas_package_addons';
|
||||
|
||||
private static $cart_item_addon_items_key = '_wiaas_addon_items';
|
||||
|
||||
private static $cart_item_addon_parent_key = '_wiaas_addon_for';
|
||||
|
||||
public static function init() {
|
||||
|
||||
self::_add_addons_hooks();
|
||||
|
||||
add_action( 'woocommerce_checkout_create_order_line_item', array( __CLASS__, 'add_order_item_meta' ), 10, 3 );
|
||||
|
||||
add_filter( 'woocommerce_hidden_order_itemmeta', array( __CLASS__, 'hidden_order_item_meta' ) );
|
||||
}
|
||||
|
||||
private static function _add_addons_hooks() {
|
||||
// Add bundle-specific cart item data based on posted vars.
|
||||
add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ), 10, 2 );
|
||||
|
||||
// Add addon items to the cart.
|
||||
add_action( 'woocommerce_add_to_cart', array( __CLASS__, 'add_addons_to_cart' ), 10, 6 );
|
||||
}
|
||||
|
||||
private static function _remove_addons_hooks() {
|
||||
|
||||
remove_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ));
|
||||
|
||||
remove_action('woocommerce_add_to_cart', array( __CLASS__, 'add_addons_to_cart' ));
|
||||
}
|
||||
|
||||
public static function add_order_item_meta( $order_item, $cart_item_key, $cart_item ){
|
||||
if (isset($cart_item[self::$cart_item_addon_items_key])) {
|
||||
$order_item->add_meta_data( self::$cart_item_addon_items_key, $cart_item[self::$cart_item_addon_items_key] );
|
||||
}
|
||||
if (isset($cart_item[self::$cart_item_addon_parent_key])) {
|
||||
$order_item->add_meta_data( self::$cart_item_addon_parent_key, $cart_item[self::$cart_item_addon_parent_key], true );
|
||||
}
|
||||
}
|
||||
|
||||
public static function hidden_order_item_meta( $hidden ) {
|
||||
|
||||
return array_merge( $hidden, array(
|
||||
self::$cart_item_addon_items_key,
|
||||
self::$cart_item_addon_parent_key,
|
||||
) );
|
||||
}
|
||||
|
||||
public static function add_cart_item_data($cart_item_data, $package_id) {
|
||||
|
||||
// If cart item that will be added is actually requested package
|
||||
if ($_POST['package_id'] = $package_id) {
|
||||
// Prepare additional data for later use.
|
||||
if ( ! isset( $cart_item_data[self::$cart_item_addon_items_key] ) ) {
|
||||
$cart_item_data[self::$cart_item_addon_items_key ] = array();
|
||||
}
|
||||
}
|
||||
|
||||
return $cart_item_data;
|
||||
}
|
||||
|
||||
public static function add_addons_to_cart($cart_item_key, $package_id, $quantity, $variation_id, $variation, $cart_item_data) {
|
||||
|
||||
$is_addon_parent = $_POST['package_id'] = $package_id && isset($cart_item_data[self::$cart_item_addon_items_key]);
|
||||
$has_selected_addons = isset($_POST['addons']) && is_array($_POST['addons']);
|
||||
|
||||
self::_remove_addons_hooks();
|
||||
|
||||
if ($is_addon_parent && $has_selected_addons) {
|
||||
$addons_ids = $_POST['addons'];
|
||||
|
||||
foreach ($addons_ids as $addon_id) {
|
||||
$addon_package = wc_get_product($addon_id);
|
||||
if (is_object($addon_package)) {
|
||||
|
||||
$addon_cart_item_key = WC()->cart->add_to_cart($addon_id);
|
||||
|
||||
if ($addon_cart_item_key) {
|
||||
WC()->cart->cart_contents[$addon_cart_item_key][self::$cart_item_addon_parent_key] = $cart_item_key;
|
||||
|
||||
WC()->cart->cart_contents[ $cart_item_key ][self::$cart_item_addon_items_key][] = $addon_cart_item_key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self::_add_addons_hooks();
|
||||
}
|
||||
|
||||
public static function get_cart_item_addons($cart_item) {
|
||||
$addon_cart_items = array();
|
||||
|
||||
if (isset($cart_item[self::$cart_item_addon_items_key])) {
|
||||
$addon_cart_items_ids = $cart_item[self::$cart_item_addon_items_key];
|
||||
|
||||
foreach ($addon_cart_items_ids as $addon_cart_item_id) {
|
||||
$addon_cart_item = WC()->cart->get_cart_item($addon_cart_item_id);
|
||||
|
||||
if (isset($addon_cart_item)) {
|
||||
$addon_cart_items[] = $addon_cart_item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $addon_cart_items;
|
||||
}
|
||||
|
||||
public static function get_order_item_addons($order_items, $parent_order_item) {
|
||||
$addon_order_items = array();
|
||||
|
||||
if (isset($parent_order_item['wiaas_addon_items']) && isset($parent_order_item['bundle_cart_key'])) {
|
||||
foreach ($order_items as $order_item) {
|
||||
if (isset($order_item['bundle_cart_key']) && $order_item['wiaas_addon_for'] === $parent_order_item['bundle_cart_key']) {
|
||||
$addon_order_items[] = $order_item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $addon_order_items;
|
||||
}
|
||||
|
||||
public static function set_package_addons($package, $children_ids) {
|
||||
$package->update_meta_data( self::$package_addons_meta_key, $children_ids );
|
||||
$package->save_meta_data();
|
||||
}
|
||||
|
||||
public static function get_package_addons($package) {
|
||||
$addon_ids = $package->get_meta( self::$package_addons_meta_key );
|
||||
|
||||
$addons = array();
|
||||
|
||||
foreach ($addon_ids as $addon_id) {
|
||||
$addon_package = wc_get_product( $addon_id );
|
||||
if (is_object($addon_package)) {
|
||||
$addons[] = $addon_package;
|
||||
}
|
||||
}
|
||||
|
||||
return $addons;
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Package_Addon::init();
|
||||
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Package_Option_Groups {
|
||||
|
||||
private static $package_option_groups_meta_key = '_wiaas_package_option_groups';
|
||||
|
||||
private static $cart_item_option_items_key = '_wiaas_option_items';
|
||||
|
||||
private static $cart_item_option_parent_key = '_wiaas_option_for';
|
||||
|
||||
public static function init() {
|
||||
|
||||
self::_add_options_hooks();
|
||||
|
||||
add_action( 'woocommerce_checkout_create_order_line_item', array( __CLASS__, 'add_order_item_meta' ), 10, 3 );
|
||||
|
||||
add_filter( 'woocommerce_hidden_order_itemmeta', array( __CLASS__, 'hidden_order_item_meta' ) );
|
||||
}
|
||||
|
||||
private static function _add_options_hooks() {
|
||||
// Add bundle-specific cart item data based on posted vars.
|
||||
add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ), 10, 2 );
|
||||
|
||||
// Add option items to the cart.
|
||||
add_action( 'woocommerce_add_to_cart', array( __CLASS__, 'add_options_to_cart' ), 10, 6 );
|
||||
}
|
||||
|
||||
private static function _remove_options_hooks() {
|
||||
|
||||
remove_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ));
|
||||
|
||||
remove_action('woocommerce_add_to_cart', array( __CLASS__, 'add_options_to_cart' ));
|
||||
}
|
||||
|
||||
public static function add_order_item_meta( $order_item, $cart_item_key, $cart_item ){
|
||||
if (isset($cart_item[self::$cart_item_option_items_key])) {
|
||||
$order_item->add_meta_data( self::$cart_item_option_items_key, $cart_item[self::$cart_item_option_items_key] );
|
||||
}
|
||||
if (isset($cart_item[self::$cart_item_option_parent_key])) {
|
||||
$order_item->add_meta_data( self::$cart_item_option_parent_key, $cart_item[self::$cart_item_option_parent_key], true );
|
||||
}
|
||||
}
|
||||
|
||||
public static function hidden_order_item_meta( $hidden ) {
|
||||
|
||||
return array_merge( $hidden, array(
|
||||
self::$cart_item_option_items_key,
|
||||
self::$cart_item_option_parent_key,
|
||||
) );
|
||||
}
|
||||
|
||||
public static function set_package_option_groups($package, $groups_data) {
|
||||
$package->update_meta_data( self::$package_option_groups_meta_key, $groups_data );
|
||||
$package->save_meta_data();
|
||||
}
|
||||
|
||||
public static function add_cart_item_data($cart_item_data, $package_id) {
|
||||
|
||||
// If cart item that will be added is actually requested package
|
||||
if ($_POST['package_id'] = $package_id) {
|
||||
// Prepare additional data for later use.
|
||||
if ( ! isset( $cart_item_data[self::$cart_item_option_items_key] ) ) {
|
||||
$cart_item_data[self::$cart_item_option_items_key ] = array();
|
||||
}
|
||||
}
|
||||
|
||||
return $cart_item_data;
|
||||
}
|
||||
|
||||
public static function add_options_to_cart($cart_item_key, $package_id, $quantity, $variation_id, $variation, $cart_item_data) {
|
||||
|
||||
$is_option_parent = $_POST['package_id'] = $package_id && isset($cart_item_data[self::$cart_item_option_items_key]);
|
||||
$has_selected_options = isset($_POST['options']) && is_array($_POST['options']);
|
||||
|
||||
if ($is_option_parent && $has_selected_options) {
|
||||
|
||||
self::_remove_options_hooks();
|
||||
|
||||
$options_ids = $_POST['options'];
|
||||
|
||||
foreach ($options_ids as $option_id) {
|
||||
$option_package = wc_get_product($option_id);
|
||||
if (is_object($option_package)) {
|
||||
|
||||
$option_cart_item_key = WC()->cart->add_to_cart($option_id);
|
||||
|
||||
if ($option_cart_item_key) {
|
||||
WC()->cart->cart_contents[$option_cart_item_key][self::$cart_item_option_parent_key] = $cart_item_key;
|
||||
|
||||
WC()->cart->cart_contents[ $cart_item_key ][self::$cart_item_option_items_key][] = $option_cart_item_key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self::_add_options_hooks();
|
||||
}
|
||||
}
|
||||
|
||||
public static function get_cart_item_options($cart_item) {
|
||||
$option_cart_items = array();
|
||||
|
||||
if (isset($cart_item[self::$cart_item_option_items_key])) {
|
||||
$option_cart_items_ids = $cart_item[self::$cart_item_option_items_key];
|
||||
|
||||
foreach ($option_cart_items_ids as $option_cart_item_id) {
|
||||
$option_cart_item = WC()->cart->get_cart_item($option_cart_item_id);
|
||||
|
||||
if (isset($option_cart_item)) {
|
||||
$option_cart_items[] = $option_cart_item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $option_cart_items;
|
||||
}
|
||||
|
||||
public static function get_order_item_options($order_items, $parent_order_item) {
|
||||
$option_order_items = array();
|
||||
|
||||
if (isset($parent_order_item['wiaas_option_items']) && isset($parent_order_item['bundle_cart_key'])) {
|
||||
foreach ($order_items as $order_item) {
|
||||
if (isset($order_item['bundle_cart_key']) && $order_item['wiaas_option_for'] === $parent_order_item['bundle_cart_key']) {
|
||||
$option_order_items[] = $order_item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $option_order_items;
|
||||
}
|
||||
|
||||
public static function get_package_option_groups($package) {
|
||||
|
||||
$groups_data = $package->get_meta( self::$package_option_groups_meta_key );
|
||||
|
||||
$option_groups = array();
|
||||
|
||||
foreach ($groups_data as $group_data) {
|
||||
$group = array(
|
||||
'id' => $group_data['id'],
|
||||
'name' => $group_data['name'],
|
||||
'default' => $group_data['default'],
|
||||
'options' => array()
|
||||
);
|
||||
|
||||
foreach ($group_data['options'] as $option_id) {
|
||||
$option_package = wc_get_product( $option_id );
|
||||
if (is_object($option_package)) {
|
||||
$group['options'][] = $option_package;
|
||||
}
|
||||
}
|
||||
$option_groups[] = $group;
|
||||
}
|
||||
|
||||
return $option_groups;
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Package_Option_Groups::init();
|
||||
@@ -51,10 +51,33 @@ class Wiaas_Package_Pricing {
|
||||
private static $package_prices_meta_key = '_wiaas_pricing_rules';
|
||||
|
||||
public static function init() {
|
||||
add_action( 'woocommerce_checkout_create_order_line_item', array( __CLASS__, 'add_order_item_meta' ), 10, 3 );
|
||||
|
||||
add_filter( 'woocommerce_product_get_price', array( __CLASS__, 'on_get_price' ), 10, 2 );
|
||||
add_filter( 'woocommerce_hidden_order_itemmeta', array( __CLASS__, 'hidden_order_item_meta' ) );
|
||||
|
||||
add_filter('woocommerce_checkout_create_order_line_item_object', array( __CLASS__, 'record_price_for_order_line_item' ), 10, 4);
|
||||
add_filter( 'woocommerce_add_cart_item_data', array( __CLASS__, 'add_cart_item_data' ), 10, 2 );
|
||||
|
||||
add_action( 'woocommerce_before_calculate_totals', array( __CLASS__, 'on_calculate_totals' ), 99, 1);
|
||||
|
||||
add_action( 'woocommerce_cart_loaded_from_session', array( __CLASS__, 'on_calculate_totals' ), 99, 1);
|
||||
}
|
||||
|
||||
public static function add_cart_item_data($cart_item_data, $package_id) {
|
||||
|
||||
if ( isset( $_POST[ 'price_id' ]) &&
|
||||
WC_Product_Factory::get_product_type( $package_id ) === 'bundle') {
|
||||
$selected_price_id = $_POST['price_id'];
|
||||
$package = wc_get_product( $package_id );
|
||||
|
||||
$configured_package_prices = self::get_package_prices($package);
|
||||
|
||||
$selected_price = $configured_package_prices[$selected_price_id];
|
||||
|
||||
if (isset($selected_price)) {
|
||||
$cart_item_data['_wiaas_payment'] = $selected_price;
|
||||
}
|
||||
}
|
||||
return $cart_item_data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,30 +97,24 @@ class Wiaas_Package_Pricing {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve customer selected package price from session when adding package to cart
|
||||
* @param $base_price
|
||||
* @param $product
|
||||
*
|
||||
* @return mixed
|
||||
* Update package cart item with `minimal_fixed_price` as its price
|
||||
* so resulting totals would be sum of these prices
|
||||
* @param $cart
|
||||
*/
|
||||
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;
|
||||
public static function on_calculate_totals($cart) {
|
||||
|
||||
foreach ($cart->cart_contents as $key => $cart_item) {
|
||||
if (isset($cart_item['_wiaas_payment'])) {
|
||||
|
||||
$price = $cart_item['_wiaas_payment'];
|
||||
|
||||
WC()->cart->cart_contents[ $key ]['data']->set_price( $price['minimal_fixed_price'] );
|
||||
}
|
||||
|
||||
if (isset($cart_item['bundled_by'])) {
|
||||
WC()->cart->cart_contents[ $key ]['data']->set_price( 0 );
|
||||
}
|
||||
}
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,12 +126,32 @@ class Wiaas_Package_Pricing {
|
||||
*
|
||||
* @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']);
|
||||
}
|
||||
public static function add_order_item_meta( $order_item, $cart_item_key, $cart_item ) {
|
||||
if (wc_pb_is_bundle_container_cart_item($cart_item) && isset($cart_item['_wiaas_payment'])) {
|
||||
|
||||
return $order_item;
|
||||
$payment = $cart_item['_wiaas_payment'];
|
||||
|
||||
$order_item->add_meta_data( '_wiaas_payment_type', $payment['payment_type'], true );
|
||||
$order_item->add_meta_data( '_wiaas_service_price', $payment['minimal_services_price'], true );
|
||||
$order_item->add_meta_data( '_wiaas_service_contract_period', $payment['services_contract_period'], true );
|
||||
$order_item->add_meta_data( '_wiaas_max_contract_period', $payment['max_contract_period'], true );
|
||||
$order_item->add_meta_data( '_wiaas_period_unit', $payment['period_unit'], true );
|
||||
$order_item->add_meta_data( '_wiaas_recurring_price', $payment['recurrent_price'], true );
|
||||
$order_item->add_meta_data( '_wiaas_pay_period', $payment['package_pay_period'], true );
|
||||
}
|
||||
}
|
||||
|
||||
public static function hidden_order_item_meta( $hidden ) {
|
||||
|
||||
return array_merge( $hidden, array(
|
||||
'_wiaas_payment_type',
|
||||
'_wiaas_service_price',
|
||||
'_wiaas_service_contract_period',
|
||||
'_wiaas_max_contract_period',
|
||||
'_wiaas_period_unit',
|
||||
'_wiaas_recurring_price',
|
||||
'_wiaas_pay_period'
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user