Added templates product
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
jQuery(document).ready(function ($) {
|
||||
var $selectTemplate = $('#_select_template');
|
||||
var $container = $('#wiaas_selected_template_items_container');
|
||||
|
||||
$selectTemplate
|
||||
|
||||
.on('change', function () {
|
||||
var selectedTemplate = $selectTemplate.val();
|
||||
|
||||
var data = {
|
||||
action: 'wiaas_select_template',
|
||||
selected_template: selectedTemplate
|
||||
};
|
||||
|
||||
|
||||
setTimeout(function () {
|
||||
|
||||
$.post(woocommerce_admin_meta_boxes.ajax_url, data, function (response) {
|
||||
|
||||
if ('' !== response.markup) {
|
||||
|
||||
$container.append(response.markup);
|
||||
|
||||
} else {
|
||||
$container.children("div").remove();
|
||||
}
|
||||
|
||||
if (response.message !== '') {
|
||||
window.alert(response.message);
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
}, 250);
|
||||
});
|
||||
|
||||
var $product_type = $('#product-type');
|
||||
var $template_product_meta_box = $('#template_product_meta_box');
|
||||
$product_type.on('change', function () {
|
||||
var selectedProductType = $product_type.val();
|
||||
|
||||
if (selectedProductType === 'bundle') {
|
||||
$template_product_meta_box.show();
|
||||
} else {
|
||||
$template_product_meta_box.hide();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
@@ -0,0 +1,247 @@
|
||||
jQuery(function ($) {
|
||||
|
||||
var $edit_in_cart = $('.bundle_edit_in_cart'),
|
||||
$group_mode_select = $('select#_wc_pb_group_mode'),
|
||||
$bundled_products_panel = $('#bundled_product_data'),
|
||||
$bundled_products_toolbar = $bundled_products_panel.find('.toolbar'),
|
||||
$template_products_container_hardware = $('.wiaas-template-items_hardware'),
|
||||
$template_products_container_software = $('.wiaas-template-items_software'),
|
||||
$template_products_container_services = $('.wiaas-template-items_services'),
|
||||
$template_products_container_isntallation = $('.wiaas-template-items_installation'),
|
||||
|
||||
$template_products = $('.wc-bundled-item',
|
||||
$template_products_container_services,
|
||||
$template_products_container_software,
|
||||
$template_products_container_hardware,
|
||||
$template_products_container_isntallation),
|
||||
|
||||
$template_search_hardware = $('#wiaastemplate_products_hardware'),
|
||||
$template_search_software = $('#wiaastemplate_products_software'),
|
||||
$template_search_services = $('#wiaastemplate_products_services'),
|
||||
$template_search_installation = $('#wiaastemplate_products_installation'),
|
||||
bundled_product_objects = {},
|
||||
bundled_products_add_count = $template_products.length,
|
||||
block_params = {
|
||||
message: null,
|
||||
overlayCSS: {
|
||||
background: '#fff',
|
||||
opacity: 0.6
|
||||
}
|
||||
};
|
||||
|
||||
$.fn.wc_bundles_select2 = function () {
|
||||
$(document.body).trigger('wc-enhanced-select-init');
|
||||
};
|
||||
|
||||
// Bundle type move stock msg up.
|
||||
$('.bundle_stock_msg').appendTo('._manage_stock_field .description');
|
||||
|
||||
// Simple type options are valid for bundles.
|
||||
$('.show_if_simple:not(.hide_if_bundle)').addClass('show_if_bundle');
|
||||
|
||||
$('body').on('woocommerce-product-type-change', function (event, select_val) {
|
||||
|
||||
if ('bundle' === select_val) {
|
||||
|
||||
$('.show_if_external').hide();
|
||||
$('.show_if_bundle').show();
|
||||
|
||||
$('input#_manage_stock').change();
|
||||
|
||||
$('#_nyp').change();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
$group_mode_select.change(function () {
|
||||
if ($.inArray($group_mode_select.val(), wc_bundles_admin_params.group_modes_with_parent) === -1) {
|
||||
$edit_in_cart.hide();
|
||||
} else {
|
||||
$edit_in_cart.show();
|
||||
}
|
||||
});
|
||||
|
||||
// Downloadable support.
|
||||
$('input#_downloadable').change(function () {
|
||||
$('select#product-type').change();
|
||||
});
|
||||
|
||||
// Trigger product type change.
|
||||
$('select#product-type').change();
|
||||
|
||||
// Trigger group mode change.
|
||||
$group_mode_select.change();
|
||||
|
||||
|
||||
init_event_handlers();
|
||||
|
||||
function init_event_handlers() {
|
||||
|
||||
|
||||
$template_search_hardware
|
||||
|
||||
.on('change', function () {
|
||||
addSearchedProduct('hardware', $template_search_hardware, $template_products_container_hardware)
|
||||
});
|
||||
|
||||
$template_search_software
|
||||
|
||||
.on('change', function () {
|
||||
addSearchedProduct('software', $template_search_software, $template_products_container_software)
|
||||
});
|
||||
|
||||
|
||||
$template_search_installation
|
||||
|
||||
.on('change', function () {
|
||||
addSearchedProduct('services', $template_search_installation, $template_products_container_isntallation)
|
||||
});
|
||||
|
||||
|
||||
$template_search_services
|
||||
|
||||
.on('change', function () {
|
||||
addSearchedProduct('installation', $template_search_services, $template_products_container_services)
|
||||
});
|
||||
|
||||
|
||||
function addSearchedProduct(options, search, container) {
|
||||
var bundled_product_ids = search.val(),
|
||||
bundled_product_id = bundled_product_ids && bundled_product_ids.length > 0 ? bundled_product_ids.shift() : false;
|
||||
|
||||
if (!bundled_product_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
search.val([]).change();
|
||||
|
||||
$bundled_products_panel.block(block_params);
|
||||
|
||||
bundled_products_add_count++;
|
||||
|
||||
var data = {
|
||||
action: 'wiaas_add_template_product',
|
||||
post_id: woocommerce_admin_meta_boxes.post_id,
|
||||
id: bundled_products_add_count,
|
||||
product_id: bundled_product_id,
|
||||
security: wc_bundles_admin_params.add_bundled_product_nonce,
|
||||
options: options,
|
||||
};
|
||||
|
||||
setTimeout(function () {
|
||||
|
||||
$.post(woocommerce_admin_meta_boxes.ajax_url, data, function (response) {
|
||||
|
||||
if ('' !== response.markup) {
|
||||
|
||||
container.append(response.markup);
|
||||
|
||||
var $added = $('.wiaas-template-item', container).last(),
|
||||
added_id = 'bundled_item_' + bundled_products_add_count;
|
||||
|
||||
$added.data('bundled_item_id', added_id);
|
||||
$added.wc_bundles_select2();
|
||||
|
||||
$bundled_products_panel.trigger('wc-bundles-added-bundled-product');
|
||||
|
||||
} else if (response.message !== '') {
|
||||
window.alert(response.message);
|
||||
}
|
||||
|
||||
// Open and close to resolve "sticky" modal issue.
|
||||
if ('yes' === wc_bundles_admin_params.is_wc_version_gte_3_2) {
|
||||
search.selectWoo('open');
|
||||
search.selectWoo('close');
|
||||
} else {
|
||||
search.select2('open');
|
||||
search.select2('close');
|
||||
}
|
||||
|
||||
$bundled_products_panel.unblock();
|
||||
|
||||
});
|
||||
|
||||
}, 250);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$template_products_container_isntallation
|
||||
|
||||
// Remove Item.
|
||||
.on('click', 'a.remove_row', function (e) {
|
||||
|
||||
var $el = $(this).closest('.wc-bundled-item'),
|
||||
el_id = $el.data('bundled_item_id');
|
||||
|
||||
$el.find('*').off();
|
||||
$el.remove();
|
||||
|
||||
delete bundled_product_objects[el_id];
|
||||
|
||||
$bundled_products_panel.triggerHandler('wc-bundled-products-changed');
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
});
|
||||
|
||||
$template_products_container_hardware
|
||||
|
||||
// Remove Item.
|
||||
.on('click', 'a.remove_row', function (e) {
|
||||
|
||||
var $el = $(this).closest('.wc-bundled-item'),
|
||||
el_id = $el.data('bundled_item_id');
|
||||
|
||||
$el.find('*').off();
|
||||
$el.remove();
|
||||
|
||||
delete bundled_product_objects[el_id];
|
||||
|
||||
$bundled_products_panel.triggerHandler('wc-bundled-products-changed');
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
});
|
||||
|
||||
$template_products_container_software
|
||||
|
||||
// Remove Item.
|
||||
.on('click', 'a.remove_row', function (e) {
|
||||
|
||||
var $el = $(this).closest('.wc-bundled-item'),
|
||||
el_id = $el.data('bundled_item_id');
|
||||
|
||||
$el.find('*').off();
|
||||
$el.remove();
|
||||
|
||||
delete bundled_product_objects[el_id];
|
||||
|
||||
$bundled_products_panel.triggerHandler('wc-bundled-products-changed');
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
});
|
||||
|
||||
$template_products_container_services
|
||||
|
||||
// Remove Item.
|
||||
.on('click', 'a.remove_row', function (e) {
|
||||
|
||||
var $el = $(this).closest('.wc-bundled-item'),
|
||||
el_id = $el.data('bundled_item_id');
|
||||
|
||||
$el.find('*').off();
|
||||
$el.remove();
|
||||
|
||||
delete bundled_product_objects[el_id];
|
||||
|
||||
$bundled_products_panel.triggerHandler('wc-bundled-products-changed');
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
<?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);
|
||||
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)) {
|
||||
|
||||
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(__(' <strong> WIAAS This product bundle does not correspond to selected template</strong> Categories missing: ' . $categories_meesage, 'woocommerce-product-bundles'));
|
||||
}
|
||||
|
||||
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'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
// Exit if accessed directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Wiaas_Template_Admin_Ajax {
|
||||
|
||||
/**
|
||||
* Hook in.
|
||||
*/
|
||||
public static function init() {
|
||||
|
||||
// Ajax add bundled product.
|
||||
add_action('wp_ajax_wiaas_add_template_product', array(__CLASS__, 'ajax_add_wiaas_template_product'));
|
||||
|
||||
// Ajax add bundled product.
|
||||
add_action('wp_ajax_wiaas_select_template', array(__CLASS__, 'ajax_handle_template_selection'));
|
||||
|
||||
add_action('wp_ajax_wiaas_product_type_change', array(__CLASS__, 'toggle_template_selection_on_type_change'));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function ajax_handle_template_selection() {
|
||||
|
||||
$selected_template_id = intval($_POST['selected_template']);
|
||||
$response = array(
|
||||
'markup' => '',
|
||||
'message' => ''
|
||||
);
|
||||
|
||||
$template_products = Wiaas_Admin_Template_Selection::show_template_products($selected_template_id);
|
||||
|
||||
if (!empty($template_products)) {
|
||||
ob_start();
|
||||
Wiaas_Admin_Template_Selection::render_template_products($template_products['hardware']);
|
||||
Wiaas_Admin_Template_Selection::render_template_products($template_products['service']);
|
||||
Wiaas_Admin_Template_Selection::render_template_products($template_products['installation']);
|
||||
Wiaas_Admin_Template_Selection::render_template_products($template_products['software']);
|
||||
|
||||
$response['markup'] = ob_get_clean();
|
||||
}
|
||||
|
||||
wp_send_json($response);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handles adding products to template via ajax.
|
||||
*/
|
||||
public static function ajax_add_wiaas_template_product() {
|
||||
|
||||
// check_ajax_referer( 'wc_bundles_add_bundled_product', 'security' );
|
||||
|
||||
$loop = intval($_POST['id']);
|
||||
$product_id = intval($_POST['product_id']);
|
||||
$options = $_POST['options'];
|
||||
$product = wc_get_product($product_id);
|
||||
$title = $product->get_title();
|
||||
|
||||
|
||||
$response = array(
|
||||
'markup' => '',
|
||||
'message' => ''
|
||||
);
|
||||
|
||||
if ($product) {
|
||||
|
||||
ob_start();
|
||||
include('views/html-wiaas-template-product.php');
|
||||
$response['markup'] = ob_get_clean();
|
||||
} else {
|
||||
$response['message'] = __('The selected product is invalid.', 'woocommerce-product-bundles');
|
||||
}
|
||||
|
||||
wp_send_json($response);
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Template_Admin_Ajax::init();
|
||||
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class Wiaas_template {
|
||||
|
||||
|
||||
public static function init() {
|
||||
add_filter('product_type_selector', array(__CLASS__, 'add_template_product_type'));
|
||||
add_action('admin_footer', array(__CLASS__, 'templates_custom_js'));
|
||||
add_filter('woocommerce_product_data_tabs', array(__CLASS__, 'custom_product_tabs'));
|
||||
add_action('woocommerce_product_data_panels', array(__CLASS__, 'wiaastemplate_product_tab_content_software'));
|
||||
add_action('woocommerce_admin_process_product_object', array(__CLASS__, 'save_wiaastemplate'));
|
||||
add_filter('woocommerce_product_data_tabs', array(__CLASS__, 'hide_attributes_data_panel'));
|
||||
add_action('woocommerce_process_product_meta_wiaastemplate', array(__CLASS__, 'save_wiaastemplate'));
|
||||
|
||||
// Enqueue scripts.
|
||||
add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 11);
|
||||
|
||||
}
|
||||
|
||||
public static function enqueue_scripts() {
|
||||
$plugin_url = untrailingslashit(plugins_url('/', WIAAS_FILE));
|
||||
wp_register_script('wiaas-template-admin-write-panels', $plugin_url . '/assets/js/wiaas-template-admin-write-panels.js',
|
||||
array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes'));
|
||||
wp_register_script('wiaas-template-admin-selection', $plugin_url . '/assets/js/wiaas-template-admin-selection.js',
|
||||
array('jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes'));
|
||||
|
||||
wp_enqueue_script( 'wiaas-template-admin-write-panels', $plugin_url . 'assets/js/wiaas-template-admin-write-panels.js',
|
||||
array( 'jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes' ));
|
||||
|
||||
wp_enqueue_script( 'wiaas-template-admin-selection', $plugin_url . '/assets/js/wiaas-template-admin-selection.js',
|
||||
array( 'jquery', 'jquery-ui-datepicker', 'wc-admin-meta-boxes' ));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to product type drop down.
|
||||
*/
|
||||
function add_template_product_type($types) {
|
||||
|
||||
// Key should be exactly the same as in the class
|
||||
$types['wiaastemplate'] = __('Template', 'wiaastemplate');
|
||||
|
||||
return $types;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show pricing fields for template product.
|
||||
*/
|
||||
function templates_custom_js() {
|
||||
|
||||
if ('product' != get_post_type()) :
|
||||
return;
|
||||
endif;
|
||||
|
||||
?>
|
||||
<script type='text/javascript'>
|
||||
jQuery(document).ready(function () {
|
||||
jQuery('.options_group.pricing').addClass('show_if_simple_template').show();
|
||||
});
|
||||
|
||||
</script><?php
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a custom product tab.
|
||||
*/
|
||||
function custom_product_tabs($tabs) {
|
||||
|
||||
$tabs['wiaastemplate_hardware'] = array(
|
||||
'label' => __('Hardware', 'wiaastemplate_hardware'),
|
||||
'target' => 'wiaastemplate_options_hardware',
|
||||
'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'),
|
||||
);
|
||||
|
||||
$tabs['wiaastemplate_installation'] = array(
|
||||
'label' => __('Installation', 'wiaastemplate_isntallation'),
|
||||
'target' => 'wiaastemplate_options_installation',
|
||||
'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'),
|
||||
);
|
||||
|
||||
$tabs['wiaastemplate_services'] = array(
|
||||
'label' => __('Services', 'wiaastemplate_services'),
|
||||
'target' => 'wiaastemplate_options_services',
|
||||
'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'),
|
||||
);
|
||||
|
||||
$tabs['wiaastemplate_software'] = array(
|
||||
'label' => __('Software', 'wiaastemplate_software'),
|
||||
'target' => 'wiaastemplate_options_software',
|
||||
'class' => array('show_if_wiaastemplate', 'show_if_wiaastemplate'),
|
||||
);
|
||||
|
||||
return $tabs;
|
||||
|
||||
}
|
||||
|
||||
public static function wiaastemplate_product_tab_content_software() {
|
||||
|
||||
self::wiaastemplate_product_tab_content('hardware');
|
||||
self::wiaastemplate_product_tab_content('installation');
|
||||
self::wiaastemplate_product_tab_content('services');
|
||||
self::wiaastemplate_product_tab_content('software');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Contents of the template options product tab.
|
||||
*/
|
||||
public static function wiaastemplate_product_tab_content($options) {
|
||||
|
||||
global $post;
|
||||
$template_items = get_post_meta($post->ID, '_template_items_' . $options, true);
|
||||
|
||||
?>
|
||||
<div id='wiaastemplate_options<?php echo '_' . $options ?>' class='panel woocommerce_options_panel'><?php
|
||||
?>
|
||||
<div class="wiaas-template-items<?php echo '_' . $options ?> "><?php
|
||||
|
||||
if (!empty($template_items)) {
|
||||
|
||||
$loop = 0;
|
||||
|
||||
foreach ($template_items as $item) {
|
||||
|
||||
$product_id = $item['product_id'];
|
||||
$title = $item['product_title'];
|
||||
$quantity = $item['quantity'];
|
||||
|
||||
include('views/html-wiaas-template-product.php');
|
||||
|
||||
$loop++;
|
||||
}
|
||||
}
|
||||
?></div>
|
||||
|
||||
<select class="wc-product-search" id="wiaastemplate_products<?php echo '_' . $options ?>" style="width: 250px;"
|
||||
name="template_product"
|
||||
data-placeholder="<?php _e('Add product to template…', 'wiaas'); ?>"
|
||||
data-action="woocommerce_json_search_products" multiple="multiple" data-limit="500">
|
||||
<option></option>
|
||||
</select>
|
||||
<?php echo wc_help_tip(__('Search for a product and add it to this template by clicking its name in the results list.', 'wiaas')); ?>
|
||||
|
||||
</div><?php
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save the custom fields.
|
||||
*/
|
||||
function save_wiaastemplate($post_id) {
|
||||
|
||||
$posted_template_data_hardware = isset($_POST['template_data_hardware']) ? $_POST['template_data_hardware'] : false;
|
||||
$posted_template_data_services = isset($_POST['template_data_services']) ? $_POST['template_data_services'] : false;
|
||||
$posted_template_data_installation = isset($_POST['template_data_installation']) ? $_POST['template_data_installation'] : false;
|
||||
$posted_template_data_software = isset($_POST['template_data_software']) ? $_POST['template_data_software'] : false;
|
||||
|
||||
self::save_template_product_meta($post_id, $posted_template_data_hardware, 'hardware');
|
||||
self::save_template_product_meta($post_id, $posted_template_data_services, 'services');
|
||||
self::save_template_product_meta($post_id, $posted_template_data_installation, 'installation');
|
||||
self::save_template_product_meta($post_id, $posted_template_data_software, 'software');
|
||||
|
||||
}
|
||||
|
||||
public static function save_template_product_meta($post_id, $posted_template_data, $option) {
|
||||
$processed_template_data = array();
|
||||
|
||||
// Sort posted data by menu order.
|
||||
usort($posted_template_data, array(__CLASS__, 'menu_sort_order'));
|
||||
|
||||
foreach ($posted_template_data as $data) {
|
||||
|
||||
$product_id = isset($data['product_id']) ? absint($data['product_id']) : false;
|
||||
$product_title = isset($data['product_title']) ? $data['product_title'] : false;
|
||||
$item_id = isset($data['item_id']) ? absint($data['item_id']) : false;
|
||||
$quantity = isset($data['quantity']) ? absint($data['quantity']) : false;
|
||||
|
||||
$product = wc_get_product($product_id);
|
||||
|
||||
if (!$product) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$item_data = array(
|
||||
|
||||
'product_id' => $product_id,
|
||||
'product_title' => $product_title,
|
||||
'item_id' => $item_id,
|
||||
'quantity' => $quantity
|
||||
|
||||
);
|
||||
|
||||
$processed_template_data[$product_id] = $item_data;
|
||||
|
||||
}
|
||||
|
||||
update_post_meta($post_id, '_template_items_' . $option, $processed_template_data);
|
||||
}
|
||||
|
||||
public static function menu_sort_order($a, $b) {
|
||||
if (isset($a['menu_order']) && isset($b['menu_order'])) {
|
||||
return $a['menu_order'] - $b['menu_order'];
|
||||
} else {
|
||||
return isset($a['menu_order']) ? 1 : -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Hide Attributes data panel.
|
||||
*/
|
||||
function hide_attributes_data_panel($tabs) {
|
||||
|
||||
$tabs['advanced']['class'][] = 'hide_if_simple_wiaastemplate hide_if_wiaastemplate';
|
||||
$tabs['shipping']['class'][] = 'hide_if_simple_wiaastemplate hide_if_wiaastemplate';
|
||||
$tabs['linked_product']['class'][] = 'hide_if_simple_wiaastemplate hide_if_wiaastemplate';
|
||||
$tabs['attribute']['class'][] = 'hide_if_simple_wiaastemplate hide_if_wiaastemplate';
|
||||
|
||||
return $tabs;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_template::init();
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if (false === $template_term_exists = get_term_by('slug', 'wiaastemplate', 'product_type')) {
|
||||
wp_insert_term('wiaastemplate', 'product_type');
|
||||
}
|
||||
|
||||
|
||||
function construct_template_products_class() {
|
||||
|
||||
class WC_Product_Template extends WC_Product {
|
||||
|
||||
|
||||
public function __construct($product) {
|
||||
parent::__construct($product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get internal type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_type() {
|
||||
return 'wiaastemplate';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
add_action('init', 'construct_template_products_class');
|
||||
|
||||
function woocommerce_product_class($classname, $product_type) {
|
||||
|
||||
if ($product_type == 'wiaastemplate') {
|
||||
$classname = 'WC_Product_Template';
|
||||
}
|
||||
|
||||
return $classname;
|
||||
}
|
||||
|
||||
add_filter('woocommerce_product_class', 'woocommerce_product_class', 10, 2);
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
// Exit if accessed directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
<div class="wc-bundled-item wc-metabox <?php echo $toggle; ?>" rel="<?php echo $loop; ?>">
|
||||
<h3>
|
||||
<strong name="<?php echo $loop; ?>" class="item-title"><?php echo $title; ?></strong>
|
||||
<a href="#" class="remove_row delete"><?php echo __('Remove', 'woocommerce'); ?></a>
|
||||
</h3>
|
||||
<div class="item-data wc-metabox-content">
|
||||
<input type="hidden" name="tempate_data[<?php echo $loop; ?>][menu_order]" class="item_menu_order" value="<?php echo $loop; ?>" /><?php
|
||||
|
||||
if ( false !== $item_id ) {
|
||||
?><input type="hidden" name="template_data_<?php echo $options?>[<?php echo $loop; ?>][item_id]" class="item_id" value="<?php echo $item_id; ?>" /><?php
|
||||
}
|
||||
|
||||
?><input type="hidden" name="template_data_<?php echo $options?>[<?php echo $loop; ?>][product_title]" class="product_title" value="<?php echo $title; ?>" /><?php
|
||||
?><input type="hidden" name="template_data_<?php echo $options?>[<?php echo $loop; ?>][product_id]" class="product_id" value="<?php echo $product_id; ?>" />
|
||||
|
||||
</div><?php
|
||||
|
||||
woocommerce_wp_text_input(array(
|
||||
'id' => '_product_quantity',
|
||||
'label' => __('Quantity', 'woocommerce'),
|
||||
'desc_tip' => 'true',
|
||||
'description' => __('Choose the quantity of product', 'woocommerce'),
|
||||
'type' => 'text',
|
||||
'name' => 'template_data_'.$options .'['. $loop.'][quantity]',
|
||||
'value' => $quantity
|
||||
));
|
||||
|
||||
?></div>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
// Exit if accessed directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
<div>
|
||||
<h3>
|
||||
<strong class="item-title"><?php echo $title; ?></strong>
|
||||
<strong class="item-title"><?php echo $quantity; ?></strong>
|
||||
</h3>
|
||||
</div><?php
|
||||
@@ -9,7 +9,11 @@ class Wiaas_Admin {
|
||||
public static function init() {
|
||||
require_once dirname( __FILE__ ) . '/admin/class-wiaas-admin-package.php';
|
||||
require_once dirname( __FILE__ ) . '/admin/class-wiaas-admin-pricing.php';
|
||||
require_once dirname(__FILE__) . '/admin/template/class-wiaas-wc-product-template.php';
|
||||
require_once dirname(__FILE__) . '/admin/template/class-wiaas-admin-template-selection.php';
|
||||
require_once dirname(__FILE__) . '/admin/template/class-wiaas-template-products.php';
|
||||
require_once dirname(__FILE__) . '/admin/template/class-wiaas-template-admin-ajax.php';
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin::init();
|
||||
Wiaas_Admin::init();
|
||||
|
||||
Reference in New Issue
Block a user