reseller to customer
This commit is contained in:
@@ -15,3 +15,7 @@
|
||||
#createuser .acf-taxonomy-field, #your-profile .acf-taxonomy-field {
|
||||
width: 25em;
|
||||
}
|
||||
|
||||
.wc-order-preview footer {
|
||||
display: none;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Admin_CL_Shop {
|
||||
|
||||
public static function init() {
|
||||
|
||||
add_action( 'admin_menu', array( __CLASS__, 'add_customers_page' ), 9 );
|
||||
}
|
||||
|
||||
public static function add_customers_page() {
|
||||
add_menu_page(
|
||||
__( 'Customers', 'wiaas' ),
|
||||
__( 'Customers', 'wiaas' ),
|
||||
'manage_wiaas_cl_customers',
|
||||
'wiaas-cl-customers',
|
||||
array(__CLASS__, 'output_customers'),
|
||||
'dashicons-groups',
|
||||
'66.0' );
|
||||
}
|
||||
|
||||
public static function output_orders() {
|
||||
|
||||
}
|
||||
|
||||
public static function output_customers() {
|
||||
$organization_id = wiaas_get_current_user_organization_id();
|
||||
|
||||
// handle default order type update if needed
|
||||
if (! empty($_POST['wiaas_update_cl_default_order_type_nonce']) &&
|
||||
! empty($_POST['default_order_type']) &&
|
||||
wp_verify_nonce(
|
||||
$_POST['wiaas_update_cl_default_order_type_nonce'],
|
||||
'wiaas_update_cl_default_order_type')
|
||||
) {
|
||||
|
||||
$default_order_type = sanitize_key($_POST['default_order_type']);
|
||||
|
||||
error_log($default_order_type);
|
||||
|
||||
Wiaas_Shop::update_default_order_type($organization_id, $default_order_type);
|
||||
|
||||
}
|
||||
|
||||
// handle customer order type update if needed
|
||||
if (! empty($_POST['wiaas_update_cl_customer_order_type_nonce']) &&
|
||||
! empty($_POST['customer_order_type']) &&
|
||||
wp_verify_nonce(
|
||||
$_POST['wiaas_update_cl_customer_order_type_nonce'],
|
||||
'wiaas_update_cl_customer_order_type')
|
||||
) {
|
||||
|
||||
$customer_id = absint($_POST['customer_order_type']['customer_id']);
|
||||
$order_type = sanitize_key($_POST['customer_order_type']['order_type']);
|
||||
|
||||
Wiaas_Shop_Data_Store::update_shop_customer_order_type(
|
||||
$organization_id,
|
||||
$customer_id,
|
||||
$order_type);
|
||||
}
|
||||
|
||||
$customers = Wiaas_Shop_Data_Store::get_shop_customers($organization_id);
|
||||
|
||||
require 'views/html-admin-cl-customers-page.php';
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin_CL_Shop::init();
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Admin_CL_Orders {
|
||||
|
||||
public static function init() {
|
||||
|
||||
add_filter( 'bulk_actions-edit-shop_order', array( __CLASS__, 'remove_bulk_actions_for_list_table_orders' ), 999 );
|
||||
|
||||
add_filter('woocommerce_admin_order_preview_actions', array(__CLASS__, 'remove_actions_from_order_preview'));
|
||||
|
||||
add_filter('woocommerce_admin_order_preview_line_items', array(__CLASS__, 'filter_order_items_for_order_preview'), 10, 2);
|
||||
|
||||
add_filter('manage_shop_order_posts_columns', array(__CLASS__, 'columns_for_list_table_orders'), 999);
|
||||
|
||||
add_filter( 'manage_edit-shop_order_sortable_columns', array( __CLASS__, 'define_sortable_columns_for_list_table_orders' ) );
|
||||
|
||||
add_action('manage_shop_order_posts_custom_column', array(__CLASS__, 'render_columns_for_list_table_orders'), 999, 2);
|
||||
}
|
||||
|
||||
public static function remove_bulk_actions_for_list_table_orders() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public static function remove_actions_from_order_preview() {
|
||||
return array();
|
||||
}
|
||||
|
||||
public static function filter_order_items_for_order_preview($order_items, $order) {
|
||||
|
||||
$items = array();
|
||||
|
||||
foreach ($order_items as $order_item) {
|
||||
if (isset($order_item['wiaas_standard_package'])) {
|
||||
$items[] = $order_item;
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
public static function columns_for_list_table_orders($columns) {
|
||||
$show_columns = array();
|
||||
$show_columns['cb'] = $columns['cb'];
|
||||
$show_columns['_wiaas_order_number'] = __( 'Order', 'woocommerce' );
|
||||
$show_columns['order_date'] = __( 'Date', 'woocommerce' );
|
||||
$show_columns['order_status'] = __( 'Status', 'woocommerce' );
|
||||
$show_columns['order_total'] = __( 'Total', 'woocommerce' );
|
||||
|
||||
return $show_columns;
|
||||
}
|
||||
|
||||
|
||||
public static function define_sortable_columns_for_list_table_orders($sortable_columns) {
|
||||
|
||||
$sortable_columns['_wiaas_order_number'] = 'ID';
|
||||
|
||||
return $sortable_columns;
|
||||
}
|
||||
|
||||
public static function render_columns_for_list_table_orders($column, $order_id) {
|
||||
|
||||
if ($column === '_wiaas_order_number') {
|
||||
|
||||
$order = wc_get_order($order_id);
|
||||
|
||||
echo '<strong>#' . esc_attr( $order->get_order_number() ) . '</strong>';
|
||||
|
||||
if ( $order->get_status() !== 'trash' ) {
|
||||
echo '<a href="#" class="order-preview" data-order-id="' . absint( $order->get_id() ) . '" title="' . esc_attr( __( 'Preview', 'wiaas' ) ) . '">' . esc_html( __( 'Preview', 'wiaas' ) ) . '</a>';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin_CL_Orders::init();
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<style>
|
||||
select {
|
||||
width: 300px;
|
||||
max-width: 100%;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="wrap">
|
||||
<h1><?php esc_html_e( 'Customers', 'wiaas' ); ?> </h1>
|
||||
|
||||
<br class="clear" />
|
||||
<div id="col-container">
|
||||
|
||||
<div id="col-right">
|
||||
<div class="col-wrap">
|
||||
<table class="widefat wp-list-table striped" style="width:100%">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col"><?php esc_html_e( 'Name', 'wiaas' ); ?></th>
|
||||
<th scope="col"><?php esc_html_e( 'Order type', 'wiaas' ); ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
foreach ($customers as $customer) {
|
||||
$name = wiaas_get_organization_name($customer['customer_id']);
|
||||
?>
|
||||
<tr>
|
||||
<td>
|
||||
<h2><?php esc_html_e($name, 'wiaas') ?></h2>
|
||||
</td>
|
||||
<td>
|
||||
<?php
|
||||
esc_html_e(
|
||||
$customer['order_type']=== 'commercial_lead' ? 'Commercial Lead' : 'Reseller',
|
||||
'wiaas'
|
||||
)
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="col-left">
|
||||
<div class="col-wrap">
|
||||
<div class="inside">
|
||||
<div class="form-wrap">
|
||||
<h2><?php esc_html_e('Default order type:', 'wiaas') ?></h2>
|
||||
<form class="form" action="" method="post">
|
||||
<input type="hidden" name="page" value="wiaas-cl-customers"/>
|
||||
<div class="form-field">
|
||||
<select class="form-control" name="default_order_type">
|
||||
<option
|
||||
value="commercial_lead"
|
||||
<?php selected('commercial_lead', Wiaas_Shop::get_default_order_type($organization_id), true) ?>
|
||||
>
|
||||
<?php esc_html_e('Commercial Lead', 'wiaas') ?>
|
||||
</option>
|
||||
<option
|
||||
value="reseller"
|
||||
<?php selected('reseller', Wiaas_Shop::get_default_order_type($organization_id), true) ?>
|
||||
>
|
||||
<?php esc_html_e('Reseller', 'wiaas') ?>
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
<input class="button button-primary button-large" type="submit" value="Change"/>
|
||||
<?php wp_nonce_field( 'wiaas_update_cl_default_order_type', 'wiaas_update_cl_default_order_type_nonce' ); ?>
|
||||
</form>
|
||||
</div>
|
||||
<hr style="margin: 30px 0;" />
|
||||
|
||||
<div class="form-wrap">
|
||||
<h2><?php esc_html_e('Update order type for customer', 'wiaas') ?></h2>
|
||||
<form class="form" action="" method="post">
|
||||
<div class="form-field">
|
||||
<label><?php esc_html_e('Customer:', 'wiaas') ?> </label>
|
||||
<select class="form-control" name="customer_order_type[customer_id]">
|
||||
<?php
|
||||
foreach ($customers as $customer) {
|
||||
$name = wiaas_get_organization_name($customer['customer_id']);
|
||||
?>
|
||||
<option
|
||||
value="<?php esc_html_e($customer['customer_id'], 'wiaas') ?>"
|
||||
>
|
||||
<?php esc_html_e($name, 'wiaas') ?>
|
||||
</option>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-field">
|
||||
<label><?php esc_html_e('Order type:', 'wiaas') ?> </label>
|
||||
<select class="form-control" name="customer_order_type[order_type]">
|
||||
<option value="commercial_lead">
|
||||
<?php esc_html_e('Commercial Lead', 'wiaas') ?>
|
||||
</option>
|
||||
<option value="reseller">
|
||||
<?php esc_html_e('Reseller', 'wiaas') ?>
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
<input class="button button-large" type="submit" value="Update"/>
|
||||
<?php wp_nonce_field( 'wiaas_update_cl_customer_order_type', 'wiaas_update_cl_customer_order_type_nonce' ); ?>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
@@ -9,6 +9,10 @@ class Wiaas_Admin_CL {
|
||||
public static function init() {
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/class-wiaas-admin-cl-packages.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/class-wiaas-admin-cl-customers.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/class-wiaas-admin-cl-orders.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/class-wiaas-admin-cl-packages-list.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/wiaas-admin-cl-packages-ajax.php';
|
||||
|
||||
@@ -14,8 +14,14 @@ class Wiaas_Admin_Organization {
|
||||
|
||||
add_filter('get_role_list', array(__CLASS__, 'get_role_list_for_user'), 10, 2);
|
||||
|
||||
|
||||
// hide woocommerce meta fields form customer user profile
|
||||
add_filter('woocommerce_customer_meta_fields', array(__CLASS__, 'hide_woocommerce_customer_fields'));
|
||||
|
||||
// save related customers when organization form data has been saved by acf
|
||||
add_action('acf/save_post', array(__CLASS__, 'maybe_save_related_customers'), 1);
|
||||
|
||||
// load related customers for organization form
|
||||
add_filter('acf/load_value/name=_wiaas_organization_customers', array(__CLASS__, 'load_related_customer_organizations'), 10, 3);
|
||||
}
|
||||
|
||||
public static function hide_woocommerce_customer_fields() {
|
||||
@@ -26,6 +32,49 @@ class Wiaas_Admin_Organization {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves related customers if organization new/edit form has been submited
|
||||
* Customer ids are collected from posted data of linked customers acf field and saved
|
||||
*
|
||||
* @param string $id in format `term_{$organization_id}`
|
||||
*/
|
||||
public static function maybe_save_related_customers($id) {
|
||||
if ($_POST['taxonomy'] === Wiaas_User_Organization::TAXONOMY_NAME && ! empty($_POST['acf'])) {
|
||||
|
||||
$field = get_field_object('_wiaas_organization_customers', $id);
|
||||
//get organization id
|
||||
$id = absint(str_replace('term_', '', $id));
|
||||
|
||||
$customer_organization_ids = $_POST['acf'][$field['key']];
|
||||
|
||||
$customer_organization_ids = is_array($customer_organization_ids) ?
|
||||
wp_parse_id_list($customer_organization_ids) :
|
||||
array();
|
||||
|
||||
Wiaas_Shop::set_shop_customers($id, $customer_organization_ids);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads related customers for linked customers acf field on organization edit form
|
||||
*
|
||||
* @param array $value
|
||||
* @param string $id in format `term_{$organization_id}`
|
||||
* @param array $field acf field details
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function load_related_customer_organizations($value, $id, $field) {
|
||||
//get organization id
|
||||
$id = absint(str_replace('term_', '', $id));
|
||||
|
||||
$customers = wp_list_pluck(
|
||||
Wiaas_Shop_Data_Store::get_shop_customers($id),
|
||||
'customer_id');
|
||||
|
||||
return $customers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render user organization roles as available user roles on user list
|
||||
* @param $role_list
|
||||
|
||||
@@ -31,12 +31,20 @@ class Wiaas_Cart_API {
|
||||
'package_id' => array(
|
||||
'description' => __( 'Wiaas package ID.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'cl_id' => array(
|
||||
'description' => __( 'Commercial lead ID.', 'wiaas' ),
|
||||
'type' => 'integer',
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'absint',
|
||||
),
|
||||
'price_id' => array(
|
||||
'description' => __( 'Selected price ID for Wiaas package.', 'wiaas' ),
|
||||
'type' => 'string',
|
||||
'enum' => array_keys(Wiaas_Package_Pricing::get_available_pay_types()),
|
||||
'required' => true,
|
||||
'sanitize_callback' => 'sanitize_key',
|
||||
),
|
||||
'options_ids' => array(
|
||||
@@ -225,6 +233,7 @@ class Wiaas_Cart_API {
|
||||
$success = Wiaas_Cart::add_package_to_cart(
|
||||
$request['package_id'],
|
||||
$request['price_id'],
|
||||
$request['cl_id'],
|
||||
$request['addons_ids'],
|
||||
$request['options_ids']
|
||||
);
|
||||
|
||||
@@ -2,34 +2,32 @@
|
||||
|
||||
class Wiaas_Package_API {
|
||||
|
||||
private static $namespace = 'wiaas';
|
||||
|
||||
public static function init() {
|
||||
|
||||
add_filter('woocommerce_rest_product_object_query', array(__CLASS__, 'filter_packages'), 10, 2);
|
||||
|
||||
add_filter('rest_dispatch_request', array(__CLASS__, 'validate_package_search_request'), 10, 4);
|
||||
}
|
||||
|
||||
public static function register_routes() {
|
||||
// TODO: Handle this when assigment of customer to commercial lead is done
|
||||
register_rest_route( self::$namespace, '/commercial-leads', array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array(__CLASS__, 'get_customer_commercial_leads'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
// do nothing
|
||||
}
|
||||
|
||||
// TODO: Handle this when assigment of customer to commercial lead is done
|
||||
public static function get_customer_commercial_leads() {
|
||||
$commercial_leads = array();
|
||||
public static function validate_package_search_request($null, $request, $route, $handler) {
|
||||
|
||||
foreach (wiaas_get_commercial_leads() as $id => $name) {
|
||||
$commercial_leads[] = array(
|
||||
'id' => $id,
|
||||
'name' => $name
|
||||
);
|
||||
}
|
||||
if (strpos($route, '/wc/v2/products') !== false) {
|
||||
|
||||
return rest_ensure_response($commercial_leads);
|
||||
if (empty($request['cl_id']) || ! absint($request['cl_id'])) {
|
||||
|
||||
return new WP_Error(
|
||||
'missing_commercial_lead',
|
||||
'Commercial lead is missing',
|
||||
array ( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* TODO: Refactor this class so it reflects the fact that customer is an organization
|
||||
* TODO: Format should be `customer/(?P<organization_id>\d+)/user/(?P<id>\d+)
|
||||
*
|
||||
* Class Wiaas_REST_Customer_API
|
||||
*/
|
||||
|
||||
class Wiaas_REST_Customer_API {
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
@@ -9,13 +16,19 @@ class Wiaas_REST_Customer_API {
|
||||
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+)/shops', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array(__CLASS__, 'get_customer_shops'),
|
||||
'permission_callback' => 'is_user_logged_in'
|
||||
) );
|
||||
|
||||
register_rest_route( self::$namespace, 'customer/(?P<id>\d+)/profile-addresses', array(
|
||||
'methods' => 'PUT',
|
||||
'callback' => array(__CLASS__, 'update_customer_profile_addresses'),
|
||||
@@ -54,6 +67,23 @@ class Wiaas_REST_Customer_API {
|
||||
|
||||
}
|
||||
|
||||
public static function get_customer_shops() {
|
||||
|
||||
$customer_id = wiaas_get_current_user_organization_id();
|
||||
|
||||
$customer_shops = Wiaas_Shop_Data_Store::get_customer_shops($customer_id);
|
||||
|
||||
$customer_shops = array_map(function($customer_shop) {
|
||||
return array(
|
||||
'id' => $customer_shop['owner_id'],
|
||||
'type' => $customer_shop['order_type'],
|
||||
'name' => wiaas_get_organization_name($customer_shop['owner_id'])
|
||||
);
|
||||
}, $customer_shops);
|
||||
|
||||
return rest_ensure_response($customer_shops);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function update_customer_profile_addresses(WP_REST_Request $request){
|
||||
|
||||
@@ -114,12 +114,13 @@ class Wiaas_Cart {
|
||||
*
|
||||
* @param int $package_id Package ID of selected package
|
||||
* @param string $price_id Price ID of selected package payment
|
||||
* @param int $commercial_lead_id Shop owner commercial lead ID
|
||||
* @param array $addons_ids Array of selected additional packages IDs
|
||||
* @param array $options_ids Array of selected option packages IDs
|
||||
*
|
||||
* @return bool TRUE if all packages are succesfully added to cart, FALSE otherwise
|
||||
*/
|
||||
public static function add_package_to_cart($package_id, $price_id, $addons_ids, $options_ids) {
|
||||
public static function add_package_to_cart($package_id, $price_id, $commercial_lead_id, $addons_ids, $options_ids) {
|
||||
// try adding package to cart
|
||||
try {
|
||||
// Check if package is in cart
|
||||
@@ -144,12 +145,11 @@ class Wiaas_Cart {
|
||||
// Retrieve package country
|
||||
$country = Wiaas_Countries::get_package_country($package);
|
||||
|
||||
// TODO: Change this so commercial lead is sent via request
|
||||
$customer_id = wiaas_get_current_user_organization_id();
|
||||
$commercial_lead_id = array_keys(wiaas_get_commercial_leads())[0];
|
||||
|
||||
// Retrieve package price
|
||||
$package_prices = Wiaas_Pricing::get_standard_package_customer_prices($package, $customer_id, $commercial_lead_id);
|
||||
|
||||
$selected_price_index = array_search($price_id, array_column($package_prices, 'id'));
|
||||
|
||||
// Initialize additional cart item data for wiaas packages
|
||||
@@ -159,6 +159,7 @@ class Wiaas_Cart {
|
||||
'_wiaas_option_items' => array(),
|
||||
'_wiaas_currency' => isset($country) ? $country['currency'] : get_woocommerce_currency(),
|
||||
'_wiaas_payment' => $package_prices[$selected_price_index] ? $package_prices[$selected_price_index] : null,
|
||||
'_wiaas_commercial_lead_id' => $commercial_lead_id,
|
||||
'_wiaas_documents' => array()
|
||||
);
|
||||
|
||||
@@ -170,7 +171,7 @@ class Wiaas_Cart {
|
||||
}
|
||||
|
||||
// Add selected additional packages and options
|
||||
self::_add_additional_packages_to_cart($cart_item_key, $price_id, $addons_ids, $options_ids);
|
||||
self::_add_additional_packages_to_cart($cart_item_key, $price_id, $commercial_lead_id, $addons_ids, $options_ids);
|
||||
|
||||
// Trigger calculation of total prices after additional packages are added
|
||||
WC()->cart->calculate_totals();
|
||||
@@ -302,6 +303,9 @@ class Wiaas_Cart {
|
||||
if (isset($cart_item['_wiaas_currency'])) {
|
||||
$order_item->add_meta_data( '_wiaas_currency', $cart_item['_wiaas_currency'], true );
|
||||
}
|
||||
if (isset($cart_item['_wiaas_commercial_lead_id'])) {
|
||||
$order_item->add_meta_data( '_wiaas_commercial_lead_id', $cart_item['_wiaas_commercial_lead_id'], true );
|
||||
}
|
||||
|
||||
// add options metadata
|
||||
if (isset($cart_item['_wiaas_option_items'])) {
|
||||
@@ -380,7 +384,8 @@ class Wiaas_Cart {
|
||||
'_wiaas_option_group_name',
|
||||
'_wiaas_standard_package',
|
||||
'_wiaas_currency',
|
||||
'_wiaas_documents'
|
||||
'_wiaas_documents',
|
||||
'_wiaas_commercial_lead_id'
|
||||
) );
|
||||
}
|
||||
|
||||
@@ -636,21 +641,20 @@ class Wiaas_Cart {
|
||||
*
|
||||
* @param string $package_cart_item_key
|
||||
* @param int $price_id
|
||||
* @param int $commercial_lead_id
|
||||
* @param array $addons_ids
|
||||
* @param array $options_ids
|
||||
*
|
||||
* @throws Exception if any of the addons or options cannot be added to cart
|
||||
*/
|
||||
private static function _add_additional_packages_to_cart($package_cart_item_key, $price_id, $addons_ids, $options_ids) {
|
||||
private static function _add_additional_packages_to_cart($package_cart_item_key, $price_id, $commercial_lead_id, $addons_ids, $options_ids) {
|
||||
|
||||
$parent_item = WC()->cart->get_cart_item($package_cart_item_key);
|
||||
|
||||
$addon_items_keys = array();
|
||||
$option_items_keys = array();
|
||||
|
||||
// TODO: Change this so commercial lead is sent via request
|
||||
$customer_id = wiaas_get_current_user_organization_id();
|
||||
$commercial_lead_id = array_keys(wiaas_get_commercial_leads())[0];
|
||||
|
||||
// Try adding package addons to cart
|
||||
foreach ($addons_ids as $addon_id) {
|
||||
|
||||
@@ -85,12 +85,15 @@ class Wiaas_Checkout {
|
||||
private static function _add_wiaas_checkout_data($order, $data) {
|
||||
// save currency
|
||||
$line_items = $order->get_items();
|
||||
foreach ($line_items as $line_item) {
|
||||
if (isset($line_item['wiaas_currency'])) {
|
||||
$order->set_currency($line_item['wiaas_currency']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$currency = array_column($line_items, 'wiaas_currency')[0];
|
||||
$currency = empty($currency) ? get_woocommerce_currency() : $currency;
|
||||
$order->set_currency($currency);
|
||||
|
||||
// set order commercial lead
|
||||
$commercial_lead_id = array_column($line_items, 'wiaas_commercial_lead_id')[0];
|
||||
$order->add_meta_data('wiaas_commercial_lead_id', $commercial_lead_id, true);
|
||||
$order->save_meta_data();
|
||||
|
||||
// save additional wiaas order info
|
||||
Wiaas_Order::set_order_vat($order->get_id(), $data['vat']);
|
||||
@@ -102,6 +105,7 @@ class Wiaas_Checkout {
|
||||
if (isset($data['project_id'])) {
|
||||
Wiaas_Order_Project::set_project_for_order($order->get_id(), $data['project_id']);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Data_Store {
|
||||
|
||||
public static function init() {
|
||||
|
||||
require_once dirname( __FILE__ ) . '/data-stores/class-wiaas-shop-data-store.php';
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Data_Store::init();
|
||||
@@ -17,7 +17,8 @@ class Wiaas_DB_Update {
|
||||
'201810101644700' => 'wiaas_db_import_aam_role_settings',
|
||||
'201810111644700' => 'wiaas_db_update_add_organization_info_ui_fields',
|
||||
'201810121644700' => 'wiaas_db_update_add_user_organization_ui_fields',
|
||||
'201810161644700' => 'wiaas_db_update_update_commercial_lead_capabilities'
|
||||
'201810171644700' => 'wiaas_db_setup_create_customer_commercial_lead_table',
|
||||
'201810171744702' => 'wiaas_db_update_update_commercial_lead_capabilities',
|
||||
);
|
||||
|
||||
public static function execute() {
|
||||
|
||||
@@ -45,8 +45,11 @@ class Wiaas_Order {
|
||||
* @param $order_id
|
||||
*/
|
||||
public static function assign_order_to_organization($order_id) {
|
||||
$user = wp_get_current_user();
|
||||
Wiaas_User_Organization::assign_post_to_user_organization($order_id, $user->ID);
|
||||
$customer_id = wiaas_get_current_user_organization_id();
|
||||
|
||||
$commercial_lead_id =
|
||||
|
||||
Wiaas_User_Organization::assign_post_to_organization($order_id, $customer_id);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -93,9 +93,8 @@ class Wiaas_Package {
|
||||
* @return array
|
||||
*/
|
||||
private static function _append_additional_packages($data, $package, $request) {
|
||||
// TODO: Change this so commercial lead is sent via request
|
||||
$customer_id = wiaas_get_current_user_organization_id();
|
||||
$commercial_lead_id = array_keys(wiaas_get_commercial_leads())[0];
|
||||
$commercial_lead_id = absint($request['cl_id']);
|
||||
|
||||
$data['additional_packages'] = array();
|
||||
$addons = Wiaas_Package_Addon::get_package_addons($package);
|
||||
@@ -142,9 +141,8 @@ class Wiaas_Package {
|
||||
* @return array
|
||||
*/
|
||||
private static function _append_package_prices($data, $package, $request) {
|
||||
// TODO: Change this so commercial lead is sent via request
|
||||
$customer_id = wiaas_get_current_user_organization_id();
|
||||
$commercial_lead_id = array_keys(wiaas_get_commercial_leads())[0];
|
||||
$commercial_lead_id = absint($request['cl_id']);
|
||||
|
||||
$data['prices'] = Wiaas_Pricing::get_standard_package_customer_prices($package, $customer_id, $commercial_lead_id);
|
||||
|
||||
|
||||
@@ -10,6 +10,75 @@ class Wiaas_Shop {
|
||||
|
||||
// update prices search terms for package after prices extras have been updated
|
||||
add_action('wiaas_package_prices_extras_set', array(__CLASS__, 'update_package_prices_search_terms'), 10, 4);
|
||||
|
||||
// create new shop if organization was assigned commercial lead role
|
||||
// or remove shop if commercial lead role was removed for organization
|
||||
add_action('wiaas_organization_roles_updated', array(__CLASS__, 'maybe_manage_shop_for_commercial_lead'), 10, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Link customers to shop (this will enable them to search and order packages from this shop)
|
||||
*
|
||||
* @param int $owner_id
|
||||
* @param array $customer_ids
|
||||
*/
|
||||
public static function set_shop_customers($owner_id, $customer_ids) {
|
||||
|
||||
$current_customer_ids = wp_list_pluck(
|
||||
Wiaas_Shop_Data_Store::get_shop_customers($owner_id),
|
||||
'customer_id');
|
||||
|
||||
// delete removed customers
|
||||
$removed_customer_ids = array_diff($current_customer_ids, $customer_ids);
|
||||
Wiaas_Shop_Data_Store::remove_shop_customers($owner_id, $removed_customer_ids);
|
||||
|
||||
// save added customers
|
||||
$added_customer_ids = array_diff($customer_ids, $current_customer_ids);
|
||||
Wiaas_Shop_Data_Store::add_shop_customers($owner_id, $added_customer_ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve default order type for shop
|
||||
*
|
||||
* @param int $owner_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_default_order_type($owner_id) {
|
||||
$order_type = get_term_meta(
|
||||
$owner_id,
|
||||
'_wiaas_shop_default_order_type',
|
||||
true);
|
||||
|
||||
return empty($order_type) ? 'commercial_lead' : $order_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update default order type for shop
|
||||
*
|
||||
* @param int $owner_id
|
||||
* @param string $order_type
|
||||
*/
|
||||
public static function update_default_order_type($owner_id, $order_type) {
|
||||
|
||||
if (in_array($order_type, array('commercial_lead', 'reseller'))) {
|
||||
update_term_meta($owner_id, '_wiaas_shop_default_order_type', $order_type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates shop for new shop owner (organization with commercial lead role) or
|
||||
* deletes existing shop if that role has been removed
|
||||
*
|
||||
* @param int $owner_id
|
||||
* @param array $roles
|
||||
*/
|
||||
public static function maybe_manage_shop_for_commercial_lead($owner_id, $roles) {
|
||||
$is_commercial_lead = in_array('commercial_lead', $roles);
|
||||
|
||||
$is_commercial_lead ?
|
||||
self::_maybe_create_shop($owner_id) :
|
||||
self::_maybe_remove_shop($owner_id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -21,12 +90,12 @@ class Wiaas_Shop {
|
||||
'query_var' => true,
|
||||
'rewrite' => false,
|
||||
'public' => true,
|
||||
'capabilities' => array(
|
||||
'manage_terms' => 'manage_wiaas_package_price_terms',
|
||||
'edit_terms' => 'edit_wiaas_package_price_terms',
|
||||
'delete_terms' => 'delete_wiaas_package_price_terms',
|
||||
'assign_terms' => 'assign_wiaas_package_price_terms',
|
||||
),
|
||||
// 'capabilities' => array(
|
||||
// 'manage_terms' => 'manage_wiaas_package_price_terms',
|
||||
// 'edit_terms' => 'edit_wiaas_package_price_terms',
|
||||
// 'delete_terms' => 'delete_wiaas_package_price_terms',
|
||||
// 'assign_terms' => 'assign_wiaas_package_price_terms',
|
||||
// ),
|
||||
);
|
||||
|
||||
register_taxonomy( '_wiaas_shop_prices', array( 'product' ), $args );
|
||||
@@ -68,7 +137,37 @@ class Wiaas_Shop {
|
||||
$new_terms_names = preg_filter('/^/', '_' . $owner_id . '_', $visible_price_types);
|
||||
|
||||
// create term for every visible pricing type and link them to package so package can be queried
|
||||
wp_set_object_terms($package_id, $new_terms_names, '_wiaas_shop_prices');
|
||||
wp_add_object_terms($package_id, $new_terms_names, '_wiaas_shop_prices');
|
||||
}
|
||||
|
||||
// PRIVATE
|
||||
|
||||
private static function _maybe_create_shop($owner_id) {
|
||||
$shop_name = 'wiaas_shop_' . $owner_id;
|
||||
|
||||
$attribute_id = wc_attribute_taxonomy_id_by_name($shop_name);
|
||||
|
||||
if ($attribute_id === 0) {
|
||||
// create shop attribute
|
||||
wc_create_attribute(array( 'slug' => $shop_name, 'name' => 'Catalogue' ));
|
||||
|
||||
$taxonomy_name = wc_attribute_taxonomy_name($shop_name);
|
||||
|
||||
register_taxonomy($taxonomy_name, array('product'));
|
||||
|
||||
// add default catalogue option to shop attribute
|
||||
wp_insert_term( 'Default', $taxonomy_name);
|
||||
}
|
||||
}
|
||||
|
||||
private static function _maybe_remove_shop($owner_id) {
|
||||
// get corresponding attribute for shop
|
||||
$attribute_id = wc_attribute_taxonomy_id_by_name('wiaas_shop_' . $owner_id);
|
||||
|
||||
// if shop attribute exists then remove it
|
||||
if ($attribute_id > 0) {
|
||||
wc_delete_attribute($attribute_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Shop_Data_Store {
|
||||
|
||||
/**
|
||||
* Inserts new customer for shop
|
||||
*
|
||||
* @param int $owner_id
|
||||
* @param array $customer_ids
|
||||
*/
|
||||
public static function add_shop_customers($owner_id, $customer_ids) {
|
||||
global $wpdb;
|
||||
|
||||
foreach ($customer_ids as $customer_id) {
|
||||
$wpdb->insert( $wpdb->prefix . 'wiaas_shop_customer_relationships',
|
||||
array(
|
||||
'customer_id' => $customer_id,
|
||||
'shop_owner_id' => $owner_id,
|
||||
'order_type' => Wiaas_Shop::get_default_order_type($owner_id)
|
||||
),
|
||||
array( '%d', '%d', '%s')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates order type for customer in shop
|
||||
*
|
||||
* @param int $owner_id
|
||||
* @param int $customer_id
|
||||
* @param string $order_type
|
||||
*
|
||||
* @return bool|WP_Error
|
||||
*/
|
||||
public static function update_shop_customer_order_type($owner_id, $customer_id, $order_type) {
|
||||
global $wpdb;
|
||||
|
||||
$results = $wpdb->update(
|
||||
$wpdb->prefix . 'wiaas_shop_customer_relationships',
|
||||
array( 'order_type' => $order_type ),
|
||||
array(
|
||||
'customer_id' => $customer_id,
|
||||
'shop_owner_id' => $owner_id,
|
||||
),
|
||||
array( '%s' ),
|
||||
array( '%d', '%d' )
|
||||
);
|
||||
|
||||
if (false === $results) {
|
||||
return new WP_Error('cannot_update_order_type', __( 'Could not update order type.', 'wiaas' ), array( 'status' => 400 ));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes customer from shop
|
||||
*
|
||||
* @param int $owner_id
|
||||
* @param array $customer_ids
|
||||
*/
|
||||
public static function remove_shop_customers($owner_id, $customer_ids) {
|
||||
|
||||
if (empty($customer_ids)) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$customer_ids = array_map('absint', $customer_ids);
|
||||
$customer_ids = implode(',', $customer_ids);
|
||||
|
||||
$wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM {$wpdb->prefix}wiaas_shop_customer_relationships
|
||||
WHERE shop_owner_id = %d AND customer_id IN (%s)",
|
||||
$owner_id,
|
||||
$customer_ids )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves array of customers assigned to shop
|
||||
*
|
||||
* @param int $owner_id
|
||||
*
|
||||
* @return array {
|
||||
* @type int customer_id
|
||||
* @type string order_typr
|
||||
* }
|
||||
*/
|
||||
public static function get_shop_customers($owner_id) {
|
||||
|
||||
if ($owner_id === 0) {
|
||||
return array();
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT customer_id, order_type
|
||||
FROM {$wpdb->prefix}wiaas_shop_customer_relationships
|
||||
WHERE shop_owner_id = %d",
|
||||
$owner_id )
|
||||
);
|
||||
|
||||
if ( empty( $results ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$customers = array_map(function($result_row) {
|
||||
return array(
|
||||
'customer_id' => $result_row->customer_id,
|
||||
'order_type' => $result_row->order_type
|
||||
);
|
||||
}, $results);
|
||||
|
||||
return $customers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves array of shops that are assigned to customer
|
||||
*
|
||||
* @param int $customer_id
|
||||
*
|
||||
* @return array {
|
||||
* @type int owner_id
|
||||
* @type string order_type
|
||||
* }
|
||||
*/
|
||||
public static function get_customer_shops($customer_id) {
|
||||
global $wpdb;
|
||||
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT customer_id, shop_owner_id, order_type
|
||||
FROM {$wpdb->prefix}wiaas_shop_customer_relationships
|
||||
WHERE customer_id = %d",
|
||||
$customer_id )
|
||||
);
|
||||
|
||||
if (empty($results)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$shops = array_map(function($result_row) {
|
||||
return array(
|
||||
'owner_id' => $result_row->shop_owner_id,
|
||||
'order_type' => $result_row->order_type
|
||||
);
|
||||
}, $results);
|
||||
|
||||
return $shops;
|
||||
}
|
||||
}
|
||||
@@ -125,4 +125,24 @@ function wiaas_db_setup_customer_capabilities() {
|
||||
$customer_role->add_cap('list_users');
|
||||
$customer_role->add_cap('edit_users');
|
||||
|
||||
}
|
||||
|
||||
function wiaas_db_setup_create_customer_commercial_lead_table() {
|
||||
global $wpdb;
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
|
||||
$sql = "
|
||||
CREATE TABLE {$wpdb->prefix}wiaas_shop_customer_relationships (
|
||||
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`customer_id` bigint(20) unsigned NOT NULL,
|
||||
`shop_owner_id` bigint(20) unsigned NOT NULL,
|
||||
`order_type` varchar(44) NOT NULL default '',
|
||||
PRIMARY KEY (`ID`),
|
||||
KEY `relationship` (`customer_id`, `shop_owner_id`),
|
||||
KEY `order_type` (`order_type`)
|
||||
) COLLATE {$wpdb->collate};
|
||||
";
|
||||
|
||||
dbDelta( $sql );
|
||||
}
|
||||
@@ -141,4 +141,6 @@ function wiaas_db_import_aam_role_settings() {
|
||||
function wiaas_db_update_update_commercial_lead_capabilities() {
|
||||
// add commercial lead specific roles
|
||||
wp_roles()->add_cap( 'commercial_lead', 'manage_wiaas_cl_products' ); // cl products screen
|
||||
wp_roles()->add_cap( 'commercial_lead', 'view_admin_dashboard' ); // cl products screen
|
||||
wp_roles()->add_cap( 'commercial_lead', 'manage_wiaas_cl_customers' ); // cl products screen
|
||||
}
|
||||
@@ -13,14 +13,17 @@ class Wiaas_Customer {
|
||||
|
||||
public static function get_customer_info($customer_id){
|
||||
$user = get_userdata($customer_id);
|
||||
|
||||
$organization_id = wiaas_get_user_organization_id($customer_id);
|
||||
|
||||
$result = array(
|
||||
'id' => $customer_id,
|
||||
'company_id' => self::get_customer_company_id($customer_id),
|
||||
'company_id' => $organization_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),
|
||||
'company_name' => wiaas_get_organization_name($organization_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),
|
||||
@@ -80,10 +83,6 @@ class Wiaas_Customer {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -36,6 +36,10 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
|
||||
|
||||
add_action( 'created_' . self::TAXONOMY_NAME, array( __CLASS__, 'on_organization_added' ));
|
||||
add_action( 'pre_delete_term', array( __CLASS__, 'on_taxonomy_term_will_be_deleted' ), 10, 2);
|
||||
add_action( 'delete_' . self::TAXONOMY_NAME, array( __CLASS__, 'on_organization_deleted' ));
|
||||
|
||||
add_action('acf/save_post', array(__CLASS__, 'on_organization_roles_maybe_updated'));
|
||||
|
||||
add_action('set_object_terms', array( __CLASS__, 'on_taxonomy_term_assigned' ), 10, 4);
|
||||
add_action('deleted_term_relationships', array( __CLASS__, 'on_taxonomy_term_unassigned' ), 10, 3);
|
||||
|
||||
@@ -61,6 +65,8 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
|
||||
*/
|
||||
public static function on_organization_added($organization_id) {
|
||||
self::_create_organization_access_group($organization_id);
|
||||
|
||||
do_action('wiaas_organization_created', $organization_id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,9 +79,37 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
|
||||
if ($taxonomy === self::TAXONOMY_NAME) {
|
||||
$organization_id = $term_id;
|
||||
self::_remove_organization_access_group($organization_id);
|
||||
|
||||
do_action('wiaas_organization_will_be_deleted', $organization_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes corresponding acces group when organization term is deleted
|
||||
*
|
||||
* @param $organization_id id of the organization term
|
||||
*/
|
||||
public static function on_organization_deleted($organization_id) {
|
||||
do_action('wiaas_organization_deleted', $organization_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $id acf object id for which data has been updated,
|
||||
* for organization it will be in format `term_{$organization_id}`
|
||||
*/
|
||||
public static function on_organization_roles_maybe_updated($id) {
|
||||
if ($_POST['taxonomy'] === self::TAXONOMY_NAME) {
|
||||
$roles = get_field('_wiaas_organization_roles', $id);
|
||||
|
||||
//get organization id
|
||||
$id = absint(str_replace('term_', '', $id));
|
||||
|
||||
do_action('wiaas_organization_roles_updated', $id, $roles);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Adds user to corresponding access groups when he is assigned to organization.
|
||||
* User will also be added to child organizations access groups.
|
||||
@@ -132,10 +166,9 @@ class Wiaas_User_Organization extends WP_User_Taxonomy {
|
||||
* to access order.
|
||||
*
|
||||
* @param $post_id - custom post id (product, order, ...)
|
||||
* @param $user_id
|
||||
* @param $organization_id
|
||||
*/
|
||||
public static function assign_post_to_user_organization($post_id, $user_id) {
|
||||
$organization_id = self::get_user_organization_id($user_id);
|
||||
public static function assign_post_to_organization($post_id, $organization_id) {
|
||||
self::_assign_post_to_organization($post_id, $organization_id);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ if (is_admin()) {
|
||||
include_once WIAAS_DIR . '/includes/class-wiaas-admin.php';
|
||||
}
|
||||
|
||||
include_once WIAAS_DIR . '/includes/class-wiaas-data-store.php';
|
||||
|
||||
include_once WIAAS_DIR . '/includes/class-wiaas-delivery-process.php';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user