Handle additional orders info for wiaas

This commit is contained in:
Almira Krdzic
2018-08-09 12:20:20 +02:00
46 changed files with 381 additions and 364 deletions

View File

@@ -51,10 +51,10 @@ class Wiass_REST_Delivery_Process_API {
foreach ($entries as $entry) {
$step = gravity_flow()->get_step( $entry['workflow_step'] );
$data[] = array(
'idOrder' => $entry['wiaas_delivery_order_id'],
'orderNumber' => $entry['wiaas_delivery_order_id'],
'order_id' => $entry['wiaas_delivery_order_id'],
'order_number' => $entry['wiaas_delivery_order_id'],
'status' => $entry['workflow_final_status'],
'stepAction' => $step->get_name(),
'step_action' => $step->get_name(),
);
}

View File

@@ -1,25 +0,0 @@
<?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

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

View File

@@ -159,4 +159,4 @@ class Wiaas_Delivery_Process {
}
}
add_action( 'gravityflow_loaded', array('Wiaas_Delivery_Process', 'init') );
add_action( 'gravityflow_loaded', array('Wiaas_Delivery_Process', 'init') );

View File

@@ -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();

View File

@@ -1,19 +1,29 @@
<?php
add_action('init', 'wiaas_load_user_organization');
defined( 'ABSPATH' ) || exit;
function wiaas_load_user_organization() {
if (class_exists('WP_User_Taxonomy')) {
require_once dirname( __FILE__ ) . '/user/class-wiaas-user-organization.php';
/**
* Class Wiaas_User
*/
class Wiaas_User {
new Wiaas_User_Organization();
public static function init() {
add_action('init', array(__CLASS__, 'load_user_organization'));
add_action('plugins_loaded', array(__CLASS__, 'remove_default_user_groups'), 30);
}
public static function load_user_organization() {
if (class_exists('WP_User_Taxonomy')) {
require_once dirname( __FILE__ ) . '/user/class-wiaas-user-organization.php';
new Wiaas_User_Organization();
}
}
public static function remove_default_user_groups() {
remove_action( 'init', 'wp_register_default_user_group_taxonomy' );
remove_action( 'init', 'wp_register_default_user_type_taxonomy' );
}
}
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' );
}
Wiaas_User::init();

View File

@@ -78,10 +78,9 @@ 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 = get_role('customer');
$customer_role->add_cap('read_private_shop_orders');
$customer_role->add_cap('read_shop_order');
$customer_role->add_cap('read_private_shop_orders');
$customer_role->add_cap('read_shop_order');
}

View File

@@ -246,4 +246,4 @@ class Wiaas_Delivery_Process_Step extends Gravity_Flow_Step {
return absint($value);
}
}
}

View File

@@ -42,14 +42,14 @@ class Wiass_REST_Delivery_Process_Api_Test extends Wiaas_Unit_Test_Case {
$this->assertTrue(is_array($pending_step));
$this->assertArrayHasKey('idOrder', $pending_step);
$this->assertArrayHasKey('orderNumber', $pending_step);
$this->assertArrayHasKey('order_id', $pending_step);
$this->assertArrayHasKey('order_number', $pending_step);
$this->assertArrayHasKey('status', $pending_step);
$this->assertArrayHasKey('stepAction', $pending_step);
$this->assertArrayHasKey('step_action', $pending_step);
$this->assertEquals($pending_step['idOrder'], $this->order_id);
$this->assertEquals($pending_step['orderNumber'], $this->order_id);
$this->assertEquals($pending_step['order_id'], $this->order_id);
$this->assertEquals($pending_step['order_number'], $this->order_id);
$this->assertEquals($pending_step['status'], 'pending');
$this->assertNotEmpty($pending_step['stepAction']);
$this->assertNotEmpty($pending_step['step_action']);
}
}

View File

@@ -26,8 +26,6 @@ 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';