From 21d96a0e55e902af5c634c590f856ddb79f1e25b Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Wed, 26 Sep 2018 13:55:31 +0200 Subject: [PATCH 01/10] Removed uneccesary code from wiaas admin panels.js --- .../js/wiaas-template-admin-write-panels.js | 129 +++++------------- 1 file changed, 31 insertions(+), 98 deletions(-) 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 index c12c4c2..dc22537 100644 --- 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 @@ -1,76 +1,23 @@ 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'), + var $template_categories_container_hardware = $('.wiaas-template-items_hardware'), + $template_categories_container_software = $('.wiaas-template-items_software'), + $template_categories_container_services = $('.wiaas-template-items_services'), + $template_categories_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_categories = $('.wc-bundled-item', + $template_categories_container_services, + $template_categories_container_software, + $template_categories_container_hardware, + $template_categories_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 - } - }; + template_category_objects = {}, + template_category_add_count = $template_categories.length; - $.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(); @@ -81,52 +28,50 @@ jQuery(function ($) { $template_search_hardware .on('change', function () { - addSearchedProduct('hardware', $template_search_hardware, $template_products_container_hardware) + addSearchedTemplateCategory('hardware', $template_search_hardware, $template_categories_container_hardware) }); $template_search_software .on('change', function () { - addSearchedProduct('software', $template_search_software, $template_products_container_software) + addSearchedTemplateCategory('software', $template_search_software, $template_categories_container_software) }); $template_search_installation .on('change', function () { - addSearchedProduct('services', $template_search_installation, $template_products_container_isntallation) + addSearchedTemplateCategory('services', $template_search_installation, $template_categories_container_isntallation) }); $template_search_services .on('change', function () { - addSearchedProduct('installation', $template_search_services, $template_products_container_services) + addSearchedTemplateCategory('installation', $template_search_services, $template_categories_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; + function addSearchedTemplateCategory(options, search, container) { + var template_category_ids = search.val(), + template_category_id = template_category_ids && template_category_ids.length > 0 ? template_category_ids.shift() : false; var template_category_title =search.text(); - if (!bundled_product_id) { + if (!template_category_id) { return false; } search.val([]).change(); - $bundled_products_panel.block(block_params); - - bundled_products_add_count++; + template_category_add_count++; var data = { action: 'wiaas_add_template_product', post_id: woocommerce_admin_meta_boxes.post_id, - id: bundled_products_add_count, + id: template_category_add_count, title: template_category_title, - product_id: bundled_product_id, + product_id: template_category_id, security: wc_bundles_admin_params.add_bundled_product_nonce, options: options, }; @@ -140,13 +85,11 @@ jQuery(function ($) { container.append(response.markup); var $added = $('.wiaas-template-item', container).last(), - added_id = 'bundled_item_' + bundled_products_add_count; + added_id = 'bundled_item_' + template_category_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); } @@ -161,8 +104,6 @@ jQuery(function ($) { } search.text(''); - $bundled_products_panel.unblock(); - }); }, 250); @@ -170,7 +111,7 @@ jQuery(function ($) { return false; } - $template_products_container_isntallation + $template_categories_container_isntallation // Remove Item. .on('click', 'a.remove_row', function (e) { @@ -181,15 +122,13 @@ jQuery(function ($) { $el.find('*').off(); $el.remove(); - delete bundled_product_objects[el_id]; - - $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + delete template_category_objects[el_id]; e.preventDefault(); }); - $template_products_container_hardware + $template_categories_container_hardware // Remove Item. .on('click', 'a.remove_row', function (e) { @@ -200,15 +139,13 @@ jQuery(function ($) { $el.find('*').off(); $el.remove(); - delete bundled_product_objects[el_id]; - - $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + delete template_category_objects[el_id]; e.preventDefault(); }); - $template_products_container_software + $template_categories_container_software // Remove Item. .on('click', 'a.remove_row', function (e) { @@ -219,15 +156,13 @@ jQuery(function ($) { $el.find('*').off(); $el.remove(); - delete bundled_product_objects[el_id]; - - $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + delete template_category_objects[el_id]; e.preventDefault(); }); - $template_products_container_services + $template_categories_container_services // Remove Item. .on('click', 'a.remove_row', function (e) { @@ -238,9 +173,7 @@ jQuery(function ($) { $el.find('*').off(); $el.remove(); - delete bundled_product_objects[el_id]; - - $bundled_products_panel.triggerHandler('wc-bundled-products-changed'); + delete template_category_objects[el_id]; e.preventDefault(); -- 2.47.3 From 93d03a96eea9c6529f04227b0baf7f91fac29fd8 Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Thu, 27 Sep 2018 15:08:21 +0200 Subject: [PATCH 02/10] Refactored template product feature --- .../class-wiaas-admin-template-selection.php | 97 +++++++------------ .../class-wiaas-template-admin-ajax.php | 4 +- .../class-wiaas-template-products.php | 37 ++----- .../class-wiaas-wc-product-template.php | 74 ++++++++++++++ 4 files changed, 119 insertions(+), 93 deletions(-) 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 index e1012a6..8e5fa1b 100644 --- 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 @@ -3,19 +3,19 @@ class Wiaas_Admin_Template_Selection { public static function init() { - add_action('add_meta_boxes', array(__CLASS__, 'create_custom_meta_box'), 100); - add_action('woocommerce_process_product_meta_bundle', array(__CLASS__, 'save_custom_content_meta_box'), 10, 1); + add_action('add_meta_boxes', array(__CLASS__, 'create_template_meta_box'), 100); + add_action('woocommerce_process_product_meta_bundle', array(__CLASS__, 'save_template_content_meta_box'), 10, 1); // Processes and saves type-specific data. add_action('woocommerce_admin_process_product_object', array(__CLASS__, 'validate_bundle'), 11); } - public static function create_custom_meta_box() { + public static function create_template_meta_box() { add_meta_box( 'template_product_meta_box', __('Choose package template (optional)', 'cmb'), - 'Wiaas_Admin_Template_Selection::add_custom_content_meta_box', + 'Wiaas_Admin_Template_Selection::add_template_content_meta_box', 'product', 'normal', 'high' @@ -23,7 +23,7 @@ class Wiaas_Admin_Template_Selection { } - public static function add_custom_content_meta_box($post) { + public static function add_template_content_meta_box($post) { $value = get_post_meta($post->ID, '_select_template', true); @@ -60,10 +60,17 @@ class Wiaas_Admin_Template_Selection { $all_template_categories = array(); - $all_template_categories = array_merge($all_template_categories, self::get_product_categories($products_form_template['hardware'])); - $all_template_categories = array_merge($all_template_categories, self::get_product_categories($products_form_template['service'])); - $all_template_categories = array_merge($all_template_categories, self::get_product_categories($products_form_template['installation'])); - $all_template_categories = array_merge($all_template_categories, self::get_product_categories($products_form_template['software'])); + $all_template_categories = array_merge($all_template_categories, + WC_Product_Template::extract_template_product_categories($products_form_template['hardware'])); + + $all_template_categories = array_merge($all_template_categories, + WC_Product_Template::extract_template_product_categories($products_form_template['service'])); + + $all_template_categories = array_merge($all_template_categories, + WC_Product_Template::extract_template_product_categories($products_form_template['installation'])); + + $all_template_categories = array_merge($all_template_categories, + WC_Product_Template::extract_template_product_categories($products_form_template['software'])); return $all_template_categories; @@ -76,10 +83,18 @@ class Wiaas_Admin_Template_Selection { return; } else { - $template_products_hardware = (get_post_meta($selected_template, '_template_items_hardware', true)); - $template_products_service = (get_post_meta($selected_template, '_template_items_service', true)); - $template_products_installation = (get_post_meta($selected_template, '_template_items_installation', true)); - $template_products_software = (get_post_meta($selected_template, '_template_items_software', true)); + + $template_products_hardware = WC_Product_Template::get_template_categories_from_meta( + $selected_template, '_template_items_hardware'); + + $template_products_service = WC_Product_Template::get_template_categories_from_meta( + $selected_template, '_template_items_service'); + + $template_products_installation = WC_Product_Template::get_template_categories_from_meta( + $selected_template, '_template_items_installation'); + + $template_products_software = WC_Product_Template::get_template_categories_from_meta( + $selected_template, '_template_items_software'); } @@ -108,33 +123,6 @@ class Wiaas_Admin_Template_Selection { } } - public static function get_product_categories($category_meta) { - $category_objects = array(); - - - if (empty($category_meta)) { - return array(); - } - - foreach ($category_meta as $meta) { - - if (!empty($meta)) { - - - $category_template_id = $meta['template_category_id']; - - $template_category_object = new Wiaas_Template_Category_Object( - $category_template_id, - $meta['template_category_title'], - $meta['quantity']); - - $category_objects[$category_template_id] = $template_category_object; - } - - } - return empty($category_objects) ? null : $category_objects; - } - public static function get_bundled_product_categories($bundled_items) { $template_category_objects = array(); @@ -165,13 +153,10 @@ class Wiaas_Admin_Template_Selection { } - function save_custom_content_meta_box($post_id) { + function save_template_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', ''); - } + + WC_Product_Template::bind_selected_template_to_product($selected_template, $post_id); } @@ -181,17 +166,15 @@ class Wiaas_Admin_Template_Selection { 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); + $template_categories_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); + $categories_form_template = self::get_categories_from_templates($template_categories_form_template); + $categories_form_bundle = WC_Product_Template::extract_bundled_product_categories($bundled_items); $bundle_category_keys = array_keys($categories_form_bundle); - if (!empty($categories_form_template)) { foreach ($categories_form_template as $category) { @@ -227,18 +210,6 @@ class Wiaas_Admin_Template_Selection { } } - - 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 index 656f0ef..842121f 100644 --- 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 @@ -74,9 +74,11 @@ class Wiaas_Template_Admin_Ajax { */ public static function ajax_wiaas_template_category_search() { - $term = ($_POST['term']); + $term = wp_unslash($_GET['term']); $response = array(); + error_log('This is term'); + error_log($term); $terms = get_terms(array( 'taxonomy' => 'template_category', 'name__like' => $term 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 index ddb5e54..90aff5d 100644 --- 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 @@ -7,7 +7,6 @@ class Wiaas_template { public static function init() { add_filter('product_type_selector', array(__CLASS__, 'add_template_product_type')); - add_action('admin_footer', array(__CLASS__, 'templates_custom_js')); add_filter('woocommerce_product_data_tabs', array(__CLASS__, 'custom_product_tabs')); add_action('woocommerce_product_data_panels', array(__CLASS__, 'wiaastemplate_product_tab_content_all')); add_action('woocommerce_admin_process_product_object', array(__CLASS__, 'save_wiaastemplate')); @@ -47,26 +46,6 @@ class Wiaas_template { } - /** - * Show pricing fields for template product. - */ - function templates_custom_js() { - - if ('product' != get_post_type()) : - return; - endif; - - ?> - " style="width: 250px;" name="template_product" data-placeholder="" - data-action="woocommerce_json_search_products" multiple="multiple" data-limit="500"> + multiple="multiple" data-limit="500"> @@ -163,14 +142,14 @@ class Wiaas_template { $posted_template_data_installation = isset($_POST['template_data_installation']) ? $_POST['template_data_installation'] : false; $posted_template_data_software = isset($_POST['template_data_software']) ? $_POST['template_data_software'] : false; - self::save_template_product_meta($post_id, $posted_template_data_hardware, 'hardware'); - self::save_template_product_meta($post_id, $posted_template_data_services, 'services'); - self::save_template_product_meta($post_id, $posted_template_data_installation, 'installation'); - self::save_template_product_meta($post_id, $posted_template_data_software, 'software'); + self::process_and_save_template_product_meta($post_id, $posted_template_data_hardware, 'hardware'); + self::process_and_save_template_product_meta($post_id, $posted_template_data_services, 'services'); + self::process_and_save_template_product_meta($post_id, $posted_template_data_installation, 'installation'); + self::process_and_save_template_product_meta($post_id, $posted_template_data_software, 'software'); } - public static function save_template_product_meta($post_id, $posted_template_data, $option) { + public static function process_and_save_template_product_meta($post_id, $posted_template_data, $option) { $processed_template_data = array(); // Sort posted data by menu order. @@ -186,7 +165,7 @@ class Wiaas_template { $item_data = array( 'template_category_id' => $product_id, - 'template_category_title' => trim($product_title), + 'template_category_title' => trim($product_title), 'quantity' => $quantity ); @@ -195,7 +174,7 @@ class Wiaas_template { } - update_post_meta($post_id, '_template_items_' . $option, $processed_template_data); + WC_Product_Template::save_template_product_meta($post_id, $option, $processed_template_data); } public static function menu_sort_order($a, $b) { diff --git a/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php b/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php index 5080c6d..7ab999d 100644 --- a/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php +++ b/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php @@ -27,7 +27,81 @@ function construct_template_products_class() { return 'wiaastemplate'; } + public function get_template_categories_from_meta($template_id, $template_category_slug) { + return get_post_meta($template_id, $template_category_slug, true); + + } + + public static function extract_template_product_categories($category_meta) { + $category_objects = array(); + + + if (empty($category_meta)) { + return array(); + } + + foreach ($category_meta as $meta) { + + if (!empty($meta)) { + + + $category_template_id = $meta['template_category_id']; + + $template_category_object = new Wiaas_Template_Category_Object( + $category_template_id, + $meta['template_category_title'], + $meta['quantity']); + + $category_objects[$category_template_id] = $template_category_object; + } + + } + return empty($category_objects) ? null : $category_objects; + } + + public static function extract_bundled_product_categories($bundled_items) { + + $template_category_objects = array(); + foreach ($bundled_items as $item_id => $item) { + $item_data = $item->get_data(); + $post_terms = wp_get_object_terms($item->get_product_id(), 'template_category', array('fields' => 'id=>name')); + + if (!empty($post_terms) && !is_wp_error($post_terms)) { + + $cat = ''; + $category_template_id = ''; + + foreach ($post_terms as $id => $term) { + $cat = $term; + $category_template_id = $id; + } + + $template_category_object = new Wiaas_Template_Category_Object( + $category_template_id, + $cat, + $item_data['quantity_max']); + $template_category_objects[$category_template_id] = $template_category_object; + + } + } + + return $template_category_objects; + } + + public static function bind_selected_template_to_product($selected_template, $post_id) { + 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 save_template_product_meta($post_id, $option, $processed_template_data) { + + update_post_meta($post_id, '_template_items_' . $option, $processed_template_data); + + } } } -- 2.47.3 From 6f6fb1115ae21382040c59ad26333e1b96ae60da Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Thu, 27 Sep 2018 16:02:49 +0200 Subject: [PATCH 03/10] renamed variables to --- .../template/class-wiaas-template-admin-ajax.php | 2 +- .../admin/template/class-wiaas-template-products.php | 12 ++++++------ .../template/views/html-wiaas-template-product.php | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) 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 index 842121f..7005b06 100644 --- 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 @@ -53,7 +53,7 @@ class Wiaas_Template_Admin_Ajax { check_ajax_referer('wc_bundles_add_bundled_product', 'security'); $loop = intval($_POST['id']); - $product_id = intval($_POST['product_id']); + $emplate_category_id = intval($_POST['template_category_id']); $title = $_POST['title']; $options = $_POST['options']; 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 index 90aff5d..899c16d 100644 --- 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 @@ -107,7 +107,7 @@ class Wiaas_template { foreach ($template_items as $item) { - $product_id = $item['template_category_id']; + $template_category_id = $item['template_category_id']; $title = $item['template_category_title']; $quantity = $item['quantity']; @@ -157,20 +157,20 @@ class Wiaas_template { foreach ($posted_template_data as $data) { - $product_id = isset($data['product_id']) ? absint($data['product_id']) : false; - $product_title = isset($data['product_title']) ? $data['product_title'] : false; + $template_category_id = isset($data['template_category_id']) ? absint($data['template_category_id']) : false; + $template_category_title = isset($data['template_category_title']) ? $data['template_category_title'] : false; $quantity = isset($data['quantity']) ? absint($data['quantity']) : false; $item_data = array( - 'template_category_id' => $product_id, - 'template_category_title' => trim($product_title), + 'template_category_id' => $template_category_id, + 'template_category_title' => trim($template_category_title), 'quantity' => $quantity ); - $processed_template_data[$product_id] = $item_data; + $processed_template_data[$template_category_id] = $item_data; } diff --git a/backend/app/plugins/wiaas/includes/admin/template/views/html-wiaas-template-product.php b/backend/app/plugins/wiaas/includes/admin/template/views/html-wiaas-template-product.php index 0c0e03d..ea8bd95 100644 --- a/backend/app/plugins/wiaas/includes/admin/template/views/html-wiaas-template-product.php +++ b/backend/app/plugins/wiaas/includes/admin/template/views/html-wiaas-template-product.php @@ -18,8 +18,8 @@ if (!defined('ABSPATH')) { ?> + ?> Date: Fri, 28 Sep 2018 14:24:47 +0200 Subject: [PATCH 04/10] Added documentation to methods --- .../class-wiaas-admin-template-selection.php | 99 ++++++++++--------- .../class-wiaas-template-admin-ajax.php | 6 +- .../class-wiaas-template-products.php | 24 ++++- .../class-wiaas-wc-product-template.php | 29 +++++- 4 files changed, 107 insertions(+), 51 deletions(-) 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 index 8e5fa1b..a052f01 100644 --- 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 @@ -11,6 +11,9 @@ class Wiaas_Admin_Template_Selection { } + /** + * Renders template selection meta box + */ public static function create_template_meta_box() { add_meta_box( 'template_product_meta_box', @@ -22,7 +25,12 @@ class Wiaas_Admin_Template_Selection { ); } - + /** + * Fill in the value of template selection dropdown, + * and render template categories of the selected template + * + * @param $post WP_Post object + */ public static function add_template_content_meta_box($post) { $value = get_post_meta($post->ID, '_select_template', true); @@ -56,31 +64,44 @@ class Wiaas_Admin_Template_Selection { } - public static function get_categories_from_templates($products_form_template) { + /** + * Extract template categories for all of the main product categories + * (software , hardware, services, installation) and combine them in one array + * + * @param $categories_form_template + * @return array of Wiaas_Template_Category_Object + */ + public static function get_categories_from_templates($categories_form_template) { $all_template_categories = array(); $all_template_categories = array_merge($all_template_categories, - WC_Product_Template::extract_template_product_categories($products_form_template['hardware'])); + WC_Product_Template::extract_template_product_categories($categories_form_template['hardware'])); $all_template_categories = array_merge($all_template_categories, - WC_Product_Template::extract_template_product_categories($products_form_template['service'])); + WC_Product_Template::extract_template_product_categories($categories_form_template['service'])); $all_template_categories = array_merge($all_template_categories, - WC_Product_Template::extract_template_product_categories($products_form_template['installation'])); + WC_Product_Template::extract_template_product_categories($categories_form_template['installation'])); $all_template_categories = array_merge($all_template_categories, - WC_Product_Template::extract_template_product_categories($products_form_template['software'])); + WC_Product_Template::extract_template_product_categories($categories_form_template['software'])); return $all_template_categories; } + /** + * Return all of the template product meta_data from selected template + * + * @param $selected_template integer an id of selected template + * @return array of template categories + */ public static function show_template_products($selected_template) { if (empty($selected_template)) { - return; + return array(); } else { @@ -107,6 +128,11 @@ class Wiaas_Admin_Template_Selection { } + /** + * Render html of template categories + * + * @param $template_products array containing template category information + */ public static function render_template_products($template_products) { if (!empty($template_products)) { @@ -123,35 +149,12 @@ class Wiaas_Admin_Template_Selection { } } - public static function get_bundled_product_categories($bundled_items) { - - $template_category_objects = array(); - foreach ($bundled_items as $item_id => $item) { - $item_data = $item->get_data(); - $post_terms = wp_get_object_terms($item->get_product_id(), 'template_category', array('fields' => 'id=>name')); - - if (!empty($post_terms) && !is_wp_error($post_terms)) { - - $cat = ''; - $category_template_id = ''; - - foreach ($post_terms as $id => $term) { - $cat = $term; - $category_template_id = $id; - } - - $template_category_object = new Wiaas_Template_Category_Object( - $category_template_id, - $cat, - $item_data['quantity_max']); - $template_category_objects[$category_template_id] = $template_category_object; - - } - } - - return $template_category_objects; - } + /** + * Save selected template + * + * @param $post_id WP_Post + */ function save_template_content_meta_box($post_id) { $selected_template = $_POST['_select_template']; @@ -160,6 +163,14 @@ class Wiaas_Admin_Template_Selection { } + /** + * Get the template categories from bundle products and selected template + * Compare template categories and quantities that are required by the template with those in bundle products + * + * Admin error is added if validation is not satisfied + * + * @param $product WC_Product_Bundle + */ public static function validate_bundle($product) { @@ -168,8 +179,8 @@ class Wiaas_Admin_Template_Selection { $bundled_items = $product->get_bundled_items('view'); $template_categories_form_template = self::show_template_products($selected_template); - $missing_categories = array(); - $insufficient_product_quantities = array(); + $missing_template_categories = array(); + $insufficient_template_category_quantities = array(); $categories_form_template = self::get_categories_from_templates($template_categories_form_template); $categories_form_bundle = WC_Product_Template::extract_bundled_product_categories($bundled_items); @@ -183,7 +194,7 @@ class Wiaas_Admin_Template_Selection { if (!in_array($template_cat_id, $bundle_category_keys)) { - array_push($missing_categories, $category->name); + array_push($missing_template_categories, $category->name); } else { @@ -191,20 +202,20 @@ class Wiaas_Admin_Template_Selection { $quantity_from_template = $category->quantity; if ((int)($quantity_from_template) !== $product_from_bundle_quantity) { - array_push($insufficient_product_quantities, $category->name); + array_push($insufficient_template_category_quantities, $category->name); } } } } - $categories_message = implode(',', $missing_categories); - $quantity_message = implode(',', $insufficient_product_quantities); + $template_categories_message = implode(',', $missing_template_categories); + $quantity_message = implode(',', $insufficient_template_category_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_message, 'woocommerce-product-bundles')); + if (!empty($missing_template_categories)) { + WC_PB_Meta_Box_Product_Data::add_admin_error(__(' WIAAS This product bundle does not correspond to selected template Categories missing: ' . $template_categories_message, 'woocommerce-product-bundles')); } - if (!empty($insufficient_product_quantities)) { + if (!empty($insufficient_template_category_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')); } } 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 index 7005b06..7c2f04f 100644 --- 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 @@ -21,6 +21,10 @@ class Wiaas_Template_Admin_Ajax { } + /** + * An ajax callback action for handling template selection + */ + public static function ajax_handle_template_selection() { $selected_template_id = intval($_POST['selected_template']); @@ -70,7 +74,7 @@ class Wiaas_Template_Admin_Ajax { } /** - * + * Handles adding template categories search. */ public static function ajax_wiaas_template_category_search() { 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 index 899c16d..9e50ed0 100644 --- 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 @@ -34,7 +34,7 @@ class Wiaas_template { } /** - * Add to product type drop down. + * Add template product type drop down. */ function add_template_product_type($types) { @@ -47,7 +47,7 @@ class Wiaas_template { /** - * Add a custom product tab. + * Add template product, hardware, services, installation and software tabs product tab. */ function custom_product_tabs($tabs) { @@ -79,6 +79,10 @@ class Wiaas_template { } + /** + * Add content do template tabs + */ + public static function wiaastemplate_product_tab_content_all() { self::wiaastemplate_product_tab_content('hardware'); @@ -89,8 +93,11 @@ class Wiaas_template { /** - * Contents of the template options product tab. + * Html contents of the template options product tab. + * + * @param $options string main wiaas category */ + public static function wiaastemplate_product_tab_content($options) { global $post; @@ -133,7 +140,7 @@ class Wiaas_template { /** - * Save the custom fields. + * Save the template categories that are bind to template. */ function save_wiaastemplate($post_id) { @@ -149,6 +156,13 @@ class Wiaas_template { } + /** + * Get extract posted data and save them to meta + * + * @param $post_id integer id of the template + * @param $posted_template_data mixed + * @param $option string indicating main wiaas category + */ public static function process_and_save_template_product_meta($post_id, $posted_template_data, $option) { $processed_template_data = array(); @@ -187,7 +201,7 @@ class Wiaas_template { /** - * Hide Attributes data panel. + * Hide default WC_Product data panels. */ function hide_attributes_data_panel($tabs) { diff --git a/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php b/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php index 7ab999d..8a58748 100644 --- a/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php +++ b/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php @@ -27,10 +27,16 @@ function construct_template_products_class() { return 'wiaastemplate'; } + /** + * Get template categories from template product meta + * + * @param $template_id integer + * @param $template_category_slug string the name of the main wiaas category + * @return mixed + */ public function get_template_categories_from_meta($template_id, $template_category_slug) { return get_post_meta($template_id, $template_category_slug, true); - } public static function extract_template_product_categories($category_meta) { @@ -60,6 +66,12 @@ function construct_template_products_class() { return empty($category_objects) ? null : $category_objects; } + /** + * Get template categories from bundled products items + * + * @param $bundled_items array + * @return array + */ public static function extract_bundled_product_categories($bundled_items) { $template_category_objects = array(); @@ -89,6 +101,13 @@ function construct_template_products_class() { return $template_category_objects; } + /** + * Save selected template to bundled product meta + * + * @param $selected_template integer + * @param $post_id integer + */ + public static function bind_selected_template_to_product($selected_template, $post_id) { if (!empty($selected_template)) update_post_meta($post_id, '_select_template', esc_attr($selected_template)); @@ -97,6 +116,14 @@ function construct_template_products_class() { } } + /** + * Save template categories to template product meta + * + * @param $post_id integer + * @param $option string a main wiaas category + * @param $processed_template_data array actual template category data + */ + public static function save_template_product_meta($post_id, $option, $processed_template_data) { update_post_meta($post_id, '_template_items_' . $option, $processed_template_data); -- 2.47.3 From 4c760b12c3ba89a29eb5001c5a38fac88127335d Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Mon, 1 Oct 2018 17:03:24 +0200 Subject: [PATCH 05/10] Added unit tests for templates --- .../class-wiaas-admin-template-selection.php | 2 +- .../tests/unit-tests/test-wiaas-templates.php | 61 +++++++++++++++++++ .../wiaas/tests/wiaas-unit-test-case.php | 15 +++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-templates.php 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 index a052f01..83411b2 100644 --- 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 @@ -165,7 +165,7 @@ class Wiaas_Admin_Template_Selection { /** * Get the template categories from bundle products and selected template - * Compare template categories and quantities that are required by the template with those in bundle products + * Compare template categories and quantities that are required by the template with those in bundle products * * Admin error is added if validation is not satisfied * diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-templates.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-templates.php new file mode 100644 index 0000000..e3f008d --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-templates.php @@ -0,0 +1,61 @@ +template_category = wp_create_term('Test', 'template_category'); + $this->template_meta = array( + 'template_category_id' => $this->template_category['term_id'], + 'template_category_title' => 'Test', + 'quantity' => 1 + ); + + $this->template = $this->create_new_wiaas_template(); + $this->product = $this->create_new_product(); + $this->package = $this->create_new_package(); + $this->add_products_to_package($this->package, $this->product); + + + } + + public function test_template_category_taxonomy_created() { + $taxonomy = get_taxonomy('template_category'); + + $this->assertInstanceOf(WP_Taxonomy::class, $taxonomy); + + $template_category_names = array_map(function ($term) { + return $term->name; + }, get_terms(array('taxonomy' => 'template_category', 'hide_empty' => false))); + + $this->assertNotEmpty($template_category_names); + $this->assertContains('Test', $template_category_names); + + } + + function test_adding_template_category_to_template() { + + WC_Product_Template::save_template_product_meta($this->template->get_id(), 'hardware', $this->template_meta); + + $test_template_category = WC_Product_Template::get_template_categories_from_meta( + $this->template->get_id(), '_template_items_hardware'); + + $this->assertEquals($test_template_category['template_category_id'], $this->template_meta['template_category_id']); + $this->assertEquals($test_template_category['template_category_title'], $this->template_meta['template_category_title']); + $this->assertEquals($test_template_category['quantity'], $this->template_meta['quantity']); + } + + function save_bind_package_to_template() { + WC_Product_Template::bind_selected_template_to_product($this->template->get_id(), $this->package->get_id()); + $value = get_post_meta($this->package->get_id(), '_select_template', true); + + $this->assertNotEmpty($value); + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php b/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php index 14a5b2f..d4c64b5 100644 --- a/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php +++ b/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php @@ -88,6 +88,21 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase { $package->sync(true); } + + function create_new_wiaas_template() { + $post_id = wp_insert_post(array( + 'post_type' => 'product', + 'post_status' => 'publish', + 'post_name' => 'product', + 'post_title' => 'Package', + 'post_content' => 'Package', + 'post_excerpt' => 'Package' + ), true); + + return new WC_Product_Template($post_id); + + } + function create_new_customer($login = 'customer_test', $organization_name = 'test-customer-organization') { $customer_id = wp_insert_user(array( 'user_login' => $login, -- 2.47.3 From b028fb3f04376af4048100aefd23ab008d17ecd9 Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Wed, 3 Oct 2018 16:58:52 +0200 Subject: [PATCH 06/10] Removed error log --- .../admin/template/class-wiaas-template-admin-ajax.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 index 7c2f04f..9c2eb71 100644 --- 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 @@ -80,9 +80,7 @@ class Wiaas_Template_Admin_Ajax { $term = wp_unslash($_GET['term']); $response = array(); - - error_log('This is term'); - error_log($term); + $terms = get_terms(array( 'taxonomy' => 'template_category', 'name__like' => $term -- 2.47.3 From 7ceac97fed838a7e90480002e1852116ed172658 Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Thu, 4 Oct 2018 10:51:18 +0200 Subject: [PATCH 07/10] Fixed template validation bug --- .../includes/admin/template/class-wiaas-template-admin-ajax.php | 2 +- .../includes/templates/class-wiaas-wc-product-template.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 index 9c2eb71..e580115 100644 --- 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 @@ -57,7 +57,7 @@ class Wiaas_Template_Admin_Ajax { check_ajax_referer('wc_bundles_add_bundled_product', 'security'); $loop = intval($_POST['id']); - $emplate_category_id = intval($_POST['template_category_id']); + $template_category_id = intval($_POST['template_category_id']); $title = $_POST['title']; $options = $_POST['options']; diff --git a/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php b/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php index 8a58748..e9b4198 100644 --- a/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php +++ b/backend/app/plugins/wiaas/includes/templates/class-wiaas-wc-product-template.php @@ -34,7 +34,7 @@ function construct_template_products_class() { * @param $template_category_slug string the name of the main wiaas category * @return mixed */ - public function get_template_categories_from_meta($template_id, $template_category_slug) { + public static function get_template_categories_from_meta($template_id, $template_category_slug) { return get_post_meta($template_id, $template_category_slug, true); } -- 2.47.3 From c5c07cbfaef8e5e2180474b3f7ea63624801dbb4 Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Thu, 4 Oct 2018 12:41:17 +0200 Subject: [PATCH 08/10] Fixed bug where template categoryes are not save properly in template --- .../wiaas/assets/js/wiaas-template-admin-write-panels.js | 6 +++--- .../admin/template/class-wiaas-admin-template-selection.php | 2 +- .../admin/template/class-wiaas-template-admin-ajax.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) 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 index dc22537..24562f8 100644 --- 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 @@ -41,14 +41,14 @@ jQuery(function ($) { $template_search_installation .on('change', function () { - addSearchedTemplateCategory('services', $template_search_installation, $template_categories_container_isntallation) + addSearchedTemplateCategory('installation', $template_search_installation, $template_categories_container_isntallation) }); $template_search_services .on('change', function () { - addSearchedTemplateCategory('installation', $template_search_services, $template_categories_container_services) + addSearchedTemplateCategory('services', $template_search_services, $template_categories_container_services) }); @@ -71,7 +71,7 @@ jQuery(function ($) { post_id: woocommerce_admin_meta_boxes.post_id, id: template_category_add_count, title: template_category_title, - product_id: template_category_id, + template_category_id: template_category_id, security: wc_bundles_admin_params.add_bundled_product_nonce, options: options, }; 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 index 83411b2..cbf217c 100644 --- 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 @@ -109,7 +109,7 @@ class Wiaas_Admin_Template_Selection { $selected_template, '_template_items_hardware'); $template_products_service = WC_Product_Template::get_template_categories_from_meta( - $selected_template, '_template_items_service'); + $selected_template, '_template_items_services'); $template_products_installation = WC_Product_Template::get_template_categories_from_meta( $selected_template, '_template_items_installation'); 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 index e580115..18e0ab5 100644 --- 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 @@ -38,7 +38,7 @@ class Wiaas_Template_Admin_Ajax { 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['services']); Wiaas_Admin_Template_Selection::render_template_products($template_products['installation']); Wiaas_Admin_Template_Selection::render_template_products($template_products['software']); -- 2.47.3 From f41252ea23a5f4ad1fad5bc0449824f33739c22b Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Thu, 4 Oct 2018 12:54:34 +0200 Subject: [PATCH 09/10] Services not listed bug fix --- .../admin/template/class-wiaas-admin-template-selection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index cbf217c..f137739 100644 --- 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 @@ -79,7 +79,7 @@ class Wiaas_Admin_Template_Selection { WC_Product_Template::extract_template_product_categories($categories_form_template['hardware'])); $all_template_categories = array_merge($all_template_categories, - WC_Product_Template::extract_template_product_categories($categories_form_template['service'])); + WC_Product_Template::extract_template_product_categories($categories_form_template['services'])); $all_template_categories = array_merge($all_template_categories, WC_Product_Template::extract_template_product_categories($categories_form_template['installation'])); -- 2.47.3 From c102b6a5ad7e84c084d2022dc98c858634c3c8f6 Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Thu, 4 Oct 2018 13:14:49 +0200 Subject: [PATCH 10/10] Fixed template selection bug --- .../admin/template/class-wiaas-admin-template-selection.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index f137739..2fca714 100644 --- 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 @@ -57,7 +57,7 @@ class Wiaas_Admin_Template_Selection { $template_products_data = self::show_template_products($value); self::render_template_products($template_products_data['hardware']); - self::render_template_products($template_products_data['service']); + self::render_template_products($template_products_data['services']); self::render_template_products($template_products_data['installation']); self::render_template_products($template_products_data['software']); ?> $template_products_hardware, - 'service' => $template_products_service, + 'services' => $template_products_services, 'installation' => $template_products_installation, 'software' => $template_products_software ); -- 2.47.3