Handle additional orders info for wiaas
This commit is contained in:
@@ -10,18 +10,27 @@ 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);
|
||||
|
||||
add_filter('woocommerce_rest_prepare_shop_order_object', array(__CLASS__, 'transform_rest_order'), 10, 3);
|
||||
|
||||
add_filter('woocommerce_rest_orders_prepare_object_query', array( __CLASS__, 'wiaas_prepare_rest_orders_query'), 10, 2);
|
||||
|
||||
add_filter('wc_order_statuses', array( __CLASS__, 'wiaas_order_statuses'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if current user has access to requested order/{orderId} via woocommerce REST API.
|
||||
@@ -37,25 +46,36 @@ class Wiaas_Order {
|
||||
* @return bool
|
||||
*/
|
||||
public static function check_order_access($permission, $context, $object_id, $post_type) {
|
||||
if ($post_type === 'shop_order' && $object_id !== 0) {
|
||||
if ($post_type === self::$object_order_type && $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.
|
||||
* Handles custom wiaas arguments to woocommerce orders api endpoint `wc/v2/orders`
|
||||
* @param $args
|
||||
* @param $request
|
||||
*
|
||||
* @param $order_id
|
||||
* @return mixed
|
||||
*/
|
||||
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);
|
||||
public static function wiaas_prepare_rest_orders_query($args, $request) {
|
||||
|
||||
# Handle wiaas_is_active flag
|
||||
if (isset($request['wiaas_is_active'])) {
|
||||
if ($request['wiaas_is_active'] === '1') {
|
||||
$args['post_status'] = array('wc-open', 'wc-processing');
|
||||
}
|
||||
if ($request['wiaas_is_active'] === '0') {
|
||||
$args['post_status'] = array('wc-completed', 'wc-cancelled');
|
||||
}
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply wiaas custome tranformation on retrieved JSON order object
|
||||
* Apply wiaas custom transformation on retrieved JSON order object
|
||||
*
|
||||
* @param $response
|
||||
* @param $order
|
||||
@@ -66,15 +86,6 @@ 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);
|
||||
|
||||
@@ -89,6 +100,18 @@ class Wiaas_Order {
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* PRIVATE
|
||||
*/
|
||||
|
||||
/**
|
||||
* Appends additional wiaas customer lead info to order json response
|
||||
* @param $data
|
||||
* @param $order
|
||||
* @param $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private static function _append_commercial_lead_info($data, $order, $request) {
|
||||
|
||||
$data['commercial_lead'] = array(
|
||||
@@ -100,20 +123,40 @@ class Wiaas_Order {
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends additional wiaas customer info to order json response
|
||||
* @param $data
|
||||
* @param $order
|
||||
* @param $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private static function _append_customer_info($data, $order, $request) {
|
||||
|
||||
$current_user = wp_get_current_user();
|
||||
|
||||
$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)
|
||||
'phone' => '+46 (10) 5595148'
|
||||
);
|
||||
|
||||
$data['is_my_order'] = $customer_id === $current_user->ID;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends additional wiaas products info to order json response
|
||||
* @param $data
|
||||
* @param $order
|
||||
* @param $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private static function _append_products_info($data, $order, $request) {
|
||||
foreach ($data['line_items'] as $index => $product_line) {
|
||||
# lock all products to `Purchase` payment type
|
||||
@@ -130,7 +173,13 @@ class Wiaas_Order {
|
||||
$product_line['pay_period'] = 0;
|
||||
|
||||
# collect status from order
|
||||
$product_line['status'] = $data['status'];
|
||||
if ($data['status'] === 'completed') {
|
||||
$product_line['status'] = 'production';
|
||||
} else if ($data['status'] === 'cancelled') {
|
||||
$product_line['status'] = 'cancelled';
|
||||
} else {
|
||||
$product_line['status'] = 'processing';
|
||||
}
|
||||
$product_line['short_desc'] = $product_line['status'];
|
||||
|
||||
# collect completion data from order
|
||||
@@ -158,12 +207,6 @@ 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();
|
||||
Reference in New Issue
Block a user