2018-06-07 16:05:22 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2018-08-01 19:07:17 +00:00
|
|
|
import { Button } from '@wordpress/components';
|
2019-04-15 07:06:34 +00:00
|
|
|
import { Component, Fragment } from '@wordpress/element';
|
2018-11-30 16:12:34 +00:00
|
|
|
import { compose } from '@wordpress/compose';
|
2019-04-15 07:06:34 +00:00
|
|
|
import Gridicon from 'gridicons';
|
2018-06-07 16:05:22 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-07-09 15:46:31 +00:00
|
|
|
import { noop } from 'lodash';
|
2018-11-30 20:45:44 +00:00
|
|
|
import interpolateComponents from 'interpolate-components';
|
2018-06-07 16:05:22 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-30 18:57:48 +00:00
|
|
|
* WooCommerce dependencies
|
2018-06-07 16:05:22 +00:00
|
|
|
*/
|
2018-08-20 21:18:13 +00:00
|
|
|
import {
|
|
|
|
EllipsisMenu,
|
2018-11-30 22:13:05 +00:00
|
|
|
EmptyContent,
|
2018-08-20 21:18:13 +00:00
|
|
|
Flag,
|
2018-11-30 20:45:44 +00:00
|
|
|
Link,
|
2018-08-20 21:18:13 +00:00
|
|
|
MenuTitle,
|
|
|
|
MenuItem,
|
|
|
|
OrderStatus,
|
2018-09-21 15:19:05 +00:00
|
|
|
Section,
|
2018-08-20 21:18:13 +00:00
|
|
|
} from '@woocommerce/components';
|
2018-10-30 18:57:48 +00:00
|
|
|
import { formatCurrency, getCurrencyFormatDecimal } from '@woocommerce/currency';
|
2019-02-20 01:51:43 +00:00
|
|
|
import { getAdminLink, getNewPath } from '@woocommerce/navigation';
|
2018-10-30 18:57:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2018-11-30 21:47:04 +00:00
|
|
|
import { ActivityCard, ActivityCardPlaceholder } from '../activity-card';
|
2018-10-30 18:57:48 +00:00
|
|
|
import ActivityHeader from '../activity-header';
|
|
|
|
import ActivityOutboundLink from '../activity-outbound-link';
|
2019-03-22 08:48:20 +00:00
|
|
|
import { DEFAULT_ACTIONABLE_STATUSES, QUERY_DEFAULTS } from 'wc-api/constants';
|
2018-12-03 15:59:53 +00:00
|
|
|
import withSelect from 'wc-api/with-select';
|
2018-06-07 16:05:22 +00:00
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
class OrdersPanel extends Component {
|
|
|
|
renderEmptyCard() {
|
|
|
|
const { hasNonActionableOrders } = this.props;
|
|
|
|
if ( hasNonActionableOrders ) {
|
2019-03-15 01:26:16 +00:00
|
|
|
return (
|
2019-04-15 07:06:34 +00:00
|
|
|
<ActivityCard
|
|
|
|
className="woocommerce-empty-review-activity-card"
|
|
|
|
title={ __( 'You have no orders to fulfill', 'woocommerce-admin' ) }
|
|
|
|
icon={ <Gridicon icon="checkmark" size={ 48 } /> }
|
|
|
|
>
|
|
|
|
{ __( "Good job, you've fulfilled all of your new orders!", 'woocommerce-admin' ) }
|
|
|
|
</ActivityCard>
|
2019-03-15 01:26:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
return (
|
|
|
|
<ActivityCard
|
|
|
|
className="woocommerce-empty-review-activity-card"
|
|
|
|
title={ __( 'You have no orders to fulfill', 'woocommerce-admin' ) }
|
|
|
|
icon={ <Gridicon icon="time" size={ 48 } /> }
|
|
|
|
actions={
|
|
|
|
<Button
|
|
|
|
href="https://docs.woocommerce.com/document/managing-orders/"
|
|
|
|
isDefault
|
|
|
|
target="_blank"
|
|
|
|
>
|
|
|
|
{ __( 'Learn more', 'woocommerce-admin' ) }
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
"You're still waiting for your customers to make their first orders. " +
|
|
|
|
'While you wait why not learn how to manage orders?',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
</ActivityCard>
|
2019-03-13 17:14:02 +00:00
|
|
|
);
|
2019-04-15 07:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
renderOrders() {
|
|
|
|
const { orders } = this.props;
|
|
|
|
|
|
|
|
if ( orders.length === 0 ) {
|
|
|
|
return this.renderEmptyCard();
|
|
|
|
}
|
|
|
|
|
|
|
|
const getCustomerString = order => {
|
|
|
|
const extended_info = order.extended_info || {};
|
|
|
|
const { first_name, last_name } = extended_info.customer || {};
|
|
|
|
|
|
|
|
if ( ! first_name && ! last_name ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = [ first_name, last_name ].join( ' ' );
|
|
|
|
return sprintf(
|
|
|
|
__(
|
|
|
|
/* translators: describes who placed an order, e.g. Order #123 placed by John Doe */
|
|
|
|
'placed by {{customerLink}}%(customerName)s{{/customerLink}}',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
{
|
|
|
|
customerName: name,
|
|
|
|
}
|
|
|
|
);
|
2018-11-30 22:13:05 +00:00
|
|
|
};
|
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
const orderCardTitle = order => {
|
|
|
|
const { extended_info, order_id } = order;
|
|
|
|
const { customer } = extended_info || {};
|
|
|
|
const customerUrl = customer.customer_id
|
|
|
|
? getNewPath( {}, '/analytics/customers', {
|
|
|
|
filter: 'single_customer',
|
|
|
|
customers: customer.customer_id,
|
|
|
|
} )
|
|
|
|
: null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{ interpolateComponents( {
|
|
|
|
mixedString: sprintf(
|
|
|
|
__(
|
|
|
|
'Order {{orderLink}}#%(orderNumber)s{{/orderLink}} %(customerString)s {{destinationFlag/}}',
|
|
|
|
'woocommerce-admin'
|
|
|
|
),
|
|
|
|
{
|
|
|
|
orderNumber: order_id,
|
|
|
|
customerString: getCustomerString( order ),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
components: {
|
|
|
|
orderLink: <Link href={ 'post.php?action=edit&post=' + order_id } type="wp-admin" />,
|
|
|
|
destinationFlag: customer.country ? (
|
|
|
|
<Flag code={ customer.country } round={ false } />
|
|
|
|
) : null,
|
|
|
|
customerLink: customerUrl ? <Link href={ customerUrl } type="wc-admin" /> : <span />,
|
|
|
|
},
|
|
|
|
} ) }
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const cards = [];
|
|
|
|
orders.forEach( order => {
|
|
|
|
const extended_info = order.extended_info || {};
|
|
|
|
const productsCount =
|
|
|
|
extended_info && extended_info.products ? extended_info.products.length : 0;
|
|
|
|
|
|
|
|
const total = order.gross_total;
|
|
|
|
const refundValue = order.refund_total;
|
|
|
|
const remainingTotal = getCurrencyFormatDecimal( total ) + refundValue;
|
|
|
|
|
|
|
|
cards.push(
|
|
|
|
<ActivityCard
|
|
|
|
key={ order.order_id }
|
|
|
|
className="woocommerce-order-activity-card"
|
|
|
|
title={ orderCardTitle( order ) }
|
|
|
|
date={ order.date_created_gmt }
|
|
|
|
subtitle={
|
|
|
|
<div>
|
|
|
|
<span>
|
|
|
|
{ sprintf(
|
|
|
|
_n( '%d product', '%d products', productsCount, 'woocommerce-admin' ),
|
|
|
|
productsCount
|
|
|
|
) }
|
|
|
|
</span>
|
|
|
|
{ refundValue ? (
|
|
|
|
<span>
|
|
|
|
<s>{ formatCurrency( total ) }</s> { formatCurrency( remainingTotal ) }
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<span>{ formatCurrency( total ) }</span>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
actions={
|
|
|
|
<Button
|
|
|
|
isDefault
|
|
|
|
href={ getAdminLink( 'post.php?action=edit&post=' + order.order_id ) }
|
|
|
|
>
|
|
|
|
{ __( 'Begin fulfillment' ) }
|
|
|
|
</Button>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<OrderStatus order={ order } />
|
|
|
|
</ActivityCard>
|
|
|
|
);
|
|
|
|
} );
|
2018-11-30 22:13:05 +00:00
|
|
|
return (
|
|
|
|
<Fragment>
|
2019-04-15 07:06:34 +00:00
|
|
|
{ cards }
|
|
|
|
<ActivityOutboundLink href={ 'edit.php?post_type=shop_order' }>
|
|
|
|
{ __( 'Manage all orders', 'woocommerce-admin' ) }
|
|
|
|
</ActivityOutboundLink>
|
2018-11-30 22:13:05 +00:00
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
render() {
|
|
|
|
const { orders, isRequesting, isError, orderStatuses } = this.props;
|
2018-07-09 15:46:31 +00:00
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
if ( isError ) {
|
|
|
|
if ( ! orderStatuses.length ) {
|
|
|
|
return (
|
|
|
|
<EmptyContent
|
|
|
|
title={ __(
|
|
|
|
"You currently don't have any actionable statuses. " +
|
|
|
|
'To display orders here, select orders that require further review in settings.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
) }
|
|
|
|
actionLabel={ __( 'Settings', 'woocommerce-admin' ) }
|
|
|
|
actionURL={ getAdminLink( 'admin.php?page=wc-admin#/analytics/settings' ) }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2019-02-20 01:51:43 +00:00
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
const title = __(
|
|
|
|
'There was an error getting your orders. Please try again.',
|
|
|
|
'woocommerce-admin'
|
|
|
|
);
|
|
|
|
const actionLabel = __( 'Reload', 'woocommerce-admin' );
|
|
|
|
const actionCallback = () => {
|
|
|
|
// @todo Add tracking for how often an error is displayed, and the reload action is clicked.
|
|
|
|
window.location.reload();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<EmptyContent
|
|
|
|
title={ title }
|
|
|
|
actionLabel={ actionLabel }
|
|
|
|
actionURL={ null }
|
|
|
|
actionCallback={ actionCallback }
|
|
|
|
/>
|
|
|
|
</Fragment>
|
|
|
|
);
|
2019-02-20 01:51:43 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
const menu = (
|
|
|
|
<EllipsisMenu label="Demo Menu">
|
|
|
|
<MenuTitle>Test</MenuTitle>
|
|
|
|
<MenuItem onInvoke={ noop }>Test</MenuItem>
|
|
|
|
</EllipsisMenu>
|
2019-02-20 01:51:43 +00:00
|
|
|
);
|
2019-04-15 07:06:34 +00:00
|
|
|
|
|
|
|
const title =
|
|
|
|
isRequesting || orders.length
|
|
|
|
? __( 'Orders', 'woocommerce-admin' )
|
|
|
|
: __( 'No orders to ship', 'woocommerce-admin' );
|
2018-11-30 20:45:44 +00:00
|
|
|
|
2018-07-16 16:28:26 +00:00
|
|
|
return (
|
2018-11-30 20:45:44 +00:00
|
|
|
<Fragment>
|
2019-04-15 07:06:34 +00:00
|
|
|
<ActivityHeader title={ title } menu={ menu } />
|
|
|
|
<Section>
|
|
|
|
{ isRequesting ? (
|
|
|
|
<ActivityCardPlaceholder
|
|
|
|
className="woocommerce-order-activity-card"
|
|
|
|
hasAction
|
|
|
|
hasDate
|
|
|
|
lines={ 2 }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
this.renderOrders()
|
|
|
|
) }
|
|
|
|
</Section>
|
2018-11-30 20:45:44 +00:00
|
|
|
</Fragment>
|
2018-07-16 16:28:26 +00:00
|
|
|
);
|
2019-04-15 07:06:34 +00:00
|
|
|
}
|
2018-06-07 16:05:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 15:46:31 +00:00
|
|
|
OrdersPanel.propTypes = {
|
2019-02-20 01:51:43 +00:00
|
|
|
orders: PropTypes.array.isRequired,
|
2018-11-30 16:12:34 +00:00
|
|
|
isError: PropTypes.bool,
|
|
|
|
isRequesting: PropTypes.bool,
|
2018-06-07 16:05:22 +00:00
|
|
|
};
|
|
|
|
|
2018-08-07 20:11:30 +00:00
|
|
|
OrdersPanel.defaultProps = {
|
2019-02-20 01:51:43 +00:00
|
|
|
orders: [],
|
2018-11-30 16:12:34 +00:00
|
|
|
isError: false,
|
|
|
|
isRequesting: false,
|
2018-08-07 20:11:30 +00:00
|
|
|
};
|
|
|
|
|
2018-11-30 16:12:34 +00:00
|
|
|
export default compose(
|
2019-03-19 10:57:11 +00:00
|
|
|
withSelect( ( select, props ) => {
|
2019-04-15 07:06:34 +00:00
|
|
|
const { hasActionableOrders } = props;
|
2019-02-20 01:51:43 +00:00
|
|
|
const { getReportItems, getReportItemsError, isReportItemsRequesting } = select( 'wc-api' );
|
2019-03-22 08:48:20 +00:00
|
|
|
const orderStatuses =
|
|
|
|
wcSettings.wcAdminSettings.woocommerce_actionable_order_statuses ||
|
|
|
|
DEFAULT_ACTIONABLE_STATUSES;
|
2019-03-19 10:57:11 +00:00
|
|
|
|
|
|
|
if ( ! orderStatuses.length ) {
|
|
|
|
return { orders: [], isError: true, isRequesting: false, orderStatuses };
|
|
|
|
}
|
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
if ( hasActionableOrders ) {
|
|
|
|
const ordersQuery = {
|
|
|
|
page: 1,
|
|
|
|
per_page: QUERY_DEFAULTS.pageSize,
|
|
|
|
status_is: orderStatuses,
|
|
|
|
extended_info: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const orders = getReportItems( 'orders', ordersQuery ).data;
|
|
|
|
const isError = Boolean( getReportItemsError( 'orders', ordersQuery ) );
|
|
|
|
const isRequesting = isReportItemsRequesting( 'orders', ordersQuery );
|
|
|
|
|
|
|
|
return { orders, isError, isRequesting, orderStatuses };
|
2019-03-19 10:57:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
const allOrdersQuery = {
|
2018-11-30 16:12:34 +00:00
|
|
|
page: 1,
|
2019-04-15 07:06:34 +00:00
|
|
|
per_page: 0,
|
2018-11-30 16:12:34 +00:00
|
|
|
};
|
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
const totalNonActionableOrders = getReportItems( 'orders', allOrdersQuery ).totalResults;
|
|
|
|
const isError = Boolean( getReportItemsError( 'orders', allOrdersQuery ) );
|
|
|
|
const isRequesting = isReportItemsRequesting( 'orders', allOrdersQuery );
|
2018-11-30 16:12:34 +00:00
|
|
|
|
2019-04-15 07:06:34 +00:00
|
|
|
return {
|
|
|
|
hasNonActionableOrders: totalNonActionableOrders > 0,
|
|
|
|
isError,
|
|
|
|
isRequesting,
|
|
|
|
orderStatuses,
|
|
|
|
};
|
2018-11-30 16:12:34 +00:00
|
|
|
} )
|
|
|
|
)( OrdersPanel );
|