2018-08-30 13:25:22 +02:00
< ? php
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 );
// 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 () {
add_meta_box (
'template_product_meta_box' ,
__ ( 'Choose package template <em>(optional)</em>' , 'cmb' ),
'Wiaas_Admin_Template_Selection::add_custom_content_meta_box' ,
'product' ,
'normal' ,
'high'
);
}
public static function add_custom_content_meta_box ( $post ) {
$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-08-30 13:25:22 +02:00
self :: render_template_products ( $template_products_data [ 'hardware' ]);
self :: render_template_products ( $template_products_data [ 'service' ]);
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 ) {
$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' ]));
return $all_template_categories ;
}
public static function show_template_products ( $selected_template ) {
if ( empty ( $selected_template )) {
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 ));
}
return array (
'hardware' => $template_products_hardware ,
'service' => $template_products_service ,
'installation' => $template_products_installation ,
'software' => $template_products_software
);
}
public static function render_template_products ( $template_products ) {
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-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' ];
include ( 'views/html-wiaas-template-selection.php' );
}
}
}
2018-09-19 22:33:59 +02:00
public static function get_product_categories ( $category_meta ) {
$category_objects = array ();
2018-08-30 13:25:22 +02:00
2018-09-19 22:33:59 +02:00
if ( empty ( $category_meta )) {
return array ();
}
2018-08-30 13:25:22 +02:00
2018-09-19 22:33:59 +02:00
foreach ( $category_meta as $meta ) {
2018-08-30 13:25:22 +02:00
2018-09-19 22:33:59 +02:00
if ( ! empty ( $meta )) {
2018-08-30 13:25:22 +02:00
2018-09-19 22:33:59 +02:00
$category_template_id = $meta [ 'template_category_id' ];
2018-08-30 13:25:22 +02:00
2018-09-19 22:33:59 +02:00
$template_category_object = new Wiaas_Template_Category_Object (
$category_template_id ,
$meta [ 'template_category_title' ],
$meta [ 'quantity' ]);
2018-08-30 13:25:22 +02:00
2018-09-19 22:33:59 +02:00
$category_objects [ $category_template_id ] = $template_category_object ;
2018-08-30 13:25:22 +02:00
}
2018-09-19 22:33:59 +02:00
2018-08-30 13:25:22 +02:00
}
2018-09-19 22:33:59 +02:00
return empty ( $category_objects ) ? null : $category_objects ;
2018-08-30 13:25:22 +02:00
}
public static function get_bundled_product_categories ( $bundled_items ) {
2018-09-19 22:33:59 +02:00
$template_category_objects = array ();
2018-08-30 13:25:22 +02:00
foreach ( $bundled_items as $item_id => $item ) {
$item_data = $item -> get_data ();
2018-09-19 22:33:59 +02:00
$post_terms = wp_get_object_terms ( $item -> get_product_id (), 'template_category' , array ( 'fields' => 'id=>name' ));
2018-08-30 13:25:22 +02:00
if ( ! empty ( $post_terms ) && ! is_wp_error ( $post_terms )) {
$cat = '' ;
2018-09-19 22:33:59 +02:00
$category_template_id = '' ;
2018-08-30 13:25:22 +02:00
2018-09-19 22:33:59 +02:00
foreach ( $post_terms as $id => $term ) {
$cat = $term ;
$category_template_id = $id ;
2018-08-30 13:25:22 +02:00
}
2018-09-19 22:33:59 +02:00
$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 ;
2018-08-30 13:25:22 +02:00
}
}
2018-09-19 22:33:59 +02:00
return $template_category_objects ;
2018-08-30 13:25:22 +02:00
}
function save_custom_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' , '' );
}
}
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 );
$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 );
$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 )) {
array_push ( $missing_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 ) {
array_push ( $insufficient_product_quantities , $category -> name );
2018-08-30 13:25:22 +02:00
}
}
}
}
2018-09-19 22:33:59 +02:00
$categories_message = implode ( ',' , $missing_categories );
2018-08-30 13:25:22 +02:00
$quantity_message = implode ( ',' , $insufficient_product_quantities );
if ( ! empty ( $missing_categories )) {
2018-09-19 22:33:59 +02:00
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' ));
2018-08-30 13:25:22 +02:00
}
if ( ! empty ( $insufficient_product_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' ));
}
}
}
2018-09-19 22:33:59 +02:00
public static function get_category_ids_form_bundle ( $product ) {
2018-08-30 13:25:22 +02:00
$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 ();