Added documentation to methods
This commit is contained in:
@@ -11,6 +11,9 @@ class Wiaas_Admin_Template_Selection {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders template selection meta box
|
||||||
|
*/
|
||||||
public static function create_template_meta_box() {
|
public static function create_template_meta_box() {
|
||||||
add_meta_box(
|
add_meta_box(
|
||||||
'template_product_meta_box',
|
'template_product_meta_box',
|
||||||
@@ -22,7 +25,12 @@ class Wiaas_Admin_Template_Selection {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
public static function add_template_content_meta_box($post) {
|
||||||
|
|
||||||
$value = get_post_meta($post->ID, '_select_template', true);
|
$value = get_post_meta($post->ID, '_select_template', true);
|
||||||
@@ -56,31 +64,44 @@ class Wiaas_Admin_Template_Selection {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
$all_template_categories = array_merge($all_template_categories,
|
$all_template_categories = array_merge($all_template_categories,
|
||||||
WC_Product_Template::extract_template_product_categories($products_form_template['hardware']));
|
WC_Product_Template::extract_template_product_categories($categories_form_template['hardware']));
|
||||||
|
|
||||||
$all_template_categories = array_merge($all_template_categories,
|
$all_template_categories = array_merge($all_template_categories,
|
||||||
WC_Product_Template::extract_template_product_categories($products_form_template['service']));
|
WC_Product_Template::extract_template_product_categories($categories_form_template['service']));
|
||||||
|
|
||||||
$all_template_categories = array_merge($all_template_categories,
|
$all_template_categories = array_merge($all_template_categories,
|
||||||
WC_Product_Template::extract_template_product_categories($products_form_template['installation']));
|
WC_Product_Template::extract_template_product_categories($categories_form_template['installation']));
|
||||||
|
|
||||||
$all_template_categories = array_merge($all_template_categories,
|
$all_template_categories = array_merge($all_template_categories,
|
||||||
WC_Product_Template::extract_template_product_categories($products_form_template['software']));
|
WC_Product_Template::extract_template_product_categories($categories_form_template['software']));
|
||||||
|
|
||||||
return $all_template_categories;
|
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) {
|
public static function show_template_products($selected_template) {
|
||||||
|
|
||||||
|
|
||||||
if (empty($selected_template)) {
|
if (empty($selected_template)) {
|
||||||
return;
|
return array();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
@@ -107,6 +128,11 @@ class Wiaas_Admin_Template_Selection {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render html of template categories
|
||||||
|
*
|
||||||
|
* @param $template_products array containing template category information
|
||||||
|
*/
|
||||||
public static function render_template_products($template_products) {
|
public static function render_template_products($template_products) {
|
||||||
if (!empty($template_products)) {
|
if (!empty($template_products)) {
|
||||||
|
|
||||||
@@ -123,35 +149,12 @@ class Wiaas_Admin_Template_Selection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save selected template
|
||||||
|
*
|
||||||
|
* @param $post_id WP_Post
|
||||||
|
*/
|
||||||
|
|
||||||
function save_template_content_meta_box($post_id) {
|
function save_template_content_meta_box($post_id) {
|
||||||
$selected_template = $_POST['_select_template'];
|
$selected_template = $_POST['_select_template'];
|
||||||
@@ -160,6 +163,14 @@ class Wiaas_Admin_Template_Selection {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
public static function validate_bundle($product) {
|
||||||
|
|
||||||
|
|
||||||
@@ -168,8 +179,8 @@ class Wiaas_Admin_Template_Selection {
|
|||||||
$bundled_items = $product->get_bundled_items('view');
|
$bundled_items = $product->get_bundled_items('view');
|
||||||
$template_categories_form_template = self::show_template_products($selected_template);
|
$template_categories_form_template = self::show_template_products($selected_template);
|
||||||
|
|
||||||
$missing_categories = array();
|
$missing_template_categories = array();
|
||||||
$insufficient_product_quantities = array();
|
$insufficient_template_category_quantities = array();
|
||||||
|
|
||||||
$categories_form_template = self::get_categories_from_templates($template_categories_form_template);
|
$categories_form_template = self::get_categories_from_templates($template_categories_form_template);
|
||||||
$categories_form_bundle = WC_Product_Template::extract_bundled_product_categories($bundled_items);
|
$categories_form_bundle = WC_Product_Template::extract_bundled_product_categories($bundled_items);
|
||||||
@@ -183,7 +194,7 @@ class Wiaas_Admin_Template_Selection {
|
|||||||
|
|
||||||
if (!in_array($template_cat_id, $bundle_category_keys)) {
|
if (!in_array($template_cat_id, $bundle_category_keys)) {
|
||||||
|
|
||||||
array_push($missing_categories, $category->name);
|
array_push($missing_template_categories, $category->name);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
@@ -191,20 +202,20 @@ class Wiaas_Admin_Template_Selection {
|
|||||||
$quantity_from_template = $category->quantity;
|
$quantity_from_template = $category->quantity;
|
||||||
|
|
||||||
if ((int)($quantity_from_template) !== $product_from_bundle_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);
|
$template_categories_message = implode(',', $missing_template_categories);
|
||||||
$quantity_message = implode(',', $insufficient_product_quantities);
|
$quantity_message = implode(',', $insufficient_template_category_quantities);
|
||||||
|
|
||||||
if (!empty($missing_categories)) {
|
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: ' . $categories_message, '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: ' . $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'));
|
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'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ class Wiaas_Template_Admin_Ajax {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An ajax callback action for handling template selection
|
||||||
|
*/
|
||||||
|
|
||||||
public static function ajax_handle_template_selection() {
|
public static function ajax_handle_template_selection() {
|
||||||
|
|
||||||
$selected_template_id = intval($_POST['selected_template']);
|
$selected_template_id = intval($_POST['selected_template']);
|
||||||
@@ -70,7 +74,7 @@ class Wiaas_Template_Admin_Ajax {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Handles adding template categories search.
|
||||||
*/
|
*/
|
||||||
public static function ajax_wiaas_template_category_search() {
|
public static function ajax_wiaas_template_category_search() {
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class Wiaas_template {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add to product type drop down.
|
* Add template product type drop down.
|
||||||
*/
|
*/
|
||||||
function add_template_product_type($types) {
|
function add_template_product_type($types) {
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ class Wiaas_template {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a custom product tab.
|
* Add template product, hardware, services, installation and software tabs product tab.
|
||||||
*/
|
*/
|
||||||
function custom_product_tabs($tabs) {
|
function custom_product_tabs($tabs) {
|
||||||
|
|
||||||
@@ -79,6 +79,10 @@ class Wiaas_template {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add content do template tabs
|
||||||
|
*/
|
||||||
|
|
||||||
public static function wiaastemplate_product_tab_content_all() {
|
public static function wiaastemplate_product_tab_content_all() {
|
||||||
|
|
||||||
self::wiaastemplate_product_tab_content('hardware');
|
self::wiaastemplate_product_tab_content('hardware');
|
||||||
@@ -89,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) {
|
public static function wiaastemplate_product_tab_content($options) {
|
||||||
|
|
||||||
global $post;
|
global $post;
|
||||||
@@ -133,7 +140,7 @@ class Wiaas_template {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save the custom fields.
|
* Save the template categories that are bind to template.
|
||||||
*/
|
*/
|
||||||
function save_wiaastemplate($post_id) {
|
function save_wiaastemplate($post_id) {
|
||||||
|
|
||||||
@@ -149,6 +156,13 @@ class Wiaas_template {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
public static function process_and_save_template_product_meta($post_id, $posted_template_data, $option) {
|
||||||
$processed_template_data = array();
|
$processed_template_data = array();
|
||||||
|
|
||||||
@@ -187,7 +201,7 @@ class Wiaas_template {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hide Attributes data panel.
|
* Hide default WC_Product data panels.
|
||||||
*/
|
*/
|
||||||
function hide_attributes_data_panel($tabs) {
|
function hide_attributes_data_panel($tabs) {
|
||||||
|
|
||||||
|
|||||||
@@ -27,10 +27,16 @@ function construct_template_products_class() {
|
|||||||
return 'wiaastemplate';
|
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 function get_template_categories_from_meta($template_id, $template_category_slug) {
|
public function get_template_categories_from_meta($template_id, $template_category_slug) {
|
||||||
|
|
||||||
return get_post_meta($template_id, $template_category_slug, true);
|
return get_post_meta($template_id, $template_category_slug, true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function extract_template_product_categories($category_meta) {
|
public static function extract_template_product_categories($category_meta) {
|
||||||
@@ -60,6 +66,12 @@ function construct_template_products_class() {
|
|||||||
return empty($category_objects) ? null : $category_objects;
|
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) {
|
public static function extract_bundled_product_categories($bundled_items) {
|
||||||
|
|
||||||
$template_category_objects = array();
|
$template_category_objects = array();
|
||||||
@@ -89,6 +101,13 @@ function construct_template_products_class() {
|
|||||||
return $template_category_objects;
|
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) {
|
public static function bind_selected_template_to_product($selected_template, $post_id) {
|
||||||
if (!empty($selected_template))
|
if (!empty($selected_template))
|
||||||
update_post_meta($post_id, '_select_template', esc_attr($selected_template));
|
update_post_meta($post_id, '_select_template', esc_attr($selected_template));
|
||||||
@@ -97,6 +116,14 @@ function construct_template_products_class() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
public static function save_template_product_meta($post_id, $option, $processed_template_data) {
|
||||||
|
|
||||||
update_post_meta($post_id, '_template_items_' . $option, $processed_template_data);
|
update_post_meta($post_id, '_template_items_' . $option, $processed_template_data);
|
||||||
|
|||||||
Reference in New Issue
Block a user