diff --git a/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-selection.js b/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-selection.js new file mode 100644 index 0000000..1ad4aac --- /dev/null +++ b/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-selection.js @@ -0,0 +1,51 @@ +jQuery(document).ready(function ($) { + var $selectTemplate = $('#_select_template'); + var $container = $('#wiaas_selected_template_items_container'); + + $selectTemplate + + .on('change', function () { + var selectedTemplate = $selectTemplate.val(); + + var data = { + action: 'wiaas_select_template', + selected_template: selectedTemplate + }; + + + setTimeout(function () { + + $.post(woocommerce_admin_meta_boxes.ajax_url, data, function (response) { + + if ('' !== response.markup) { + + $container.append(response.markup); + + } else { + $container.children("div").remove(); + } + + if (response.message !== '') { + window.alert(response.message); + } + + + }); + + }, 250); + }); + + var $product_type = $('#product-type'); + var $template_product_meta_box = $('#template_product_meta_box'); + $product_type.on('change', function () { + var selectedProductType = $product_type.val(); + + if (selectedProductType === 'bundle') { + $template_product_meta_box.show(); + } else { + $template_product_meta_box.hide(); + } + }); + + +}); \ No newline at end of file diff --git a/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js b/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js new file mode 100644 index 0000000..8054a6e --- /dev/null +++ b/backend/app/plugins/wiaas/assets/js/wiaas-template-admin-write-panels.js @@ -0,0 +1,247 @@ +jQuery(function ($) { + + var $edit_in_cart = $('.bundle_edit_in_cart'), + $group_mode_select = $('select#_wc_pb_group_mode'), + $bundled_products_panel = $('#bundled_product_data'), + $bundled_products_toolbar = $bundled_products_panel.find('.toolbar'), + $template_products_container_hardware = $('.wiaas-template-items_hardware'), + $template_products_container_software = $('.wiaas-template-items_software'), + $template_products_container_services = $('.wiaas-template-items_services'), + $template_products_container_isntallation = $('.wiaas-template-items_installation'), + + $template_products = $('.wc-bundled-item', + $template_products_container_services, + $template_products_container_software, + $template_products_container_hardware, + $template_products_container_isntallation), + + $template_search_hardware = $('#wiaastemplate_products_hardware'), + $template_search_software = $('#wiaastemplate_products_software'), + $template_search_services = $('#wiaastemplate_products_services'), + $template_search_installation = $('#wiaastemplate_products_installation'), + bundled_product_objects = {}, + bundled_products_add_count = $template_products.length, + block_params = { + message: null, + overlayCSS: { + background: '#fff', + opacity: 0.6 + } + }; + + $.fn.wc_bundles_select2 = function () { + $(document.body).trigger('wc-enhanced-select-init'); + }; + + // Bundle type move stock msg up. + $('.bundle_stock_msg').appendTo('._manage_stock_field .description'); + + // Simple type options are valid for bundles. + $('.show_if_simple:not(.hide_if_bundle)').addClass('show_if_bundle'); + + $('body').on('woocommerce-product-type-change', function (event, select_val) { + + if ('bundle' === select_val) { + + $('.show_if_external').hide(); + $('.show_if_bundle').show(); + + $('input#_manage_stock').change(); + + $('#_nyp').change(); + } + + }); + + $group_mode_select.change(function () { + if ($.inArray($group_mode_select.val(), wc_bundles_admin_params.group_modes_with_parent) === -1) { + $edit_in_cart.hide(); + } else { + $edit_in_cart.show(); + } + }); + + // Downloadable support. + $('input#_downloadable').change(function () { + $('select#product-type').change(); + }); + + // Trigger product type change. + $('select#product-type').change(); + + // Trigger group mode change. + $group_mode_select.change(); + + + init_event_handlers(); + + function init_event_handlers() { + + + $template_search_hardware + + .on('change', function () { + addSearchedProduct('hardware', $template_search_hardware, $template_products_container_hardware) + }); + + $template_search_software + + .on('change', function () { + addSearchedProduct('software', $template_search_software, $template_products_container_software) + }); + + + $template_search_installation + + .on('change', function () { + addSearchedProduct('services', $template_search_installation, $template_products_container_isntallation) + }); + + + $template_search_services + + .on('change', function () { + addSearchedProduct('installation', $template_search_services, $template_products_container_services) + }); + + + function addSearchedProduct(options, search, container) { + var bundled_product_ids = search.val(), + bundled_product_id = bundled_product_ids && bundled_product_ids.length > 0 ? bundled_product_ids.shift() : false; + + if (!bundled_product_id) { + return false; + } + + search.val([]).change(); + + $bundled_products_panel.block(block_params); + + bundled_products_add_count++; + + var data = { + action: 'wiaas_add_template_product', + post_id: woocommerce_admin_meta_boxes.post_id, + id: bundled_products_add_count, + product_id: bundled_product_id, + security: wc_bundles_admin_params.add_bundled_product_nonce, + options: options, + }; + + setTimeout(function () { + + $.post(woocommerce_admin_meta_boxes.ajax_url, data, function (response) { + + if ('' !== response.markup) { + + container.append(response.markup); + + var $added = $('.wiaas-template-item', container).last(), + added_id = 'bundled_item_' + bundled_products_add_count; + + $added.data('bundled_item_id', added_id); + $added.wc_bundles_select2(); + + $bundled_products_panel.trigger('wc-bundles-added-bundled-product'); + + } else if (response.message !== '') { + window.alert(response.message); + } + + // Open and close to resolve "sticky" modal issue. + if ('yes' === wc_bundles_admin_params.is_wc_version_gte_3_2) { + search.selectWoo('open'); + search.selectWoo('close'); + } else { + search.select2('open'); + search.select2('close'); + } + + $bundled_products_panel.unblock(); + + }); + + }, 250); + + return false; + } + + $template_products_container_isntallation + + // Remove Item. + .on('click', 'a.remove_row', function (e) { + + var $el = $(this).closest('.wc-bundled-item'), + el_id = $el.data('bundled_item_id'); + + $el.find('*').off(); + $el.remove(); + + delete bundled_product_objects[el_id]; + + $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + + e.preventDefault(); + + }); + + $template_products_container_hardware + + // Remove Item. + .on('click', 'a.remove_row', function (e) { + + var $el = $(this).closest('.wc-bundled-item'), + el_id = $el.data('bundled_item_id'); + + $el.find('*').off(); + $el.remove(); + + delete bundled_product_objects[el_id]; + + $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + + e.preventDefault(); + + }); + + $template_products_container_software + + // Remove Item. + .on('click', 'a.remove_row', function (e) { + + var $el = $(this).closest('.wc-bundled-item'), + el_id = $el.data('bundled_item_id'); + + $el.find('*').off(); + $el.remove(); + + delete bundled_product_objects[el_id]; + + $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + + e.preventDefault(); + + }); + + $template_products_container_services + + // Remove Item. + .on('click', 'a.remove_row', function (e) { + + var $el = $(this).closest('.wc-bundled-item'), + el_id = $el.data('bundled_item_id'); + + $el.find('*').off(); + $el.remove(); + + delete bundled_product_objects[el_id]; + + $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + + e.preventDefault(); + + }) + + } +}); + diff --git a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php new file mode 100644 index 0000000..828768e --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-admin-template-selection.php @@ -0,0 +1,247 @@ +(optional)', 'cmb'), + 'Wiaas_Admin_Template_Selection::add_custom_content_meta_box', + 'product', + 'normal', + 'high' + ); + } + + + public static function add_custom_content_meta_box($post) { + + $value = get_post_meta($post->ID, '_select_template', true); + + if (empty($value)) { + $value = ''; + } + + $products = wc_get_products(array( + 'type' => 'wiaastemplate' + )); + $product_ids[''] = __('Select a template', 'wiaas'); + foreach ($products as $prod) { + $product_ids[$prod->id] = $prod->name; + } + woocommerce_wp_select(array( + 'id' => '_select_template', + 'options' => $product_ids, + 'value' => $value + )); + + ?> +
$template_products_hardware, + 'service' => $template_products_service, + 'installation' => $template_products_installation, + 'software' => $template_products_software + ); + + } + + public static function render_template_products($template_products) { + + if (!empty($template_products)) { + + foreach ($template_products as $item) { + + $product_id = $item['product_id']; + $title = $item['product_title']; + $quantity = $item['quantity']; + + include('views/html-wiaas-template-selection.php'); + + } + } + } + + public static function get_product_categories($products) { + $category_ids = array(); + + if (empty($products)) { + return $category_ids; + } + + foreach ($products as $product) { + + $product_id = $product['product_id']; + + $post_terms = wp_get_object_terms($product_id, 'product_cat', array('fields' => 'id=>name')); + + if (!empty($post_terms) && !is_wp_error($post_terms)) { + + $cat = ''; + + foreach ($post_terms as $term) { + if ($term != "Uncategorized") { + $cat = $term; + } + } + + $product_with_cat = array( + 'quantity' => $product['quantity'], + 'category_term' => $cat + ); + + $category_ids[$product_id] = $product_with_cat; + + + } + } + return $category_ids; + } + + public static function get_bundled_product_categories($bundled_items) { + + $category_ids = array(); + foreach ($bundled_items as $item_id => $item) { + $item_data = $item->get_data(); + $post_terms = wp_get_object_terms($item->get_product_id(), 'product_cat', array('fields' => 'id=>name')); + + if (!empty($post_terms) && !is_wp_error($post_terms)) { + + $cat = ''; + + foreach ($post_terms as $term) { + if ($term != "Uncategorized") { + $cat = $term; + } + } + + $category_ids[$cat] = $item_data['quantity_max']; + + } + } + + return $category_ids; + } + + + function save_custom_content_meta_box($post_id) { + $selected_template = $_POST['_select_template']; + if (!empty($selected_template)) + update_post_meta($post_id, '_select_template', esc_attr($selected_template)); + else { + update_post_meta($post_id, '_select_template', ''); + } + + } + + public static function validate_bundle($product) { + + + if ($product->get_type() == 'bundle') { + $selected_template = $_POST['_select_template']; + $bundled_items = $product->get_bundled_items('view'); + $products_form_template = self::show_template_products($selected_template); + + $missing_categories = array(); + $insufficient_product_quantities = array(); + + + $categories_form_template = self::get_categories_from_templates($products_form_template); + $categories_form_bundle = self::get_bundled_product_categories($bundled_items); + $bundle_category_keys = array_keys($categories_form_bundle); + + + if (!empty($categories_form_template)) { + + foreach ($categories_form_template as $category) { + + if (!in_array($category['category_term'], $bundle_category_keys)) { + + array_push($missing_categories, $category['category_term']); + + } else { + + $product_from_bundle_quantity = $categories_form_bundle[$category['category_term']]; + + if ($category['quantity'] != $product_from_bundle_quantity) { + array_push($insufficient_product_quantities, $category['category_term']); + } + } + } + } + + $categories_meesage = implode(',', $missing_categories); + $quantity_message = implode(',', $insufficient_product_quantities); + + + if (!empty($missing_categories)) { + WC_PB_Meta_Box_Product_Data::add_admin_error(__(' WIAAS This product bundle does not correspond to selected template Categories missing: ' . $categories_meesage, 'woocommerce-product-bundles')); + } + + if (!empty($insufficient_product_quantities)) { + WC_PB_Meta_Box_Product_Data::add_admin_error(__(' WIAAS This product bundle does not correspond to selected template Categories with different quantities: ' . $quantity_message, 'woocommerce-product-bundles')); + } + } + + } + + public + static function get_category_ids_form_bundle($product) { + $products_form_budnle = $product->get_bundled_data_items(); + $product_ids = array(); + + foreach ($products_form_budnle as $product) { + array_push($product_ids, $product->get_id()); + } + + return self::get_product_categories($product_ids); + + } +} + +Wiaas_Admin_Template_Selection::init(); diff --git a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php new file mode 100644 index 0000000..51f4b98 --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-admin-ajax.php @@ -0,0 +1,82 @@ + '', + 'message' => '' + ); + + $template_products = Wiaas_Admin_Template_Selection::show_template_products($selected_template_id); + + if (!empty($template_products)) { + ob_start(); + Wiaas_Admin_Template_Selection::render_template_products($template_products['hardware']); + Wiaas_Admin_Template_Selection::render_template_products($template_products['service']); + Wiaas_Admin_Template_Selection::render_template_products($template_products['installation']); + Wiaas_Admin_Template_Selection::render_template_products($template_products['software']); + + $response['markup'] = ob_get_clean(); + } + + wp_send_json($response); + } + + + /** + * Handles adding products to template via ajax. + */ + public static function ajax_add_wiaas_template_product() { + +// check_ajax_referer( 'wc_bundles_add_bundled_product', 'security' ); + + $loop = intval($_POST['id']); + $product_id = intval($_POST['product_id']); + $options = $_POST['options']; + $product = wc_get_product($product_id); + $title = $product->get_title(); + + + $response = array( + 'markup' => '', + 'message' => '' + ); + + if ($product) { + + ob_start(); + include('views/html-wiaas-template-product.php'); + $response['markup'] = ob_get_clean(); + } else { + $response['message'] = __('The selected product is invalid.', 'woocommerce-product-bundles'); + } + + wp_send_json($response); + } +} + +Wiaas_Template_Admin_Ajax::init(); diff --git a/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php new file mode 100644 index 0000000..18e7ff6 --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/template/class-wiaas-template-products.php @@ -0,0 +1,233 @@ + + __('Hardware', 'wiaastemplate_hardware'), + 'target' => 'wiaastemplate_options_hardware', + 'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'), + ); + + $tabs['wiaastemplate_installation'] = array( + 'label' => __('Installation', 'wiaastemplate_isntallation'), + 'target' => 'wiaastemplate_options_installation', + 'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'), + ); + + $tabs['wiaastemplate_services'] = array( + 'label' => __('Services', 'wiaastemplate_services'), + 'target' => 'wiaastemplate_options_services', + 'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'), + ); + + $tabs['wiaastemplate_software'] = array( + 'label' => __('Software', 'wiaastemplate_software'), + 'target' => 'wiaastemplate_options_software', + 'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'), + ); + + return $tabs; + + } + + public static function wiaastemplate_product_tab_content_software() { + + self::wiaastemplate_product_tab_content('hardware'); + self::wiaastemplate_product_tab_content('installation'); + self::wiaastemplate_product_tab_content('services'); + self::wiaastemplate_product_tab_content('software'); + } + + + /** + * Contents of the template options product tab. + */ + public static function wiaastemplate_product_tab_content($options) { + + global $post; + $template_items = get_post_meta($post->ID, '_template_items_' . $options, true); + + ?> +