2020-11-06 01:21:05 +00:00
|
|
|
|
/**
|
|
|
|
|
* External dependencies
|
|
|
|
|
*/
|
|
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2022-06-01 17:15:09 +00:00
|
|
|
|
import { useMemo, useContext } from '@wordpress/element';
|
2021-07-23 12:47:23 +00:00
|
|
|
|
import { useSelect } from '@wordpress/data';
|
2024-01-19 04:20:36 +00:00
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
import PropTypes from 'prop-types';
|
2022-02-21 02:34:25 +00:00
|
|
|
|
import interpolateComponents from '@automattic/interpolate-components';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
import {
|
|
|
|
|
EmptyContent,
|
|
|
|
|
Flag,
|
|
|
|
|
H,
|
|
|
|
|
Link,
|
|
|
|
|
OrderStatus,
|
|
|
|
|
Section,
|
|
|
|
|
} from '@woocommerce/components';
|
|
|
|
|
import { getNewPath } from '@woocommerce/navigation';
|
2022-01-06 12:53:30 +00:00
|
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
2022-06-01 17:15:09 +00:00
|
|
|
|
import { ORDERS_STORE_NAME, ITEMS_STORE_NAME } from '@woocommerce/data';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
2024-01-19 04:20:36 +00:00
|
|
|
|
import { CurrencyContext, CurrencyFactory } from '@woocommerce/currency';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal dependencies
|
|
|
|
|
*/
|
|
|
|
|
import {
|
|
|
|
|
ActivityCard,
|
|
|
|
|
ActivityCardPlaceholder,
|
2021-12-14 16:56:42 +00:00
|
|
|
|
} from '~/activity-panel/activity-card';
|
2022-01-06 12:53:30 +00:00
|
|
|
|
import { getAdminSetting } from '~/utils/admin-settings';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
import './style.scss';
|
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
function recordOrderEvent( eventName ) {
|
|
|
|
|
recordEvent( `activity_panel_orders_${ eventName }`, {} );
|
|
|
|
|
}
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
const renderEmptyCard = () => {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<ActivityCard
|
|
|
|
|
className="woocommerce-empty-activity-card"
|
|
|
|
|
title=""
|
|
|
|
|
icon=""
|
|
|
|
|
>
|
|
|
|
|
<span
|
|
|
|
|
className="woocommerce-order-empty__success-icon"
|
|
|
|
|
role="img"
|
|
|
|
|
aria-labelledby="woocommerce-order-empty-message"
|
2020-11-06 01:21:05 +00:00
|
|
|
|
>
|
2021-07-23 12:47:23 +00:00
|
|
|
|
🎉
|
|
|
|
|
</span>
|
|
|
|
|
<H id="woocommerce-order-empty-message">
|
2022-03-30 09:00:04 +00:00
|
|
|
|
{ __( 'You’ve fulfilled all your orders', 'woocommerce' ) }
|
2021-07-23 12:47:23 +00:00
|
|
|
|
</H>
|
|
|
|
|
</ActivityCard>
|
|
|
|
|
<Link
|
|
|
|
|
href={ 'edit.php?post_type=shop_order' }
|
|
|
|
|
onClick={ () => recordOrderEvent( 'orders_manage' ) }
|
|
|
|
|
className="woocommerce-layout__activity-panel-outbound-link woocommerce-layout__activity-panel-empty"
|
|
|
|
|
type="wp-admin"
|
|
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
|
{ __( 'Manage all orders', 'woocommerce' ) }
|
2021-07-23 12:47:23 +00:00
|
|
|
|
</Link>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-01 17:15:09 +00:00
|
|
|
|
function renderOrders( orders, customers, getFormattedOrderTotal ) {
|
2021-07-23 12:47:23 +00:00
|
|
|
|
if ( orders.length === 0 ) {
|
|
|
|
|
return renderEmptyCard();
|
2020-11-06 01:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const getCustomerString = ( customer ) => {
|
|
|
|
|
const { name } = customer || {};
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2022-06-01 17:15:09 +00:00
|
|
|
|
if ( ! name ) {
|
2021-07-23 12:47:23 +00:00
|
|
|
|
return '';
|
2020-11-06 01:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
return `{{customerLink}}${ name }{{/customerLink}}`;
|
|
|
|
|
};
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
const orderCardTitle = ( order ) => {
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const {
|
|
|
|
|
id: orderId,
|
|
|
|
|
number: orderNumber,
|
|
|
|
|
customer_id: customerId,
|
|
|
|
|
} = order;
|
|
|
|
|
const customer =
|
|
|
|
|
customers.find( ( c ) => c.user_id === customerId ) || {};
|
2021-07-23 12:47:23 +00:00
|
|
|
|
let customerUrl = null;
|
2022-06-01 17:15:09 +00:00
|
|
|
|
if ( customer && customer.id ) {
|
2021-07-23 12:47:23 +00:00
|
|
|
|
customerUrl = window.wcAdminFeatures.analytics
|
2020-11-06 01:21:05 +00:00
|
|
|
|
? getNewPath( {}, '/analytics/customers', {
|
|
|
|
|
filter: 'single_customer',
|
2022-06-01 17:15:09 +00:00
|
|
|
|
customers: customer.id,
|
2020-11-06 01:21:05 +00:00
|
|
|
|
} )
|
2022-06-01 17:15:09 +00:00
|
|
|
|
: getAdminLink( 'user-edit.php?user_id=' + customer.id );
|
2021-07-23 12:47:23 +00:00
|
|
|
|
}
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{ interpolateComponents( {
|
|
|
|
|
mixedString: sprintf(
|
2023-11-16 11:15:11 +00:00
|
|
|
|
/* translators: 1: order number, 2: customer name */
|
2021-07-23 12:47:23 +00:00
|
|
|
|
__(
|
|
|
|
|
'{{orderLink}}Order #%(orderNumber)s{{/orderLink}} %(customerString)s',
|
2022-03-30 09:00:04 +00:00
|
|
|
|
'woocommerce'
|
2020-11-06 01:21:05 +00:00
|
|
|
|
),
|
2021-07-23 12:47:23 +00:00
|
|
|
|
{
|
|
|
|
|
orderNumber,
|
2022-06-01 17:15:09 +00:00
|
|
|
|
customerString: getCustomerString( customer ),
|
2021-07-23 12:47:23 +00:00
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
components: {
|
|
|
|
|
orderLink: (
|
|
|
|
|
<Link
|
|
|
|
|
href={ getAdminLink(
|
|
|
|
|
'post.php?action=edit&post=' + orderId
|
|
|
|
|
) }
|
|
|
|
|
onClick={ () =>
|
|
|
|
|
recordOrderEvent( 'order_number' )
|
|
|
|
|
}
|
|
|
|
|
type="wp-admin"
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
destinationFlag:
|
|
|
|
|
customer && customer.country ? (
|
2020-11-06 01:21:05 +00:00
|
|
|
|
<Flag
|
2021-07-23 12:47:23 +00:00
|
|
|
|
code={ customer && customer.country }
|
2020-11-06 01:21:05 +00:00
|
|
|
|
round={ false }
|
|
|
|
|
/>
|
|
|
|
|
) : null,
|
2021-07-23 12:47:23 +00:00
|
|
|
|
customerLink: customerUrl ? (
|
|
|
|
|
<Link
|
|
|
|
|
href={ customerUrl }
|
|
|
|
|
onClick={ () =>
|
|
|
|
|
recordOrderEvent( 'customer_name' )
|
|
|
|
|
}
|
|
|
|
|
type="wc-admin"
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<span />
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
} ) }
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
const cards = [];
|
|
|
|
|
orders.forEach( ( order ) => {
|
|
|
|
|
const {
|
|
|
|
|
date_created_gmt: dateCreatedGmt,
|
2022-06-01 17:15:09 +00:00
|
|
|
|
line_items: lineItems,
|
2021-07-23 12:47:23 +00:00
|
|
|
|
id: orderId,
|
|
|
|
|
} = order;
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const productsCount = lineItems ? lineItems.length : 0;
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
cards.push(
|
|
|
|
|
<ActivityCard
|
|
|
|
|
key={ orderId }
|
|
|
|
|
className="woocommerce-order-activity-card"
|
|
|
|
|
title={ orderCardTitle( order ) }
|
|
|
|
|
date={ dateCreatedGmt }
|
|
|
|
|
onClick={ ( { target } ) => {
|
|
|
|
|
recordOrderEvent( 'orders_begin_fulfillment' );
|
|
|
|
|
if ( ! target.href ) {
|
|
|
|
|
window.location.href = getAdminLink(
|
|
|
|
|
`post.php?action=edit&post=${ orderId }`
|
|
|
|
|
);
|
2020-11-06 01:21:05 +00:00
|
|
|
|
}
|
2021-07-23 12:47:23 +00:00
|
|
|
|
} }
|
|
|
|
|
subtitle={
|
|
|
|
|
<div>
|
|
|
|
|
<span>
|
|
|
|
|
{ sprintf(
|
2023-11-16 11:15:11 +00:00
|
|
|
|
/* translators: %d: number of products */
|
2021-07-23 12:47:23 +00:00
|
|
|
|
_n(
|
|
|
|
|
'%d product',
|
|
|
|
|
'%d products',
|
|
|
|
|
productsCount,
|
2022-03-30 09:00:04 +00:00
|
|
|
|
'woocommerce'
|
2021-07-23 12:47:23 +00:00
|
|
|
|
),
|
|
|
|
|
productsCount
|
|
|
|
|
) }
|
|
|
|
|
</span>
|
2022-06-01 17:15:09 +00:00
|
|
|
|
<span>
|
|
|
|
|
{ getFormattedOrderTotal(
|
|
|
|
|
order.total,
|
|
|
|
|
order.currency
|
|
|
|
|
) }
|
|
|
|
|
</span>
|
2021-07-23 12:47:23 +00:00
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<OrderStatus
|
|
|
|
|
order={ order }
|
2022-01-06 12:53:30 +00:00
|
|
|
|
orderStatusMap={ getAdminSetting( 'orderStatuses', {} ) }
|
2021-07-23 12:47:23 +00:00
|
|
|
|
/>
|
|
|
|
|
</ActivityCard>
|
2020-11-06 01:21:05 +00:00
|
|
|
|
);
|
2021-07-23 12:47:23 +00:00
|
|
|
|
} );
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{ cards }
|
|
|
|
|
<Link
|
|
|
|
|
href={ 'edit.php?post_type=shop_order' }
|
|
|
|
|
className="woocommerce-layout__activity-panel-outbound-link"
|
|
|
|
|
onClick={ () => recordOrderEvent( 'orders_manage' ) }
|
|
|
|
|
type="wp-admin"
|
|
|
|
|
>
|
2022-03-30 09:00:04 +00:00
|
|
|
|
{ __( 'Manage all orders', 'woocommerce' ) }
|
2021-07-23 12:47:23 +00:00
|
|
|
|
</Link>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2022-01-19 16:45:17 +00:00
|
|
|
|
function OrdersPanel( { unreadOrdersCount, orderStatuses } ) {
|
2021-07-23 12:47:23 +00:00
|
|
|
|
const actionableOrdersQuery = useMemo(
|
|
|
|
|
() => ( {
|
|
|
|
|
page: 1,
|
|
|
|
|
per_page: 5,
|
|
|
|
|
status: orderStatuses,
|
|
|
|
|
_fields: [
|
|
|
|
|
'id',
|
|
|
|
|
'number',
|
2022-06-01 17:15:09 +00:00
|
|
|
|
'currency',
|
2021-07-23 12:47:23 +00:00
|
|
|
|
'status',
|
2022-06-01 17:15:09 +00:00
|
|
|
|
'total',
|
2021-07-23 12:47:23 +00:00
|
|
|
|
'customer',
|
2022-06-01 17:15:09 +00:00
|
|
|
|
'line_items',
|
2021-07-23 12:47:23 +00:00
|
|
|
|
'customer_id',
|
|
|
|
|
'date_created_gmt',
|
|
|
|
|
],
|
|
|
|
|
} ),
|
|
|
|
|
[ orderStatuses ]
|
|
|
|
|
);
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const currencyContext = useContext( CurrencyContext );
|
|
|
|
|
|
2024-01-19 04:20:36 +00:00
|
|
|
|
const storeCurrency = currencyContext.getCurrencyConfig();
|
|
|
|
|
const { currencySymbols = {} } = getAdminSetting( 'onboarding', {} );
|
|
|
|
|
const getFormattedOrderTotal = ( total, orderCurrencyCode ) => {
|
|
|
|
|
if ( ! orderCurrencyCode ) {
|
2022-06-01 17:15:09 +00:00
|
|
|
|
return null;
|
2021-07-23 12:47:23 +00:00
|
|
|
|
}
|
2020-11-06 01:21:05 +00:00
|
|
|
|
|
2024-01-19 04:20:36 +00:00
|
|
|
|
// If the order currency is the same as the store currency, we show the formatted amount.
|
|
|
|
|
if ( storeCurrency && storeCurrency.code === orderCurrencyCode ) {
|
|
|
|
|
return currencyContext.formatAmount( total );
|
|
|
|
|
}
|
|
|
|
|
const symbol = currencySymbols[ orderCurrencyCode ];
|
|
|
|
|
|
|
|
|
|
if ( ! symbol ) {
|
|
|
|
|
// This should never happen, but if it does, we'll just show the currency code.
|
|
|
|
|
return `${ orderCurrencyCode }${ total }`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the order currency is different from the store currency, we show the currency code and amount in the order currency.
|
|
|
|
|
return CurrencyFactory( {
|
|
|
|
|
...storeCurrency,
|
|
|
|
|
symbol: decodeEntities( symbol ),
|
|
|
|
|
code: orderCurrencyCode,
|
|
|
|
|
} ).formatAmount( total );
|
2022-06-01 17:15:09 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
|
const {
|
|
|
|
|
orders = [],
|
|
|
|
|
isRequesting,
|
|
|
|
|
isError,
|
|
|
|
|
customerItems,
|
|
|
|
|
} = useSelect( ( select ) => {
|
|
|
|
|
const { getOrders, hasFinishedResolution, getOrdersError } =
|
|
|
|
|
select( ORDERS_STORE_NAME );
|
|
|
|
|
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
|
|
|
|
|
const { getItems } = select( ITEMS_STORE_NAME );
|
2022-06-01 17:15:09 +00:00
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
|
if ( ! orderStatuses.length && unreadOrdersCount === 0 ) {
|
|
|
|
|
return { isRequesting: false };
|
|
|
|
|
}
|
2022-06-01 17:15:09 +00:00
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
|
/* eslint-disable @wordpress/no-unused-vars-before-return */
|
|
|
|
|
const actionableOrders = getOrders( actionableOrdersQuery, null );
|
2021-07-23 12:47:23 +00:00
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
|
const isRequestingActionable = hasFinishedResolution( 'getOrders', [
|
|
|
|
|
actionableOrdersQuery,
|
|
|
|
|
] );
|
2021-07-23 12:47:23 +00:00
|
|
|
|
|
2022-06-21 08:37:34 +00:00
|
|
|
|
if (
|
|
|
|
|
isRequestingActionable ||
|
|
|
|
|
unreadOrdersCount === null ||
|
|
|
|
|
actionableOrders === null
|
|
|
|
|
) {
|
2021-07-23 12:47:23 +00:00
|
|
|
|
return {
|
2022-06-21 08:37:34 +00:00
|
|
|
|
isError: Boolean( getOrdersError( actionableOrdersQuery ) ),
|
|
|
|
|
isRequesting: true,
|
2021-07-23 12:47:23 +00:00
|
|
|
|
orderStatuses,
|
2020-11-06 01:21:05 +00:00
|
|
|
|
};
|
2021-07-23 12:47:23 +00:00
|
|
|
|
}
|
2022-06-21 08:37:34 +00:00
|
|
|
|
|
|
|
|
|
const customers = getItems( 'customers', {
|
|
|
|
|
users: actionableOrders
|
|
|
|
|
.map( ( order ) => order.customer_id )
|
|
|
|
|
.filter( ( id ) => id !== 0 ),
|
|
|
|
|
_fields: [ 'id', 'name', 'country', 'user_id' ],
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
orders: actionableOrders,
|
|
|
|
|
isError: Boolean( getOrdersError( actionableOrders ) ),
|
|
|
|
|
isRequesting: isRequestingActionable,
|
|
|
|
|
orderStatuses,
|
|
|
|
|
customerItems: customers,
|
|
|
|
|
};
|
|
|
|
|
} );
|
2021-07-23 12:47:23 +00:00
|
|
|
|
|
|
|
|
|
if ( isError ) {
|
|
|
|
|
if ( ! orderStatuses.length && window.wcAdminFeatures.analytics ) {
|
2020-11-06 01:21:05 +00:00
|
|
|
|
return (
|
2021-07-23 12:47:23 +00:00
|
|
|
|
<EmptyContent
|
|
|
|
|
title={ __(
|
|
|
|
|
"You currently don't have any actionable statuses. " +
|
|
|
|
|
'To display orders here, select orders that require further review in settings.',
|
2022-03-30 09:00:04 +00:00
|
|
|
|
'woocommerce'
|
2021-07-23 12:47:23 +00:00
|
|
|
|
) }
|
2022-03-30 09:00:04 +00:00
|
|
|
|
actionLabel={ __( 'Settings', 'woocommerce' ) }
|
2021-07-23 12:47:23 +00:00
|
|
|
|
actionURL={ getAdminLink(
|
|
|
|
|
'admin.php?page=wc-admin&path=/analytics/settings'
|
|
|
|
|
) }
|
|
|
|
|
/>
|
2020-11-06 01:21:05 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 05:05:11 +00:00
|
|
|
|
throw new Error(
|
|
|
|
|
'Failed to load orders, raise error to trigger ErrorBoundary'
|
2020-11-06 01:21:05 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
2022-06-01 17:15:09 +00:00
|
|
|
|
const customerList = customerItems
|
|
|
|
|
? Array.from( customerItems, ( [ , value ] ) => value )
|
|
|
|
|
: [];
|
2021-07-23 12:47:23 +00:00
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Section>
|
|
|
|
|
{ isRequesting ? (
|
|
|
|
|
<ActivityCardPlaceholder
|
|
|
|
|
className="woocommerce-order-activity-card"
|
|
|
|
|
hasAction
|
|
|
|
|
hasDate
|
|
|
|
|
lines={ 1 }
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
2022-06-01 17:15:09 +00:00
|
|
|
|
renderOrders( orders, customerList, getFormattedOrderTotal )
|
2021-07-23 12:47:23 +00:00
|
|
|
|
) }
|
|
|
|
|
</Section>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2020-11-06 01:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OrdersPanel.propTypes = {
|
2022-01-19 16:45:17 +00:00
|
|
|
|
unreadOrdersCount: PropTypes.number,
|
2020-11-06 16:50:24 +00:00
|
|
|
|
orderStatuses: PropTypes.array,
|
2020-11-06 01:21:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-07-23 12:47:23 +00:00
|
|
|
|
export default OrdersPanel;
|