From 434083820c0d46d344acf1d7c8b3ff25f2e72417 Mon Sep 17 00:00:00 2001 From: Nedim Uka Date: Wed, 19 Sep 2018 22:33:59 +0200 Subject: [PATCH] Repalced virtual product with template category --- .../js/wiaas-template-admin-write-panels.js | 84 +++++++++++++++++ .../class-wiaas-admin-template-selection.php | 91 +++++++++--------- .../class-wiaas-template-admin-ajax.php | 42 ++++++--- .../class-wiaas-template-products.php | 29 +++--- .../wiaas/includes/class-wiass-templates.php | 11 +++ .../class-wiaas-template-category-object.php | 23 +++++ .../class-wiaas-template-category.php | 93 +++++++++++++++++++ backend/app/plugins/wiaas/wiaas.php | 2 + 8 files changed, 297 insertions(+), 78 deletions(-) create mode 100644 backend/app/plugins/wiaas/includes/class-wiass-templates.php create mode 100644 backend/app/plugins/wiaas/includes/templates/class-wiaas-template-category-object.php create mode 100644 backend/app/plugins/wiaas/includes/templates/class-wiaas-template-category.php 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 8054a6e..0baeabe 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 @@ -109,6 +109,8 @@ jQuery(function ($) { var bundled_product_ids = search.val(), bundled_product_id = bundled_product_ids && bundled_product_ids.length > 0 ? bundled_product_ids.shift() : false; + var template_category_title =search.text(); + if (!bundled_product_id) { return false; } @@ -123,6 +125,7 @@ jQuery(function ($) { action: 'wiaas_add_template_product', post_id: woocommerce_admin_meta_boxes.post_id, id: bundled_products_add_count, + title: template_category_title, product_id: bundled_product_id, security: wc_bundles_admin_params.add_bundled_product_nonce, options: options, @@ -243,5 +246,86 @@ jQuery(function ($) { }) } + + // Ajax product search box + $( ':input.wiaas-term-search' ).filter( ':not(.enhanced)' ).each( function() { + var select2_args = { + allowClear: $( this ).data( 'allow_clear' ) ? true : false, + placeholder: $( this ).data( 'placeholder' ), + minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3', + escapeMarkup: function( m ) { + return m; + }, + ajax: { + url: wc_enhanced_select_params.ajax_url, + dataType: 'json', + delay: 250, + data: function( params ) { + return { + term: params.term, + action: 'wiaas_template_category_search', + security: wc_enhanced_select_params.search_products_nonce, + exclude: $( this ).data( 'exclude' ), + include: $( this ).data( 'include' ), + limit: $( this ).data( 'limit' ) + }; + }, + processResults: function( data ) { + var terms = []; + if ( data ) { + $.each( data, function( id, text ) { + terms.push( { id: id, text: text } ); + }); + } + return { + results: terms + }; + }, + cache: true + } + }; + + // select2_args = $.extend( select2_args, getEnhancedSelectFormatString() ); + + $( this ).selectWoo( select2_args ).addClass( 'enhanced' ); + + if ( $( this ).data( 'sortable' ) ) { + var $select = $(this); + var $list = $( this ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' ); + + $list.sortable({ + placeholder : 'ui-state-highlight select2-selection__choice', + forcePlaceholderSize: true, + items : 'li:not(.select2-search__field)', + tolerance : 'pointer', + stop: function() { + $( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() { + var id = $( this ).data( 'data' ).id; + var option = $select.find( 'option[value="' + id + '"]' )[0]; + $select.prepend( option ); + } ); + } + }); + // Keep multiselects ordered alphabetically if they are not sortable. + } else if ( $( this ).prop( 'multiple' ) ) { + $( this ).on( 'change', function(){ + var $children = $( this ).children(); + $children.sort(function(a, b){ + var atext = a.text.toLowerCase(); + var btext = b.text.toLowerCase(); + + if ( atext > btext ) { + return 1; + } + if ( atext < btext ) { + return -1; + } + return 0; + }); + $( this ).html( $children ); + }); + } + }); + }); 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 828768e..e1012a6 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 @@ -47,6 +47,7 @@ class Wiaas_Admin_Template_Selection { ?>
'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; + 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 $category_ids; + return empty($category_objects) ? null : $category_objects; } public static function get_bundled_product_categories($bundled_items) { - $category_ids = array(); + $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(), 'product_cat', array('fields' => 'id=>name')); + $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 $term) { - if ($term != "Uncategorized") { - $cat = $term; - } + foreach ($post_terms as $id => $term) { + $cat = $term; + $category_template_id = $id; } - $category_ids[$cat] = $item_data['quantity_max']; + $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 $category_ids; + return $template_category_objects; } @@ -199,28 +195,30 @@ class Wiaas_Admin_Template_Selection { if (!empty($categories_form_template)) { foreach ($categories_form_template as $category) { + $template_cat_id = $category->template_category_id; - if (!in_array($category['category_term'], $bundle_category_keys)) { - array_push($missing_categories, $category['category_term']); + if (!in_array($template_cat_id, $bundle_category_keys)) { + + array_push($missing_categories, $category->name); } else { - $product_from_bundle_quantity = $categories_form_bundle[$category['category_term']]; + $product_from_bundle_quantity = $categories_form_bundle[$template_cat_id]->quantity; + $quantity_from_template = $category->quantity; - if ($category['quantity'] != $product_from_bundle_quantity) { - array_push($insufficient_product_quantities, $category['category_term']); + if ((int)($quantity_from_template) !== $product_from_bundle_quantity) { + array_push($insufficient_product_quantities, $category->name); } } } } - $categories_meesage = implode(',', $missing_categories); + $categories_message = 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')); + 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($insufficient_product_quantities)) { @@ -230,8 +228,7 @@ class Wiaas_Admin_Template_Selection { } - public - static function get_category_ids_form_bundle($product) { + public static function get_category_ids_form_bundle($product) { $products_form_budnle = $product->get_bundled_data_items(); $product_ids = array(); 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 51f4b98..d38d03e 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 @@ -12,8 +12,9 @@ class Wiaas_Template_Admin_Ajax { */ public static function init() { - // Ajax add bundled product. + // Ajax template category add_action('wp_ajax_wiaas_add_template_product', array(__CLASS__, 'ajax_add_wiaas_template_product')); + add_action('wp_ajax_wiaas_template_category_search', array(__CLASS__, 'ajax_wiaas_template_category_search')); // Ajax add bundled product. add_action('wp_ajax_wiaas_select_template', array(__CLASS__, 'ajax_handle_template_selection')); @@ -22,7 +23,6 @@ class Wiaas_Template_Admin_Ajax { } - public static function ajax_handle_template_selection() { $selected_template_id = intval($_POST['selected_template']); @@ -48,31 +48,47 @@ class Wiaas_Template_Admin_Ajax { /** - * Handles adding products to template via ajax. + * Handles adding template categories to template via ajax. */ public static function ajax_add_wiaas_template_product() { -// check_ajax_referer( 'wc_bundles_add_bundled_product', 'security' ); + check_ajax_referer('wc_bundles_add_bundled_product', 'security'); $loop = intval($_POST['id']); $product_id = intval($_POST['product_id']); + $title = $_POST['title']; $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(); + + wp_send_json($response); + } + + /** + * + */ + public static function ajax_wiaas_template_category_search() { + + $term = ($_POST['term']); + $response = array(); + + $terms = get_terms(array( + 'taxonomy' => 'template_category', + 'name__like' => $term + )); + + foreach ($terms as $t) { + error_log($t->name); + error_log($t->term_id); + $response[$t->term_id] = $t->name; - 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); 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 18e7ff6..ddb5e54 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 @@ -9,7 +9,7 @@ class Wiaas_template { 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_software')); + add_action('woocommerce_product_data_panels', array(__CLASS__, 'wiaastemplate_product_tab_content_all')); add_action('woocommerce_admin_process_product_object', array(__CLASS__, 'save_wiaastemplate')); add_filter('woocommerce_product_data_tabs', array(__CLASS__, 'hide_attributes_data_panel')); add_action('woocommerce_process_product_meta_wiaastemplate', array(__CLASS__, 'save_wiaastemplate')); @@ -26,11 +26,11 @@ class Wiaas_template { wp_register_script('wiaas-template-admin-selection', $plugin_url . '/assets/js/wiaas-template-admin-selection.js', array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes')); - wp_enqueue_script( 'wiaas-template-admin-write-panels', $plugin_url . 'assets/js/wiaas-template-admin-write-panels.js', - array( 'jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes' )); + wp_enqueue_script('wiaas-template-admin-write-panels', $plugin_url . 'assets/js/wiaas-template-admin-write-panels.js', + array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes')); - wp_enqueue_script( 'wiaas-template-admin-selection', $plugin_url . '/assets/js/wiaas-template-admin-selection.js', - array( 'jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes' )); + wp_enqueue_script('wiaas-template-admin-selection', $plugin_url . '/assets/js/wiaas-template-admin-selection.js', + array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes')); } @@ -100,7 +100,7 @@ class Wiaas_template { } - public static function wiaastemplate_product_tab_content_software() { + public static function wiaastemplate_product_tab_content_all() { self::wiaastemplate_product_tab_content('hardware'); self::wiaastemplate_product_tab_content('installation'); @@ -128,8 +128,8 @@ class Wiaas_template { foreach ($template_items as $item) { - $product_id = $item['product_id']; - $title = $item['product_title']; + $product_id = $item['template_category_id']; + $title = $item['template_category_title']; $quantity = $item['quantity']; include('views/html-wiaas-template-product.php'); @@ -139,7 +139,7 @@ class Wiaas_template { } ?>
-