(optional)', 'cmb'), 'Wiaas_Admin_Template_Selection::add_template_content_meta_box', 'product', 'normal', 'high' ); } /** * 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); 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 )); ?>
ID ); if ($product->get_type() == 'bundle') { $bundled_items = $product->get_bundled_items('view'); $categories_form_bundle = WC_Product_Template::extract_bundled_product_categories($bundled_items); 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); } ?>
$template_products_hardware, '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 * @param $categories_form_bundle */ public static function render_template_products($template_products, $categories_form_bundle = array()) { if (!empty($template_products)) { foreach ($template_products as $item) { $connected_product = $categories_form_bundle[$item['template_category_id']] ? $categories_form_bundle[$item['template_category_id']]->product_name : ''; $product_id = $item['template_category_id']; $title = $item['template_category_title']; $quantity = $item['quantity']; $connected_product_name = $connected_product; include('views/html-wiaas-template-selection.php'); } } } /** * Save selected template * * @param $post_id WP_Post */ function save_template_content_meta_box($post_id) { $selected_template = $_POST['_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'); $template_categories_form_template = self::show_template_products($selected_template); $missing_template_categories = array(); $insufficient_template_category_quantities = array(); $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) { $template_cat_id = $category->template_category_id; if (!in_array($template_cat_id, $bundle_category_keys)) { array_push($missing_template_categories, $category->name); } else { $product_from_bundle_quantity = $categories_form_bundle[$template_cat_id]->quantity; $quantity_from_template = $category->quantity; if ((int)($quantity_from_template) !== $product_from_bundle_quantity) { array_push($insufficient_template_category_quantities, $category->name); } } } } $template_categories_message = implode(',', $missing_template_categories); $quantity_message = implode(',', $insufficient_template_category_quantities); if (!empty($missing_template_categories)) { WC_PB_Meta_Box_Product_Data::add_admin_error(__(' WIAAS This product bundle does not correspond to selected template Categories missing: ' . $template_categories_message, 'woocommerce-product-bundles')); } if (!empty($insufficient_template_category_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')); } } } } Wiaas_Admin_Template_Selection::init();