Work on wiaas orders

This commit is contained in:
Almira Krdzic
2018-08-08 12:21:26 +02:00
parent c1834f679a
commit b8653ad18b
25 changed files with 644 additions and 81 deletions

View File

@@ -0,0 +1,25 @@
<?php
/**
* Class Wiaas_Customer
*
* Handler integration of Wiaas customer with Woocommerce customer
*/
class Wiaas_Customer {
public static function init() {
add_action('woocommerce_new_customer', array(__CLASS__, 'append_customer_info'));
}
/**
* Appends Wiaas customer info (name, phone)
*
* For now we will generate these, but later will be added to the interface
*/
public static function append_customer_info($customer_id) {
update_user_meta($customer_id, 'wiaas_customer_name', 'Wiaas Customer');
update_user_meta($customer_id, 'wiaas_customer_phone', '+46 (10) 5595148');
}
}
Wiaas_Customer::init();

View File

@@ -7,7 +7,8 @@ class Wiaas_DB_Update {
private static $db_updates = array(
'20180728222206' => 'wiaas_db_update_enable_product_by_user_role',
'20180801222206' => 'wiaas_db_update_setup_gravity',
'20180802222206' => 'wiaas_db_update_add_delivery_process_forms'
'20180802222206' => 'wiaas_db_update_add_delivery_process_forms',
'20180808222206' => 'wiaas_db_update_setup_customer_capabilities'
);
public static function execute() {

View File

@@ -8,8 +8,50 @@
class Wiaas_Order {
private static $object_order_type = 'shop_order';
private static $wiaas_order_status = array(
);
public static function init() {
add_filter('woocommerce_rest_prepare_shop_order_object', array(__CLASS__, 'transform_rest_order'), 10, 3);
add_action('woocommerce_new_order', array( __CLASS__, 'assign_order_to_organization' ));
add_filter('woocommerce_rest_check_permissions', array( __CLASS__, 'check_order_access'), 10, 4);
}
/**
* Checks if current user has access to requested order/{orderId} via woocommerce REST API.
* Endpoint `/orders` is filtered correctly by groups, but endpoint `/orders/{orderId}` will return order even if
* user does not have access to it.
* Groups has general support for this using rest_prepare_{$post_type} but woocommerce api does not
* use this filter anymore. So we will just call the same function just with woocommerce filter.
*
* @param $permission
* @param $context
* @param $object_id
* @param $post_type
* @return bool
*/
public static function check_order_access($permission, $context, $object_id, $post_type) {
if ($post_type === 'shop_order' && $object_id !== 0) {
return Groups_Post_Access::user_can_read_post($object_id);
}
#return Groups_Post_Access::user_can_read_post($object_id);
return $permission;
}
/**
* Assignees order to corresponding user organization when order is created.
*
* @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);
}
/**
@@ -24,14 +66,81 @@ class Wiaas_Order {
public static function transform_rest_order($response, $order, $request) {
$data = $response->get_data();
$is_customer = wcj_is_user_role('customer');
if ($is_customer || true) {
// Format date values.
$data['date_created'] = self::_format_order_date($order->get_date_created());
$data['date_modified'] = self::_format_order_date($order->get_date_modified());
$data['date_completed'] = self::_format_order_date($order->get_date_completed());
}
# apply overrides
$data = self::_append_products_info($data, $order, $request);
$data = self::_append_order_process($data, $order, $request);
$data = self::_append_customer_info($data, $order, $request);
$data = self::_append_commercial_lead_info($data, $order, $request);
$response->set_data($data);
return $response;
}
private static function _append_commercial_lead_info($data, $order, $request) {
$data['commercial_lead'] = array(
'name' => 'Coor Service Management',
'phone' => '123456789',
'email' => 'rikard@co-ideation.com'
);
return $data;
}
private static function _append_customer_info($data, $order, $request) {
$customer_id = $data['customer_id'];
$customer_user = get_user_by('id', $customer_id);
$data['customer'] = array(
'email' => $customer_user->user_email,
'name' => $customer_user->display_name,
'phone' => get_user_meta($customer_id, 'wiaas_customer_phone', true)
);
return $data;
}
private static function _append_products_info($data, $order, $request) {
foreach ($data['line_items'] as $index => $product_line) {
# lock all products to `Purchase` payment type
$product_line['payment_type'] = 'Purchase';
# lock all products to have no service
$product_line['service_price'] = 0;
$product_line['service_contract_period'] = 0;
$product_line['max_contract_period'] = 36;
$product_line['period_unit'] = 'month';
# simplify payment for all products
$product_line['recurring_price'] = 0;
$product_line['pay_period'] = 0;
# collect status from order
$product_line['status'] = $data['status'];
$product_line['short_desc'] = $product_line['status'];
# collect completion data from order
$product_line['date_completed'] = $data['date_completed'];
$data['line_items'][$index] = $product_line;
}
return $data;
}
/**
* Append order delivery process info if single order is requested
* @param $data
@@ -49,6 +158,12 @@ class Wiaas_Order {
return $data;
}
private static function _format_order_date($date) {
$date = new WC_DateTime( $date, new DateTimeZone( 'UTC' ) );
$date->setTimezone( new DateTimeZone( wc_timezone_string() ) );
return gmdate('jS F, Y', $date->getTimestamp());
}
}
Wiaas_Order::init();

View File

@@ -0,0 +1,19 @@
<?php
add_action('init', 'wiaas_load_user_organization');
function wiaas_load_user_organization() {
if (class_exists('WP_User_Taxonomy')) {
require_once dirname( __FILE__ ) . '/user/class-wiaas-user-organization.php';
new Wiaas_User_Organization();
}
}
add_action('plugins_loaded', 'remove_default_user_groups', 30);
function remove_default_user_groups() {
remove_action( 'init', 'wp_register_default_user_group_taxonomy' );
remove_action( 'init', 'wp_register_default_user_type_taxonomy' );
}

View File

@@ -76,4 +76,12 @@ function wiaas_db_update_add_delivery_process_forms() {
}
do_action('gform_forms_post_import', $created_forms);
}
function wiaas_db_update_setup_customer_capabilities() {
$customer_role = get_role('customer');
$customer_role->add_cap('read_private_shop_orders');
$customer_role->add_cap('read_shop_order');
}

View File

@@ -0,0 +1,254 @@
<?php
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
class Wiaas_User_Organization extends WP_User_Taxonomy {
const TAXONOMY_NAME = 'wiaas-user-organization';
const TAXONOMY_SLUG = 'users/wiaas-organization';
public function __construct()
{
$args = array(
'singular' => __('Organization', 'wp-user-groups'),
'plural' => __('Organizations', 'wp-user-groups'),
'exclusive' => true,
'public' => true,
'show_in_rest' => true,
'rest_base' => 'organization'
);
$labels = array();
$caps = array();
parent::__construct(self::TAXONOMY_NAME, self::TAXONOMY_SLUG, $args, $labels, $caps);
$this->hooks();
}
/**
* Add organization specific hooks
*/
function hooks() {
parent::hooks();
add_action('user_new_form', array( $this, 'show_organizations_selection' ));
add_action('user_register', array( $this, 'save_terms_for_user' ));
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('set_object_terms', array( __CLASS__, 'on_taxonomy_term_assigned' ), 10, 4);
add_action('deleted_term_relationships', array( __CLASS__, 'on_taxonomy_term_unassigned' ), 10, 3);
}
// hooks functions
/**
* Creates corresponding access group for newly created organizational term
*
* @param $organization_id id of the organization term
*/
public static function on_organization_added($organization_id) {
self::_create_organization_access_group($organization_id);
}
/**
* Removes corresponding acces group when organization term is deleted
*
* @param $term_id - term id that will be deleted
* @param $taxonomy - taxonomy to which term belongs (in our case `user-organizations`)
*/
public static function on_taxonomy_term_will_be_deleted($term_id, $taxonomy) {
if ($taxonomy === self::TAXONOMY_NAME) {
$organization_id = $term_id;
self::_remove_organization_access_group($organization_id);
}
}
/**
* Adds user to corresponding access groups when he is assigned to organization.
* User will also be added to child organizations access groups.
*
* @param $object_id - id of object to which term is assigned (in our case $user_id)
* @param $terms - assigned terms (in our case $organizations)
* @param $tt_ids - assigned terms ids (in our case $organizations_ids)
* @param $taxonomy - taxonomy to which term belongs (in our case `user-organizations`)
*/
public static function on_taxonomy_term_assigned($object_id, $terms, $tt_ids, $taxonomy) {
if ($taxonomy === self::TAXONOMY_NAME) {
$user_id = $object_id;
$organization_id = $tt_ids[0];
add_user_meta($user_id, 'organization_id', $organization_id, true);
self::_add_user_to_access_group($user_id, $organization_id);
}
}
/**
* Removes user from corresponding access groups when he is removed from organization.
* User will also be removed from child organizations access groups.
*
* @param $object_id - id of object to which term is assigned (in our case $user_id)
* @param $tt_ids - assigned terms ids (in our case $organizations_ids)
* @param $taxonomy - taxonomy to which term belongs (in our case `user-organizations`)
*/
public static function on_taxonomy_term_unassigned($object_id, $tt_ids, $taxonomy) {
if ($taxonomy === self::TAXONOMY_NAME) {
$user_id = $object_id;
$organization_id = $tt_ids[0];
delete_user_meta($user_id, 'organization_id');
self::_remove_user_from_organization_access_groups($user_id, $organization_id);
}
}
/**
* Retrieves user organization based on user id
*
* @param null $user_id
* @return mixed
*/
public static function get_user_organization($user_id = null) {
if (!isset($user_id)) {
$user = wp_get_current_user();
$user_id = $user->ID;
}
$terms = wp_get_object_terms($user_id, self::TAXONOMY_NAME);
return $terms[0];
}
/**
* Assignees post to user organization. Post will be assigned to corresponding access groups.
* If user organization has parent organizations, staff from parent organizations will also be able
* to access order.
*
* @param $post_id - custom post id (product, order, ...)
* @param $user_id
*/
public static function assign_post_to_user_organization($post_id, $user_id) {
$organization = self::get_user_organization($user_id);
self::_assign_post_to_organization($post_id, $organization->term_id);
}
// private helper functions
/**
* Retrieves organization object based organization id
*
* @param $organization_id
* @return mixed
*/
private static function _get_organization_access_group_id($organization_id) {
return get_term_meta($organization_id, 'group_id', true);
}
/**
* Retrieves all access groups ids for organization. This includes corresponding access group
* for provided organization and also access groups for all of its child organizations.
*
* @param $organization_id
* @return array
*/
private static function _get_organization_all_access_groups_ids($organization_id) {
$access_groups_ids = array();
$access_groups_ids[] = self::_get_organization_access_group_id($organization_id);
$organization_departments_ids = self::_get_organization_departments_ids($organization_id);
foreach ($organization_departments_ids as $organization_department_id) {
$access_groups_ids[] = self::_get_organization_access_group_id($organization_department_id);
}
return $access_groups_ids;
}
/**
* Retrieves all departments of organization
*
* @param $organization_id
* @return array|WP_Error
*/
private static function _get_organization_departments_ids($organization_id) {
return get_term_children($organization_id, self::TAXONOMY_NAME);
}
/**
* Assign custom post to corresponding organizational acccess group.
*
* @param $post_id
* @param $organization_id
*/
private static function _assign_post_to_organization($post_id, $organization_id) {
if (class_exists('Groups_Post_Access')) {
$access_group_id = self::_get_organization_access_group_id($organization_id);
Groups_Post_Access::update( array( 'post_id' => $post_id, 'groups_read' => [$access_group_id] ) );
}
}
/**
* Create corresponding access group for organization
*
* @param $organization_id
*/
private static function _create_organization_access_group($organization_id) {
if (class_exists('Groups_Group')) {
$organization = get_term_by('id', $organization_id, self::TAXONOMY_NAME);
$access_group_id = Groups_Group::create(array(
'name' => $organization->name,
));
add_term_meta($organization_id, 'group_id', $access_group_id);
}
}
/**
* Remove corresponding access group for organization
*
* @param $organization_id
*/
private static function _remove_organization_access_group($organization_id) {
if (class_exists('Groups_Group')) {
$access_group_id = self::_get_organization_access_group_id($organization_id);
Groups_Group::delete($access_group_id);
}
}
/**
* Add user to all access groups found in provided organization.
*
* @param $user_id
* @param $organization_id
*/
private static function _add_user_to_access_group($user_id, $organization_id) {
if (class_exists('Groups_User_Group')) {
$access_groups_ids = self::_get_organization_all_access_groups_ids($organization_id);
foreach ($access_groups_ids as $access_group_id) {
Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $access_group_id ) );
}
}
}
/**
* Remove user from all access groups found in provided organization,
*
* @param $user_id
* @param $organization_id
*/
private static function _remove_user_from_organization_access_groups($user_id, $organization_id) {
if (class_exists('Groups_User_Group')) {
$access_groups_ids = self::_get_organization_all_access_groups_ids($organization_id);
foreach ($access_groups_ids as $access_group_id) {
Groups_User_Group::delete($user_id, $access_group_id);
}
}
}
/**
* Show organizations selection without $user info
*/
function show_organizations_selection() {
$terms = get_terms( array(
'taxonomy' => self::TAXONOMY_NAME,
) );
$taxonomy = get_taxonomy(self::TAXONOMY_NAME);
$this->table_contents(null, $taxonomy, $terms);
}
}

View File

@@ -26,4 +26,8 @@ include_once WIAAS_DIR . '/includes/class-wiaas-delivery-process.php';
include_once WIAAS_DIR . '/includes/class-wiaas-order.php';
include_once WIAAS_DIR . '/includes/class-wiaas-customer.php';
include_once WIAAS_DIR . '/includes/class-wiaas-user.php';
include_once WIAAS_DIR . '/includes/class-wiaas-api.php';