Merge branch 'master' into order-projects
This commit is contained in:
@@ -33,6 +33,8 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
$(this).show();
|
||||
$(this).removeClass('hidden');
|
||||
$(this).removeClass('show_if_downloadable');
|
||||
$(this).addClass('show_if_simple');
|
||||
$(this).addClass('show_if_bundle');
|
||||
|
||||
$(this).find('._download_limit_field, ._download_expiry_field').hide();
|
||||
});
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
<?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['template_category_id'];
|
||||
$title = $item['template_category_title'];
|
||||
$quantity = $item['quantity'];
|
||||
|
||||
include('views/html-wiaas-template-selection.php');
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function get_product_categories($category_meta) {
|
||||
$category_objects = array();
|
||||
|
||||
|
||||
if (empty($category_meta)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
foreach ($category_meta as $meta) {
|
||||
|
||||
if (!empty($meta)) {
|
||||
|
||||
|
||||
$category_template_id = $meta['template_category_id'];
|
||||
|
||||
$template_category_object = new Wiaas_Template_Category_Object(
|
||||
$category_template_id,
|
||||
$meta['template_category_title'],
|
||||
$meta['quantity']);
|
||||
|
||||
$category_objects[$category_template_id] = $template_category_object;
|
||||
}
|
||||
|
||||
}
|
||||
return empty($category_objects) ? null : $category_objects;
|
||||
}
|
||||
|
||||
public static function get_bundled_product_categories($bundled_items) {
|
||||
|
||||
$template_category_objects = array();
|
||||
foreach ($bundled_items as $item_id => $item) {
|
||||
$item_data = $item->get_data();
|
||||
$post_terms = wp_get_object_terms($item->get_product_id(), 'template_category', array('fields' => 'id=>name'));
|
||||
|
||||
if (!empty($post_terms) && !is_wp_error($post_terms)) {
|
||||
|
||||
$cat = '';
|
||||
$category_template_id = '';
|
||||
|
||||
foreach ($post_terms as $id => $term) {
|
||||
$cat = $term;
|
||||
$category_template_id = $id;
|
||||
}
|
||||
|
||||
$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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return $template_category_objects;
|
||||
}
|
||||
|
||||
|
||||
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) {
|
||||
$template_cat_id = $category->template_category_id;
|
||||
|
||||
|
||||
if (!in_array($template_cat_id, $bundle_category_keys)) {
|
||||
|
||||
array_push($missing_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_product_quantities, $category->name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$categories_message = 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_message, '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,93 @@
|
||||
<?php
|
||||
|
||||
// Exit if accessed directly.
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class Wiaas_Template_Admin_Ajax {
|
||||
|
||||
/**
|
||||
* Hook in.
|
||||
*/
|
||||
public static function init() {
|
||||
|
||||
// Ajax template category
|
||||
add_action('wp_ajax_wiaas_add_template_product', array(__CLASS__, 'ajax_add_wiaas_template_product'));
|
||||
add_action('wp_ajax_wiaas_template_category_search', array(__CLASS__, 'ajax_wiaas_template_category_search'));
|
||||
|
||||
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 template categories 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']);
|
||||
$title = $_POST['title'];
|
||||
$options = $_POST['options'];
|
||||
|
||||
$response = array(
|
||||
'markup' => '',
|
||||
'message' => ''
|
||||
);
|
||||
|
||||
ob_start();
|
||||
include('views/html-wiaas-template-product.php');
|
||||
$response['markup'] = ob_get_clean();
|
||||
|
||||
wp_send_json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static function ajax_wiaas_template_category_search() {
|
||||
|
||||
$term = ($_POST['term']);
|
||||
$response = array();
|
||||
|
||||
$terms = get_terms(array(
|
||||
'taxonomy' => 'template_category',
|
||||
'name__like' => $term
|
||||
));
|
||||
|
||||
foreach ($terms as $t) {
|
||||
$response[$t->term_id] = $t->name;
|
||||
}
|
||||
|
||||
wp_send_json($response);
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Template_Admin_Ajax::init();
|
||||
@@ -0,0 +1,226 @@
|
||||
<?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_all'));
|
||||
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_all() {
|
||||
|
||||
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['template_category_id'];
|
||||
$title = $item['template_category_title'];
|
||||
$quantity = $item['quantity'];
|
||||
|
||||
include('views/html-wiaas-template-product.php');
|
||||
|
||||
$loop++;
|
||||
}
|
||||
}
|
||||
?></div>
|
||||
|
||||
<select class="wiaas-term-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;
|
||||
$quantity = isset($data['quantity']) ? absint($data['quantity']) : false;
|
||||
|
||||
|
||||
$item_data = array(
|
||||
|
||||
'template_category_id' => $product_id,
|
||||
'template_category_title' => trim($product_title),
|
||||
'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,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
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_REST_Customer_API {
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $namespace = 'wiaas';
|
||||
|
||||
public function __construct() {
|
||||
include_once dirname( __FILE__ ) . '/../user/class-wiaas-customer.php';
|
||||
include_once dirname( __FILE__ ) . '/helper/class-rest-helper-functions.php';
|
||||
}
|
||||
|
||||
|
||||
public static function register_routes() {
|
||||
|
||||
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/profile-addresses', array(
|
||||
'methods' => 'PUT',
|
||||
'callback' => array(__CLASS__, 'update_customer_profile_addresses'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/profile-addresses/(?P<address_id>\d+)', array(
|
||||
'methods' => 'DELETE',
|
||||
'callback' => array(__CLASS__, 'delete_customer_profile_address'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/billing-addresses', array(
|
||||
'methods' => 'PUT',
|
||||
'callback' => array(__CLASS__, 'update_customer_billing_addresses'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/billing-addresses/(?P<address_id>\d+)', array(
|
||||
'methods' => 'DELETE',
|
||||
'callback' => array(__CLASS__, 'delete_customer_billing_addresses'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/personal-info', array(
|
||||
'methods' => 'PUT',
|
||||
'callback' => array(__CLASS__, 'update_customer_personal_info'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/company-info', array(
|
||||
'methods' => 'PUT',
|
||||
'callback' => array(__CLASS__, 'update_customer_company_info'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function update_customer_profile_addresses(WP_REST_Request $request){
|
||||
$customer_id = $request['id'];
|
||||
$params = $request->get_body_params();
|
||||
$new_address = json_decode($params['profile_address']);
|
||||
|
||||
if (!Wiaas_Customer::update_customer_profile_addresses($customer_id, $new_address)){
|
||||
return self::generate_wiaas_response('PROFILE_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('PROFILE_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
public static function delete_customer_profile_address(WP_REST_Request $request){
|
||||
$customer_id = $request['id'];
|
||||
$address_id = $request['address_id'];
|
||||
|
||||
if (!Wiaas_Customer::delete_customer_profile_address($customer_id, $address_id)){
|
||||
return self::generate_wiaas_response('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
public static function update_customer_billing_addresses(WP_REST_Request $request){
|
||||
$customer_id = $request['id'];
|
||||
$params = $request->get_body_params();
|
||||
$new_address = json_decode($params['billing_address']);
|
||||
|
||||
if (!Wiaas_Customer::update_customer_billing_addresses($customer_id, $new_address)){
|
||||
return self::generate_wiaas_response('BILLING_ADDRESS_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('BILLING_ADDRESS_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
|
||||
}
|
||||
|
||||
public static function delete_customer_billing_addresses(WP_REST_Request $request){
|
||||
$customer_id = $request['id'];
|
||||
$address_id = $request['address_id'];
|
||||
|
||||
if (!Wiaas_Customer::delete_customer_billing_address($customer_id, $address_id)){
|
||||
return self::generate_wiaas_response('ADDRESS_ERROR', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('ADDRESS_REMOVED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
public static function update_customer_personal_info(WP_REST_Request $request){
|
||||
$customer_id = $request['id'];
|
||||
$params = $request->get_body_params();
|
||||
|
||||
$first_name = $params['first_name'];
|
||||
$last_name = $params['last_name'];
|
||||
$phone = $params['phone'];
|
||||
$name = $first_name . ' ' . $last_name;
|
||||
|
||||
if (!is_string($name) || strlen($name) === 1){
|
||||
return self::generate_wiaas_response('ADD_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
if (!is_string($phone) || strlen($phone) < 1){
|
||||
return self::generate_wiaas_response('ADD_PHONE_NUMBER', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
if (!Wiaas_Customer::update_customer_profile_info($customer_id, $first_name, $last_name, $phone)){
|
||||
return self::generate_wiaas_response('PROFILE_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('PROFILE_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
public static function update_customer_company_info(WP_REST_Request $request){
|
||||
$customer_id = $request['id'];
|
||||
$params = $request->get_body_params();
|
||||
|
||||
$company_name = $params['company_name'];
|
||||
$vat_code = $params['vat_code'];
|
||||
|
||||
|
||||
if (!is_string($company_name) || strlen($company_name) < 1){
|
||||
return self::generate_wiaas_response('ADD_COMPANY_NAME', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
if (!is_string($vat_code) || strlen($vat_code) < 1){
|
||||
return self::generate_wiaas_response('ADD_VAT', 'error', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
if (!Wiaas_Customer::update_customer_company_info($customer_id, $company_name, $vat_code)){
|
||||
return self::generate_wiaas_response('COMPANY_NOT_CHANGED', 'warning', Wiaas_Customer::get_customer_info($customer_id));
|
||||
}
|
||||
|
||||
return self::generate_wiaas_response('COMPANY_UPDATED', 'success', Wiaas_Customer::get_customer_info($customer_id));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private static function generate_wiaas_response($message, $code, $data = NULL){
|
||||
$response = array(
|
||||
'messages' => [
|
||||
array(
|
||||
'code' => $code,
|
||||
'message' => $message
|
||||
)
|
||||
],
|
||||
'data' => $data
|
||||
);
|
||||
|
||||
return new WP_REST_Response($response);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
class Wiass_REST_User_API {
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $namespace = 'wiaas';
|
||||
|
||||
public function __construct() {
|
||||
include_once dirname( __FILE__ ) . '/../class-wiaas-countries.php';
|
||||
}
|
||||
|
||||
|
||||
public static function register_routes() {
|
||||
register_rest_route( self::$namespace, 'user/validate-token', array(
|
||||
'methods' => 'POST',
|
||||
'callback' => array(__CLASS__, 'validate_token'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'user/get-countries', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array(__CLASS__, 'get_countries'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
}
|
||||
|
||||
|
||||
public static function validate_token() {
|
||||
$user = wp_get_current_user();
|
||||
|
||||
return new WP_REST_Response(array(
|
||||
'userInfo' => array(
|
||||
'wiaas_id_user' => $user->ID,
|
||||
'wiaas_is_company_admin' => 1, //TODO: don't hardcode this
|
||||
'wiaas_user_full_name' => $user->first_name . ' ' . $user->last_name,
|
||||
'wiaas_user_type' => $user->roles,
|
||||
'wiaas_username' => $user->data->user_login
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
public static function get_countries(){
|
||||
$countries = Wiaas_Countries::get_list_of_countries();
|
||||
|
||||
return new WP_REST_Response($countries);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,10 @@ 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-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();
|
||||
|
||||
@@ -34,13 +34,22 @@ class Wiaas_API {
|
||||
include_once dirname( __FILE__ ) . '/api/class-wiaas-rest-delivery-process-api.php';
|
||||
include_once dirname( __FILE__ ) . '/api/class-wiaas-cart-api.php';
|
||||
include_once dirname( __FILE__ ) . '/api/class-wiaas-document-api.php';
|
||||
|
||||
#User controller
|
||||
include_once dirname( __FILE__ ) . '/api/class-wiaas-rest-user-api.php';
|
||||
|
||||
#Customer controller
|
||||
include_once dirname( __FILE__ ) . '/api/class-wiaas-rest-customer.php';
|
||||
|
||||
}
|
||||
|
||||
public static function register_rest_routes() {
|
||||
$controllers = array(
|
||||
'Wiass_REST_Delivery_Process_API',
|
||||
'Wiaas_Cart_API',
|
||||
'Wiaas_Document_API'
|
||||
'Wiaas_Document_API',
|
||||
'Wiass_REST_User_API',
|
||||
'Wiaas_REST_Customer_API'
|
||||
);
|
||||
|
||||
foreach ( $controllers as $controller ) {
|
||||
|
||||
@@ -17,18 +17,21 @@ class Wiaas_Countries {
|
||||
*/
|
||||
private static $available_countries = array(
|
||||
'Sweden' => array(
|
||||
'id' => 1,
|
||||
'name' => 'Sweden',
|
||||
'code' => 'se',
|
||||
'vat' => 9 ,
|
||||
'currency' => 'SEK'
|
||||
),
|
||||
'Denmark' => array(
|
||||
'id' => 2,
|
||||
'name' => 'Denmark',
|
||||
'code' => 'dk',
|
||||
'vat' => 9 ,
|
||||
'currency' => 'DKK'
|
||||
),
|
||||
'Finland' => array(
|
||||
'id' => 3,
|
||||
'name' => 'Finland',
|
||||
'code' => 'fi',
|
||||
'vat' => 9 ,
|
||||
@@ -41,6 +44,24 @@ class Wiaas_Countries {
|
||||
add_action('woocommerce_after_register_taxonomy', array(__CLASS__, 'register_product_countries_taxonomy'));
|
||||
}
|
||||
|
||||
public static function get_list_of_countries(){
|
||||
$result = [];
|
||||
foreach(self::$available_countries as $country){
|
||||
array_push($result, array(
|
||||
'country_id' => $country['id'],
|
||||
'country_name' => $country['name']
|
||||
));
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function get_country_name_by_id($id){
|
||||
foreach(self::$available_countries as $country){
|
||||
if ($country['id'] == $id) return $country['name'];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers product taxonomy for avaiable countries
|
||||
*/
|
||||
|
||||
@@ -8,8 +8,14 @@ defined( 'ABSPATH' ) || exit;
|
||||
class Wiaas_User {
|
||||
|
||||
public static function init() {
|
||||
include_once dirname( __FILE__ ) . '/class-wiaas-countries.php';
|
||||
include_once dirname( __FILE__ ) . '/user/class-wiaas-customer.php';
|
||||
|
||||
add_action('init', array(__CLASS__, 'load_user_organization'));
|
||||
add_action('plugins_loaded', array(__CLASS__, 'remove_default_user_groups'), 30);
|
||||
|
||||
add_filter('woocommerce_rest_prepare_customer', array(__CLASS__, 'transform_rest_customer'), 10, 3);
|
||||
add_filter('jwt_auth_token_before_dispatch', array(__CLASS__, 'transform_jwt_token_response'), 10, 2);
|
||||
}
|
||||
|
||||
public static function load_user_organization() {
|
||||
@@ -24,6 +30,47 @@ class Wiaas_User {
|
||||
remove_action( 'init', 'wp_register_default_user_group_taxonomy' );
|
||||
remove_action( 'init', 'wp_register_default_user_type_taxonomy' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply wiaas custom transformation on retrieved JSON customer object
|
||||
*
|
||||
* @param $response
|
||||
* @param $order
|
||||
* @param $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function transform_rest_customer($response, $order, $request) {
|
||||
$data = $response->get_data();
|
||||
$user_id = $data['id'];
|
||||
$customer_info = Wiaas_Customer::get_customer_info($user_id);
|
||||
|
||||
return new WP_REST_Response($customer_info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply wiaas custom transformation on JWT token response
|
||||
*
|
||||
* @param $data
|
||||
* @param $user
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function transform_jwt_token_response($data, $user) {
|
||||
return new WP_REST_Response(array(
|
||||
'token' => $data['token'],
|
||||
'userInfo' => array(
|
||||
'wiaas_id_user' => $user->ID,
|
||||
'wiaas_is_company_admin' => 1, //TODO: don't hardcode this
|
||||
'wiaas_user_full_name' => $user->first_name . ' ' . $user->last_name,
|
||||
'wiaas_user_type' => $user->roles,
|
||||
'wiaas_username' => $user->data->user_login
|
||||
)
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Wiaas_User::init();
|
||||
12
backend/app/plugins/wiaas/includes/class-wiass-templates.php
Normal file
12
backend/app/plugins/wiaas/includes/class-wiass-templates.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
class Wiaas_Templates {
|
||||
|
||||
public static function init() {
|
||||
require_once dirname( __FILE__ ) . '/templates/class-wiaas-template-category.php';
|
||||
require_once dirname( __FILE__ ) . '/templates/class-wiaas-template-category-object.php';
|
||||
require_once dirname(__FILE__) . '/templates/class-wiaas-wc-product-template.php';
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Templates::init();
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Wiaas_Template_Category_Object {
|
||||
|
||||
public $template_category_id = '';
|
||||
|
||||
public $name = '';
|
||||
|
||||
public $quantity = '';
|
||||
|
||||
|
||||
public function __construct($id, $title, $quant) {
|
||||
$this->template_category_id = $id;
|
||||
$this->name = $title;
|
||||
$this->quantity = $quant;
|
||||
}
|
||||
|
||||
public function get_quantity(){
|
||||
return $this->quantity;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Class Wiaas_Template_Category
|
||||
*/
|
||||
|
||||
|
||||
class Wiaas_Template_Category {
|
||||
|
||||
public static function init() {
|
||||
|
||||
add_action('init', array( __CLASS__, 'register_template_category_taxonomy' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register wiaas template category taxonomy
|
||||
*/
|
||||
public static function register_template_category_taxonomy() {
|
||||
|
||||
$labels = array(
|
||||
'name' => _x( 'Template category', 'taxonomy general name', 'wiaas' ),
|
||||
'singular_name' => _x( 'Template category', 'taxonomy singular name', 'wiaas' ),
|
||||
'search_items' => __( 'Search Template categories', 'wiaas' ),
|
||||
'all_items' => __( 'All Template categories', 'wiaas' ),
|
||||
'parent_item' => __( 'Parent Template category', 'wiaas' ),
|
||||
'parent_item_colon' => __( 'Parent Template category:', 'wiaas' ),
|
||||
'edit_item' => __( 'Edit Template category', 'wiaas' ),
|
||||
'update_item' => __( 'Update Template category', 'wiaas' ),
|
||||
'add_new_item' => __( 'Add New Template category', 'wiaas' ),
|
||||
'new_item_name' => __( 'New Template category Name', 'wiaas' ),
|
||||
'menu_name' => __( 'Template category', 'wiaas' ),
|
||||
);
|
||||
|
||||
$args = array(
|
||||
'hierarchical' => false,
|
||||
'labels' => $labels,
|
||||
'show_ui' => true,
|
||||
'show_admin_column' => true,
|
||||
'query_var' => true,
|
||||
'rewrite' => array( 'slug' => 'template_category' ),
|
||||
);
|
||||
|
||||
register_taxonomy( 'template_category', array( 'product' ), $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve available wiaas Template categories
|
||||
* @return array
|
||||
*/
|
||||
public static function get_available_template_categories() {
|
||||
$types = get_terms( array(
|
||||
'taxonomy' => 'template_category',
|
||||
'hide_empty' => false,
|
||||
) );
|
||||
|
||||
return array_map(function($type) {
|
||||
return $type->name;
|
||||
}, $types);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve Template category for provided package id
|
||||
* @param $product_id
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public static function get_template_category($product_id) {
|
||||
$terms = wp_get_object_terms($product_id, 'template_category');
|
||||
$template_category = isset($terms[0]) ? $terms[0]->name : null;
|
||||
return $template_category;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Template category for provided package id
|
||||
* @param $product_id
|
||||
* @param $type
|
||||
*/
|
||||
public static function set_template_category($product_id, $type) {
|
||||
|
||||
wp_delete_object_term_relationships( $product_id, 'template_category' );
|
||||
|
||||
if (isset($type)) {
|
||||
wp_set_object_terms($product_id, $type, 'template_category', false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Template_Category::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);
|
||||
218
backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php
Normal file
218
backend/app/plugins/wiaas/includes/user/class-wiaas-customer.php
Normal file
@@ -0,0 +1,218 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class Wiaas_Customer
|
||||
*/
|
||||
class Wiaas_Customer {
|
||||
|
||||
public function __construct() {
|
||||
include_once dirname( __FILE__ ) . '/../class-wiaas-countries.php';
|
||||
}
|
||||
|
||||
public static function get_customer_info($customer_id){
|
||||
$user = get_userdata($customer_id);
|
||||
$result = array(
|
||||
'id' => $customer_id,
|
||||
'company_id' => self::get_customer_company_id($customer_id),
|
||||
'is_company_admin' => self::get_customer_company_admin_status($customer_id),
|
||||
'mail' => $user->user_email,
|
||||
'name' => $user->first_name . ' ' . $user->last_name,
|
||||
'phone' => self::get_customer_phone_number($customer_id),
|
||||
'company_name' => self::get_customer_company_name($customer_id),
|
||||
'vat_code' => self::get_customer_vat_code($customer_id),
|
||||
'billing_addresses' => self::get_customer_billing_addresses($customer_id),
|
||||
'profile_addresses' => self::get_customer_profile_addresses($customer_id),
|
||||
'user_type' => $user->roles
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function get_customer_profile_addresses($customer_id){
|
||||
return get_user_meta($customer_id, 'profile_addresses', true) ?: [];
|
||||
}
|
||||
|
||||
public static function get_customer_billing_addresses($customer_id){
|
||||
return get_user_meta($customer_id, 'billing_addresses', true) ?: [];
|
||||
}
|
||||
|
||||
public static function get_customer_vat_code($customer_id){
|
||||
return get_user_meta($customer_id, 'vat_code', true) ?: '';
|
||||
}
|
||||
|
||||
public static function get_customer_company_name($customer_id){
|
||||
return get_user_meta($customer_id, 'company_name', true) ?: '';
|
||||
}
|
||||
|
||||
public static function get_customer_company_id($customer_id){
|
||||
return 0; //TODO: don't hardocde this
|
||||
}
|
||||
|
||||
public static function get_customer_company_admin_status($customer_id){
|
||||
return 1; //TODO: don't hardcode this
|
||||
}
|
||||
|
||||
public static function get_customer_phone_number($customer_id){
|
||||
return get_user_meta($customer_id, 'phone', true) ?: '';
|
||||
}
|
||||
|
||||
public static function update_customer_profile_info($customer_id, $first_name, $last_name, $phone){
|
||||
$user = array(
|
||||
'ID' => $customer_id,
|
||||
'first_name' => $first_name,
|
||||
'last_name' => $last_name
|
||||
);
|
||||
|
||||
if (is_wp_error(wp_update_user($user))){
|
||||
return false;
|
||||
}
|
||||
update_user_meta( $customer_id, 'phone', $phone);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function update_customer_company_info($customer_id, $company_name, $vat_code){
|
||||
$result1 = update_user_meta( $customer_id, 'company_name', $company_name);
|
||||
$result2 = update_user_meta( $customer_id, 'vat_code', $vat_code );
|
||||
return $result1 || $result2;
|
||||
}
|
||||
|
||||
public static function update_customer_billing_addresses($customer_id, $new_address){
|
||||
if (!self::validate_address($new_address)){
|
||||
return false;
|
||||
}
|
||||
$billing_addresses = self::get_customer_billing_addresses($customer_id);
|
||||
|
||||
if ($new_address->id){
|
||||
$updated = array(
|
||||
'id' => $new_address->id,
|
||||
'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected),
|
||||
'delivery_mail' => $new_address->delivery_mail,
|
||||
'id_country_selected' => $new_address->id_country_selected,
|
||||
'city' => $new_address->city,
|
||||
'detailed_address' => $new_address->detailed_address,
|
||||
'zip_code' => $new_address->zip_code,
|
||||
'first_name' => $new_address->first_name,
|
||||
'last_name' => $new_address->last_name,
|
||||
'invoice_mail' => $new_address->invoice_mail
|
||||
);
|
||||
foreach($billing_addresses as $key => $address){
|
||||
if ($address['id'] === $new_address->id){
|
||||
$billing_addresses[$key] = $updated;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$new_billing_address = array(
|
||||
'id' => time(),
|
||||
'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected),
|
||||
'delivery_mail' => $new_address->delivery_mail,
|
||||
'id_country_selected' => $new_address->id_country_selected,
|
||||
'city' => $new_address->city,
|
||||
'detailed_address' => $new_address->detailed_address,
|
||||
'zip_code' => $new_address->zip_code,
|
||||
'first_name' => $new_address->first_name,
|
||||
'last_name' => $new_address->last_name,
|
||||
'invoice_mail' => $new_address->invoice_mail
|
||||
);
|
||||
|
||||
array_push($billing_addresses, $new_billing_address);
|
||||
}
|
||||
|
||||
return update_user_meta( $customer_id, 'billing_addresses', $billing_addresses);
|
||||
}
|
||||
|
||||
public static function update_customer_profile_addresses($customer_id, $new_address){
|
||||
if (!self::validate_address($new_address)){
|
||||
return false;
|
||||
}
|
||||
$profile_addresses = self::get_customer_profile_addresses($customer_id);
|
||||
|
||||
if ($new_address->id){
|
||||
$updated = array(
|
||||
'id' => $new_address->id,
|
||||
'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected),
|
||||
'delivery_mail' => $new_address->delivery_mail,
|
||||
'id_country_selected' => $new_address->id_country_selected,
|
||||
'city' => $new_address->city,
|
||||
'detailed_address' => $new_address->detailed_address,
|
||||
'zip_code' => $new_address->zip_code,
|
||||
'first_name' => $new_address->first_name,
|
||||
'last_name' => $new_address->last_name,
|
||||
'invoice_mail' => $new_address->invoice_mail
|
||||
);
|
||||
foreach($profile_addresses as $key => $address){
|
||||
if ($address['id'] === $new_address->id){
|
||||
$profile_addresses[$key] = $updated;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$new_delivery_address = array(
|
||||
'id' => time(),
|
||||
'country_name' => Wiaas_Countries::get_country_name_by_id($new_address->id_country_selected),
|
||||
'delivery_mail' => $new_address->delivery_mail,
|
||||
'id_country_selected' => $new_address->id_country_selected,
|
||||
'city' => $new_address->city,
|
||||
'detailed_address' => $new_address->detailed_address,
|
||||
'zip_code' => $new_address->zip_code,
|
||||
'first_name' => $new_address->first_name,
|
||||
'last_name' => $new_address->last_name,
|
||||
'invoice_mail' => $new_address->invoice_mail
|
||||
);
|
||||
|
||||
array_push($profile_addresses, $new_delivery_address);
|
||||
}
|
||||
|
||||
return update_user_meta( $customer_id, 'profile_addresses', $profile_addresses);
|
||||
}
|
||||
|
||||
public static function delete_customer_profile_address($customer_id, $address_id){
|
||||
$profile_addresses = self::get_customer_profile_addresses($customer_id);
|
||||
$counter = 0;
|
||||
foreach($profile_addresses as $key => $address){
|
||||
if ($address['id'] == $address_id){
|
||||
array_splice($profile_addresses, $counter, 1);
|
||||
break;
|
||||
}
|
||||
$counter++;
|
||||
}
|
||||
return update_user_meta( $customer_id, 'profile_addresses', $profile_addresses);
|
||||
}
|
||||
|
||||
public static function delete_customer_billing_address($customer_id, $address_id){
|
||||
$billing_addresses = self::get_customer_billing_addresses($customer_id);
|
||||
$counter = 0;
|
||||
foreach($billing_addresses as $key => $address){
|
||||
if ($address['id'] == $address_id){
|
||||
array_splice($billing_addresses, $counter, 1);
|
||||
break;
|
||||
}
|
||||
$counter++;
|
||||
}
|
||||
return update_user_meta( $customer_id, 'billing_addresses', $billing_addresses);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if address is valid
|
||||
*
|
||||
* @param $address
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private static function validate_address($address){
|
||||
if (empty($address->city)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (empty($address->detailed_address)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return is_numeric($address->zip_code);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user