(optional)', '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 )); ?>
$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)) { foreach ($template_products as $item) { $product_id = $item['product_id']; $title = $item['product_title']; $quantity = $item['quantity']; include('views/html-wiaas-template-selection.php'); } } } public static function get_product_categories($products) { $category_ids = array(); if (empty($products)) { return $category_ids; } foreach ($products as $product) { $product_id = $product['product_id']; $post_terms = wp_get_object_terms($product_id, 'product_cat', array('fields' => 'id=>name')); if (!empty($post_terms) && !is_wp_error($post_terms)) { $cat = ''; foreach ($post_terms as $term) { if ($term != "Uncategorized") { $cat = $term; } } $product_with_cat = array( 'quantity' => $product['quantity'], 'category_term' => $cat ); $category_ids[$product_id] = $product_with_cat; } } return $category_ids; } public static function get_bundled_product_categories($bundled_items) { $category_ids = array(); foreach ($bundled_items as $item_id => $item) { $item_data = $item->get_data(); $post_terms = wp_get_object_terms($item->get_product_id(), 'product_cat', array('fields' => 'id=>name')); if (!empty($post_terms) && !is_wp_error($post_terms)) { $cat = ''; foreach ($post_terms as $term) { if ($term != "Uncategorized") { $cat = $term; } } $category_ids[$cat] = $item_data['quantity_max']; } } return $category_ids; } 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) { if (!in_array($category['category_term'], $bundle_category_keys)) { array_push($missing_categories, $category['category_term']); } else { $product_from_bundle_quantity = $categories_form_bundle[$category['category_term']]; if ($category['quantity'] != $product_from_bundle_quantity) { array_push($insufficient_product_quantities, $category['category_term']); } } } } $categories_meesage = implode(',', $missing_categories); $quantity_message = implode(',', $insufficient_product_quantities); if (!empty($missing_categories)) { WC_PB_Meta_Box_Product_Data::add_admin_error(__(' WIAAS This product bundle does not correspond to selected template Categories missing: ' . $categories_meesage, 'woocommerce-product-bundles')); } if (!empty($insufficient_product_quantities)) { WC_PB_Meta_Box_Product_Data::add_admin_error(__(' WIAAS This product bundle does not correspond to selected template 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();