242 lines
9.8 KiB
PHP
242 lines
9.8 KiB
PHP
<?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();
|