Use order notes for comments
This commit is contained in:
@@ -23,6 +23,8 @@ class Wiaas_Delivery_Process {
|
|||||||
|
|
||||||
add_filter( 'gform_entry_meta', array(__CLASS__, 'extend_gravity_form_entry_meta'), 10, 2 );
|
add_filter( 'gform_entry_meta', array(__CLASS__, 'extend_gravity_form_entry_meta'), 10, 2 );
|
||||||
add_action( 'gravityflow_workflow_complete', array(__CLASS__, 'maybe_complete_parent_process_step'), 5, 3 );
|
add_action( 'gravityflow_workflow_complete', array(__CLASS__, 'maybe_complete_parent_process_step'), 5, 3 );
|
||||||
|
|
||||||
|
add_action( 'gravityflow_workflow_complete', array(__CLASS__, 'maybe_complete_parent_order'), 10, 3 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,6 +36,29 @@ class Wiaas_Delivery_Process {
|
|||||||
Gravity_Flow_Steps::register( new Wiaas_Delivery_Process_Step() );
|
Gravity_Flow_Steps::register( new Wiaas_Delivery_Process_Step() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maybe complete parent order for completed delivery process
|
||||||
|
* @param $entry_id
|
||||||
|
* @param $form
|
||||||
|
*/
|
||||||
|
public static function maybe_complete_parent_order($entry_id, $form) {
|
||||||
|
$entry = GFAPI::get_entry($entry_id);
|
||||||
|
|
||||||
|
$order_id = $entry['wiaas_delivery_order_id'];
|
||||||
|
|
||||||
|
if (!isset($order_id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$process_entry_id = get_post_meta($order_id, 'wiaas_delivery_process_entry_id', true);
|
||||||
|
|
||||||
|
// order process entry completed, so complete order
|
||||||
|
if (absint($process_entry_id) === $entry_id) {
|
||||||
|
$order = wc_get_order($order_id);
|
||||||
|
$order->update_status('completed', 'Completed order delivery process.', true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves delivery process instance for order
|
* Retrieves delivery process instance for order
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -18,8 +18,19 @@ class Wiaas_Order {
|
|||||||
add_filter('woocommerce_rest_prepare_shop_order_object', array(__CLASS__, 'transform_rest_order'), 999, 3);
|
add_filter('woocommerce_rest_prepare_shop_order_object', array(__CLASS__, 'transform_rest_order'), 999, 3);
|
||||||
|
|
||||||
add_filter('woocommerce_rest_orders_prepare_object_query', array( __CLASS__, 'wiaas_prepare_rest_orders_query'), 10, 2);
|
add_filter('woocommerce_rest_orders_prepare_object_query', array( __CLASS__, 'wiaas_prepare_rest_orders_query'), 10, 2);
|
||||||
|
|
||||||
|
add_filter('woocommerce_new_order_note_data', array( __CLASS__, 'update_new_order_comment_date'), 10, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function update_new_order_comment_date($comment_data, $order_data) {
|
||||||
|
$user = wp_get_current_user();
|
||||||
|
|
||||||
|
$comment_data['comment_author'] = $user->display_name;
|
||||||
|
$comment_data['comment_author_email'] = $user->user_email;
|
||||||
|
|
||||||
|
return $comment_data;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assignees order to corresponding user organization when order is created.
|
* Assignees order to corresponding user organization when order is created.
|
||||||
*
|
*
|
||||||
@@ -94,6 +105,8 @@ class Wiaas_Order {
|
|||||||
$data = self::_append_commercial_lead_info($data, $order, $request);
|
$data = self::_append_commercial_lead_info($data, $order, $request);
|
||||||
|
|
||||||
$data = self::_append_wiaas_order_details($data, $order, $request);
|
$data = self::_append_wiaas_order_details($data, $order, $request);
|
||||||
|
|
||||||
|
$data = self::_append_order_comments($data, $order, $request);
|
||||||
|
|
||||||
$response->set_data($data);
|
$response->set_data($data);
|
||||||
|
|
||||||
@@ -229,6 +242,34 @@ class Wiaas_Order {
|
|||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Append order comments if single order is requested
|
||||||
|
* @param $data
|
||||||
|
* @param $order
|
||||||
|
* @param $request
|
||||||
|
*/
|
||||||
|
private static function _append_order_comments($data, $order, $request) {
|
||||||
|
|
||||||
|
if (isset($request['id'])) {
|
||||||
|
$current_user = wp_get_current_user();
|
||||||
|
|
||||||
|
$comments = $order->get_customer_order_notes();
|
||||||
|
|
||||||
|
$data['comments'] = array();
|
||||||
|
|
||||||
|
foreach ($comments as $comment) {
|
||||||
|
$data['comments'][] = array(
|
||||||
|
'id' => $comment->comment_ID,
|
||||||
|
'content' => $comment->comment_content,
|
||||||
|
'username' => $comment->comment_author,
|
||||||
|
'date' => $comment->comment_date,
|
||||||
|
'is_owner' => $comment->comment_author === $current_user->display_name,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Wiaas_Order::init();
|
Wiaas_Order::init();
|
||||||
|
|||||||
@@ -62,27 +62,16 @@ const sendComment = () => ({
|
|||||||
type: SEND_ORDER_COMMENT
|
type: SEND_ORDER_COMMENT
|
||||||
})
|
})
|
||||||
|
|
||||||
export const addComment = (idOrder, newComment, existingComments) => {
|
export const addComment = (idOrder, newComment) => {
|
||||||
return dispatch => {
|
return dispatch => {
|
||||||
dispatch(sendComment());
|
dispatch(sendComment());
|
||||||
//TODO: get real user name and store in comment object
|
|
||||||
const newCommentObject = {
|
|
||||||
comment : newComment,
|
|
||||||
date : moment().format("Do MMM, YY"),
|
|
||||||
username: localStorage.getItem('username') || '-',
|
|
||||||
}
|
|
||||||
const concatenatedComments = (existingComments) ? existingComments.concat(newCommentObject) : [newCommentObject];
|
|
||||||
|
|
||||||
return htmlClient.fetch({
|
return htmlClient.fetch({
|
||||||
url: `${API_SERVER}/wp-json/wc/v2/orders/${idOrder}`,
|
url: `${API_SERVER}/wp-json/wc/v2/orders/${idOrder}/notes`,
|
||||||
method: 'put',
|
method: 'post',
|
||||||
data: {
|
data: {
|
||||||
meta_data: [
|
note : newComment || 'Test comment',
|
||||||
{
|
customer_note: true,
|
||||||
key:'comments',
|
|
||||||
value: JSON.stringify(concatenatedComments),
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
|||||||
@@ -18,19 +18,19 @@ class OrderComments extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addNewComment(){
|
addNewComment(){
|
||||||
this.props.dispatch(addComment(this.props.orderInfo.id, this.state.newComment, this.props.orderComments));
|
this.props.dispatch(addComment(this.props.orderInfo.id, this.state.newComment));
|
||||||
}
|
}
|
||||||
|
|
||||||
onEditorChange(newComment){
|
onEditorChange(newComment){
|
||||||
this.setState({newComment});
|
this.setState({newComment});
|
||||||
}
|
}
|
||||||
|
|
||||||
getOffset(username){
|
getOffset(isOwner){
|
||||||
return (username === localStorage.getItem('username')) ? 6 : 0;
|
return isOwner ? 6 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
getClassByOwner(username){
|
getClassByOwner(isOwner){
|
||||||
return (username === localStorage.getItem('username')) ? 'mine' : '';
|
return isOwner ? 'mine' : '';
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
@@ -46,9 +46,9 @@ class OrderComments extends Component {
|
|||||||
orderComments &&
|
orderComments &&
|
||||||
orderComments.map((orderComment, index) =>
|
orderComments.map((orderComment, index) =>
|
||||||
<Row key={'order-comment-' + index}>
|
<Row key={'order-comment-' + index}>
|
||||||
<Col xl={{size:6, offset:this.getOffset(orderComment.username)}}>
|
<Col xl={{size:6, offset:this.getOffset(orderComment.isOwner)}}>
|
||||||
<div className={'order-comment ' + this.getClassByOwner(orderComment.username)} key={'order-comment-' + index}>
|
<div className={'order-comment ' + this.getClassByOwner(orderComment.isOwner)} key={'order-comment-' + index}>
|
||||||
<div className="order-comment-header">{orderComment.username} - {orderComment.date}</div>
|
<div className="order-comment-header">{orderComment.username} - {orderComment.dateCreated}</div>
|
||||||
<div dangerouslySetInnerHTML={{__html: orderComment.comment}}></div>
|
<div dangerouslySetInnerHTML={{__html: orderComment.comment}}></div>
|
||||||
</div>
|
</div>
|
||||||
</Col>
|
</Col>
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ $link-line-height: 1.5rem;
|
|||||||
|
|
||||||
.completed {
|
.completed {
|
||||||
border-left: $border-width $production-status-color solid;
|
border-left: $border-width $production-status-color solid;
|
||||||
|
background: $production-status-color;
|
||||||
}
|
}
|
||||||
|
|
||||||
.end-of-life {
|
.end-of-life {
|
||||||
@@ -131,7 +132,7 @@ $link-line-height: 1.5rem;
|
|||||||
background: $canceled-status-color;
|
background: $canceled-status-color;
|
||||||
}
|
}
|
||||||
|
|
||||||
.production {
|
.completed {
|
||||||
background: $production-status-color;
|
background: $production-status-color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,6 @@ function formatAddress(addressObject) {
|
|||||||
return `${addressObject.address_1}, ${addressObject.city}, ${addressObject.country}, ${addressObject.postcode}`;
|
return `${addressObject.address_1}, ${addressObject.city}, ${addressObject.country}, ${addressObject.postcode}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractComments(metaDataArray){
|
|
||||||
const commentsObject = metaDataArray.find(metaDataElement => {
|
|
||||||
return metaDataElement.key === "comments";
|
|
||||||
});
|
|
||||||
return commentsObject ? JSON.parse(commentsObject.value) : [];
|
|
||||||
}
|
|
||||||
|
|
||||||
export const fromWCOrder = (WCOrder) => {
|
export const fromWCOrder = (WCOrder) => {
|
||||||
let processInfo = Object.assign({},WCOrder['delivery-process']);
|
let processInfo = Object.assign({},WCOrder['delivery-process']);
|
||||||
if (WCOrder['delivery-process']){
|
if (WCOrder['delivery-process']){
|
||||||
@@ -59,7 +52,13 @@ export const fromWCOrder = (WCOrder) => {
|
|||||||
};
|
};
|
||||||
}),
|
}),
|
||||||
process: processInfo,
|
process: processInfo,
|
||||||
comments: extractComments(WCOrder.meta_data),
|
comments: WCOrder.comments ? WCOrder.comments.map(comment => ({
|
||||||
|
id: comment.id,
|
||||||
|
comment: comment.content,
|
||||||
|
username: comment.username,
|
||||||
|
dateCreated: formatDate(comment.date),
|
||||||
|
isOwner: comment['is_owner']
|
||||||
|
})) : [],
|
||||||
deliveryAddress: formatAddress(WCOrder.shipping),
|
deliveryAddress: formatAddress(WCOrder.shipping),
|
||||||
customer: WCOrder.customer,
|
customer: WCOrder.customer,
|
||||||
commercialLead: WCOrder['commercial_lead']
|
commercialLead: WCOrder['commercial_lead']
|
||||||
|
|||||||
Reference in New Issue
Block a user