2018-08-30 13:25:22 +02:00
< ? php
class Wiaas_Admin_Template_Selection {
public static function init () {
2018-09-27 15:08:21 +02:00
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 );
2018-08-30 13:25:22 +02:00
// Processes and saves type-specific data.
add_action ( 'woocommerce_admin_process_product_object' , array ( __CLASS__ , 'validate_bundle' ), 11 );
}
2018-09-28 14:24:47 +02:00
/**
* Renders template selection meta box
*/
2018-09-27 15:08:21 +02:00
public static function create_template_meta_box () {
2018-08-30 13:25:22 +02:00
add_meta_box (
'template_product_meta_box' ,
__ ( 'Choose package template <em>(optional)</em>' , 'cmb' ),
2018-09-27 15:08:21 +02:00
'Wiaas_Admin_Template_Selection::add_template_content_meta_box' ,
2018-08-30 13:25:22 +02:00
'product' ,
'normal' ,
'high'
);
}
2018-09-28 14:24:47 +02:00
/**
* Fill in the value of template selection dropdown ,
* and render template categories of the selected template
*
* @ param $post WP_Post object
*/
2018-09-27 15:08:21 +02:00
public static function add_template_content_meta_box ( $post ) {
2018-08-30 13:25:22 +02:00
$value = get_post_meta ( $post -> ID , '_select_template' , true );
if ( empty ( $value )) {
$value = '' ;
}
$products = wc_get_products ( array (
'type' => 'wiaastemplate'
));
$product_ids [ '' ] = __ ( 'Select a template' , 'wiaas' );
foreach ( $products as $prod ) {
$product_ids [ $prod -> id ] = $prod -> name ;
}
woocommerce_wp_select ( array (
'id' => '_select_template' ,
'options' => $product_ids ,
'value' => $value
));
?>
< div id = " wiaas_selected_template_items_container " >< ? php
$template_products_data = self :: show_template_products ( $value );
2018-09-19 22:33:59 +02:00
2018-10-23 15:48:56 +02:00
$product = wc_get_product ( $post -> ID );
2018-10-29 15:18:49 +01:00
if ( $product -> get_type () == 'bundle' ) {
$bundled_items = $product -> get_bundled_items ( 'view' );
$categories_form_bundle = WC_Product_Template :: extract_bundled_product_categories ( $bundled_items );
2018-10-23 15:48:56 +02:00
2018-10-29 15:18:49 +01:00
self :: render_template_products ( $template_products_data [ 'hardware' ], $categories_form_bundle );
self :: render_template_products ( $template_products_data [ 'services' ], $categories_form_bundle );
self :: render_template_products ( $template_products_data [ 'installation' ], $categories_form_bundle );
self :: render_template_products ( $template_products_data [ 'software' ], $categories_form_bundle );
}
2018-08-30 13:25:22 +02:00
?> </div><?php
}
2018-09-28 14:24:47 +02:00
/**
* 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 ) {
2018-08-30 13:25:22 +02:00
$all_template_categories = array ();
2018-09-27 15:08:21 +02:00
$all_template_categories = array_merge ( $all_template_categories ,
2018-09-28 14:24:47 +02:00
WC_Product_Template :: extract_template_product_categories ( $categories_form_template [ 'hardware' ]));
2018-09-27 15:08:21 +02:00
$all_template_categories = array_merge ( $all_template_categories ,
2018-10-04 12:54:34 +02:00
WC_Product_Template :: extract_template_product_categories ( $categories_form_template [ 'services' ]));
2018-09-27 15:08:21 +02:00
$all_template_categories = array_merge ( $all_template_categories ,
2018-09-28 14:24:47 +02:00
WC_Product_Template :: extract_template_product_categories ( $categories_form_template [ 'installation' ]));
2018-09-27 15:08:21 +02:00
$all_template_categories = array_merge ( $all_template_categories ,
2018-09-28 14:24:47 +02:00
WC_Product_Template :: extract_template_product_categories ( $categories_form_template [ 'software' ]));
2018-08-30 13:25:22 +02:00
return $all_template_categories ;
}
2018-09-28 14:24:47 +02:00
/**
* 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
*/
2018-08-30 13:25:22 +02:00
public static function show_template_products ( $selected_template ) {
if ( empty ( $selected_template )) {
2018-09-28 14:24:47 +02:00
return array ();
2018-08-30 13:25:22 +02:00
} else {
2018-09-27 15:08:21 +02:00
$template_products_hardware = WC_Product_Template :: get_template_categories_from_meta (
$selected_template , '_template_items_hardware' );
2018-10-04 13:14:49 +02:00
$template_products_services = WC_Product_Template :: get_template_categories_from_meta (
2018-10-04 12:41:17 +02:00
$selected_template , '_template_items_services' );
2018-09-27 15:08:21 +02:00
$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' );
2018-08-30 13:25:22 +02:00
}
return array (
'hardware' => $template_products_hardware ,
2018-10-04 13:14:49 +02:00
'services' => $template_products_services ,
2018-08-30 13:25:22 +02:00
'installation' => $template_products_installation ,
'software' => $template_products_software
);
}
2018-09-28 14:24:47 +02:00
/**
* Render html of template categories
*
* @ param $template_products array containing template category information
2018-10-23 15:48:56 +02:00
* @ param $categories_form_bundle
2018-09-28 14:24:47 +02:00
*/
2018-10-23 15:48:56 +02:00
public static function render_template_products ( $template_products , $categories_form_bundle = array ()) {
2018-08-30 13:25:22 +02:00
if ( ! empty ( $template_products )) {
2018-09-19 22:33:59 +02:00
2018-08-30 13:25:22 +02:00
foreach ( $template_products as $item ) {
2018-10-23 15:48:56 +02:00
$connected_product = $categories_form_bundle [ $item [ 'template_category_id' ]] ?
$categories_form_bundle [ $item [ 'template_category_id' ]] -> product_name : '' ;
2018-09-19 22:33:59 +02:00
$product_id = $item [ 'template_category_id' ];
$title = $item [ 'template_category_title' ];
2018-08-30 13:25:22 +02:00
$quantity = $item [ 'quantity' ];
2018-10-23 15:48:56 +02:00
$connected_product_name = $connected_product ;
2018-08-30 13:25:22 +02:00
include ( 'views/html-wiaas-template-selection.php' );
}
}
}
2018-09-28 14:24:47 +02:00
/**
* Save selected template
*
* @ param $post_id WP_Post
*/
2018-08-30 13:25:22 +02:00
2018-09-27 15:08:21 +02:00
function save_template_content_meta_box ( $post_id ) {
2018-08-30 13:25:22 +02:00
$selected_template = $_POST [ '_select_template' ];
2018-09-27 15:08:21 +02:00
WC_Product_Template :: bind_selected_template_to_product ( $selected_template , $post_id );
2018-08-30 13:25:22 +02:00
}
2018-09-28 14:24:47 +02:00
/**
* Get the template categories from bundle products and selected template
2018-10-01 17:03:24 +02:00
* Compare template categories and quantities that are required by the template with those in bundle products
2018-09-28 14:24:47 +02:00
*
* Admin error is added if validation is not satisfied
*
* @ param $product WC_Product_Bundle
*/
2018-08-30 13:25:22 +02:00
public static function validate_bundle ( $product ) {
if ( $product -> get_type () == 'bundle' ) {
$selected_template = $_POST [ '_select_template' ];
$bundled_items = $product -> get_bundled_items ( 'view' );
2018-09-27 15:08:21 +02:00
$template_categories_form_template = self :: show_template_products ( $selected_template );
2018-08-30 13:25:22 +02:00
2018-09-28 14:24:47 +02:00
$missing_template_categories = array ();
$insufficient_template_category_quantities = array ();
2018-08-30 13:25:22 +02:00
2018-09-27 15:08:21 +02:00
$categories_form_template = self :: get_categories_from_templates ( $template_categories_form_template );
$categories_form_bundle = WC_Product_Template :: extract_bundled_product_categories ( $bundled_items );
2018-08-30 13:25:22 +02:00
$bundle_category_keys = array_keys ( $categories_form_bundle );
if ( ! empty ( $categories_form_template )) {
foreach ( $categories_form_template as $category ) {
2018-09-19 22:33:59 +02:00
$template_cat_id = $category -> template_category_id ;
2018-08-30 13:25:22 +02:00
2018-09-19 22:33:59 +02:00
if ( ! in_array ( $template_cat_id , $bundle_category_keys )) {
2018-09-28 14:24:47 +02:00
array_push ( $missing_template_categories , $category -> name );
2018-08-30 13:25:22 +02:00
} else {
2018-09-19 22:33:59 +02:00
$product_from_bundle_quantity = $categories_form_bundle [ $template_cat_id ] -> quantity ;
$quantity_from_template = $category -> quantity ;
2018-08-30 13:25:22 +02:00
2018-09-19 22:33:59 +02:00
if (( int )( $quantity_from_template ) !== $product_from_bundle_quantity ) {
2018-09-28 14:24:47 +02:00
array_push ( $insufficient_template_category_quantities , $category -> name );
2018-08-30 13:25:22 +02:00
}
}
}
}
2018-09-28 14:24:47 +02:00
$template_categories_message = implode ( ',' , $missing_template_categories );
$quantity_message = implode ( ',' , $insufficient_template_category_quantities );
2018-08-30 13:25:22 +02:00
2018-09-28 14:24:47 +02:00
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' ));
2018-08-30 13:25:22 +02:00
}
2018-09-28 14:24:47 +02:00
if ( ! empty ( $insufficient_template_category_quantities )) {
2018-08-30 13:25:22 +02:00
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' ));
}
}
}
}
Wiaas_Admin_Template_Selection :: init ();