Repalced virtual product with template category

This commit is contained in:
Nedim Uka
2018-09-19 22:33:59 +02:00
parent f2d3efc526
commit 434083820c
8 changed files with 297 additions and 78 deletions

View File

@@ -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 );
});
}
});
});

View File

@@ -47,6 +47,7 @@ class Wiaas_Admin_Template_Selection {
?>
<div id="wiaas_selected_template_items_container"><?php
$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['installation']);
@@ -92,13 +93,13 @@ class Wiaas_Admin_Template_Selection {
}
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'];
$product_id = $item['template_category_id'];
$title = $item['template_category_title'];
$quantity = $item['quantity'];
include('views/html-wiaas-template-selection.php');
@@ -107,65 +108,60 @@ class Wiaas_Admin_Template_Selection {
}
}
public static function get_product_categories($products) {
$category_ids = array();
public static function get_product_categories($category_meta) {
$category_objects = array();
if (empty($products)) {
return $category_ids;
if (empty($category_meta)) {
return array();
}
foreach ($products as $product) {
foreach ($category_meta as $meta) {
$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;
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(__(' <strong> WIAAS This product bundle does not correspond to selected template</strong> Categories missing: ' . $categories_meesage, 'woocommerce-product-bundles'));
WC_PB_Meta_Box_Product_Data::add_admin_error(__(' <strong> WIAAS This product bundle does not correspond to selected template</strong> 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();

View File

@@ -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);

View File

@@ -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 {
}
?></div>
<select class="wc-product-search" id="wiaastemplate_products<?php echo '_' . $options ?>" style="width: 250px;"
<select class="wiaas-term-search" id="wiaastemplate_products<?php echo '_' . $options ?>" style="width: 250px;"
name="template_product"
data-placeholder="<?php _e('Add product to template&hellip;', 'wiaas'); ?>"
data-action="woocommerce_json_search_products" multiple="multiple" data-limit="500">
@@ -180,20 +180,13 @@ class Wiaas_template {
$product_id = isset($data['product_id']) ? absint($data['product_id']) : false;
$product_title = isset($data['product_title']) ? $data['product_title'] : false;
$item_id = isset($data['item_id']) ? absint($data['item_id']) : false;
$quantity = isset($data['quantity']) ? absint($data['quantity']) : false;
$product = wc_get_product($product_id);
if (!$product) {
continue;
}
$item_data = array(
'product_id' => $product_id,
'product_title' => $product_title,
'item_id' => $item_id,
'template_category_id' => $product_id,
'template_category_title' => trim($product_title),
'quantity' => $quantity
);

View File

@@ -0,0 +1,11 @@
<?php
class Wiaas_Templates {
public static function init() {
require_once dirname( __FILE__ ) . '/templates/class-wiaas-template-category.php';
require_once dirname( __FILE__ ) . '/templates/class-wiaas-template-category-object.php';
}
}
Wiaas_Templates::init();

View File

@@ -0,0 +1,23 @@
<?php
class Wiaas_Template_Category_Object {
public $template_category_id = '';
public $name = '';
public $quantity = '';
public function __construct($id, $title, $quant) {
$this->template_category_id = $id;
$this->name = $title;
$this->quantity = $quant;
}
public function get_quantity(){
return $this->quantity;
}
}

View File

@@ -0,0 +1,93 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Wiaas_Template_Category
*/
class Wiaas_Template_Category {
public static function init() {
add_action('init', array( __CLASS__, 'register_template_category_taxonomy' ) );
}
/**
* Register wiaas template category taxonomy
*/
public static function register_template_category_taxonomy() {
$labels = array(
'name' => _x( 'Template category', 'taxonomy general name', 'wiaas' ),
'singular_name' => _x( 'Template category', 'taxonomy singular name', 'wiaas' ),
'search_items' => __( 'Search Template categories', 'wiaas' ),
'all_items' => __( 'All Template categories', 'wiaas' ),
'parent_item' => __( 'Parent Template category', 'wiaas' ),
'parent_item_colon' => __( 'Parent Template category:', 'wiaas' ),
'edit_item' => __( 'Edit Template category', 'wiaas' ),
'update_item' => __( 'Update Template category', 'wiaas' ),
'add_new_item' => __( 'Add New Template category', 'wiaas' ),
'new_item_name' => __( 'New Template category Name', 'wiaas' ),
'menu_name' => __( 'Template category', 'wiaas' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'template_category' ),
);
register_taxonomy( 'template_category', array( 'product' ), $args );
}
/**
* Retrieve available wiaas Template categories
* @return array
*/
public static function get_available_template_categories() {
$types = get_terms( array(
'taxonomy' => 'template_category',
'hide_empty' => false,
) );
return array_map(function($type) {
return $type->name;
}, $types);
}
/**
* Retrieve Template category for provided package id
* @param $product_id
*
* @return null
*/
public static function get_template_category($product_id) {
$terms = wp_get_object_terms($product_id, 'template_category');
$template_category = isset($terms[0]) ? $terms[0]->name : null;
return $template_category;
}
/**
* Set Template category for provided package id
* @param $product_id
* @param $type
*/
public static function set_template_category($product_id, $type) {
wp_delete_object_term_relationships( $product_id, 'template_category' );
if (isset($type)) {
wp_set_object_terms($product_id, $type, 'template_category', false);
}
}
}
Wiaas_Template_Category::init();

View File

@@ -43,3 +43,5 @@ include_once WIAAS_DIR . '/includes/class-wiaas-cart.php';
include_once WIAAS_DIR . '/includes/class-wiaas-api.php';
include_once WIAAS_DIR . '/includes/class-wiaas-admin.php';
include_once WIAAS_DIR . '/includes/class-wiass-templates.php';