Refactored template product feature

This commit is contained in:
Nedim Uka
2018-09-27 15:08:21 +02:00
parent 21d96a0e55
commit 93d03a96ee
4 changed files with 119 additions and 93 deletions

View File

@@ -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 <em>(optional)</em>', '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();

View File

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

View File

@@ -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;
?>
<script type='text/javascript'>
jQuery(document).ready(function () {
jQuery('.options_group.pricing').addClass('show_if_simple_template').show();
});
</script><?php
}
/**
* Add a custom product tab.
*/
@@ -142,7 +121,7 @@ class Wiaas_template {
<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">
multiple="multiple" data-limit="500">
<option></option>
</select>
<?php echo wc_help_tip(__('Search for a product and add it to this template by clicking its name in the results list.', 'wiaas')); ?>
@@ -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) {

View File

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