Repalced virtual product with template category
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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…', '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
|
||||
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user