Merge branch 'package-template-refactoring' into 'master'

Templates refactoring

See merge request saburly/wiaas/new-wiaas!26
This commit was merged in pull request #26.
This commit is contained in:
Almira
2018-10-04 11:21:17 +00:00
8 changed files with 338 additions and 249 deletions

View File

@@ -3,27 +3,35 @@
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() {
/**
* Renders template selection 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'
);
}
public static function add_custom_content_meta_box($post) {
/**
* 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);
@@ -49,49 +57,82 @@ 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']);
?></div><?php
}
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, 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($categories_form_template['hardware']));
$all_template_categories = array_merge($all_template_categories,
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']));
$all_template_categories = array_merge($all_template_categories,
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 {
$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_services = WC_Product_Template::get_template_categories_from_meta(
$selected_template, '_template_items_services');
$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');
}
return array(
'hardware' => $template_products_hardware,
'service' => $template_products_service,
'services' => $template_products_services,
'installation' => $template_products_installation,
'software' => $template_products_software
);
}
/**
* 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)) {
@@ -108,90 +149,43 @@ class Wiaas_Admin_Template_Selection {
}
}
public static function get_product_categories($category_meta) {
$category_objects = array();
/**
* Save selected template
*
* @param $post_id WP_Post
*/
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();
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;
}
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);
}
/**
* 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) {
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();
$missing_template_categories = array();
$insufficient_template_category_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) {
@@ -200,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 {
@@ -208,37 +202,25 @@ 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(__(' <strong> WIAAS This product bundle does not correspond to selected template</strong> Categories missing: ' . $categories_message, 'woocommerce-product-bundles'));
if (!empty($missing_template_categories)) {
WC_PB_Meta_Box_Product_Data::add_admin_error(__(' <strong> WIAAS This product bundle does not correspond to selected template</strong> 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(__(' <strong> WIAAS This product bundle does not correspond to selected template</strong> Categories with different quantities: ' . $quantity_message, 'woocommerce-product-bundles'));
}
}
}
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

@@ -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']);
@@ -34,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']);
@@ -53,7 +57,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']);
$template_category_id = intval($_POST['template_category_id']);
$title = $_POST['title'];
$options = $_POST['options'];
@@ -70,13 +74,13 @@ class Wiaas_Template_Admin_Ajax {
}
/**
*
* Handles adding template categories search.
*/
public static function ajax_wiaas_template_category_search() {
$term = ($_POST['term']);
$term = wp_unslash($_GET['term']);
$response = array();
$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'));
@@ -35,7 +34,7 @@ class Wiaas_template {
}
/**
* Add to product type drop down.
* Add template product type drop down.
*/
function add_template_product_type($types) {
@@ -48,27 +47,7 @@ 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.
* Add template product, hardware, services, installation and software tabs product tab.
*/
function custom_product_tabs($tabs) {
@@ -100,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');
@@ -110,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;
@@ -128,7 +114,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'];
@@ -142,7 +128,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')); ?>
@@ -154,7 +140,7 @@ class Wiaas_template {
/**
* Save the custom fields.
* Save the template categories that are bind to template.
*/
function save_wiaastemplate($post_id) {
@@ -163,14 +149,21 @@ 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) {
/**
* 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();
// Sort posted data by menu order.
@@ -178,24 +171,24 @@ 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;
}
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) {
@@ -208,7 +201,7 @@ class Wiaas_template {
/**
* Hide Attributes data panel.
* Hide default WC_Product data panels.
*/
function hide_attributes_data_panel($tabs) {

View File

@@ -18,8 +18,8 @@ if (!defined('ABSPATH')) {
?><input type="hidden" name="template_data_<?php echo $options?>[<?php echo $loop; ?>][item_id]" class="item_id" value="<?php echo $item_id; ?>" /><?php
}
?><input type="hidden" name="template_data_<?php echo $options?>[<?php echo $loop; ?>][product_title]" class="product_title" value="<?php echo $title; ?>" /><?php
?><input type="hidden" name="template_data_<?php echo $options?>[<?php echo $loop; ?>][product_id]" class="product_id" value="<?php echo $product_id; ?>" />
?><input type="hidden" name="template_data_<?php echo $options?>[<?php echo $loop; ?>][template_category_title]" class="product_title" value="<?php echo $title; ?>" /><?php
?><input type="hidden" name="template_data_<?php echo $options?>[<?php echo $loop; ?>][template_category_id]" class="product_id" value="<?php echo $template_category_id; ?>" />
</div><?php

View File

@@ -27,7 +27,108 @@ 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 static 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;
}
/**
* 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();
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 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));
else {
update_post_meta($post_id, '_select_template', '');
}
}
/**
* 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);
}
}
}