2020-03-06 12:27:54 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
2020-03-06 12:27:54 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-04-22 11:37:27 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2023-11-20 12:53:41 +00:00
|
|
|
import type { CartTotalsTaxLineItem, Currency } from '@woocommerce/types';
|
2023-03-02 14:26:00 +00:00
|
|
|
import type { ReactElement } from 'react';
|
2020-03-06 12:27:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-01-20 20:35:53 +00:00
|
|
|
import TotalsItem from '../item';
|
2020-03-06 12:27:54 +00:00
|
|
|
|
2021-03-05 14:03:48 +00:00
|
|
|
interface Values {
|
|
|
|
tax_lines: CartTotalsTaxLineItem[];
|
|
|
|
total_tax: string;
|
|
|
|
}
|
|
|
|
|
2021-12-21 17:08:57 +00:00
|
|
|
export interface TotalsTaxesProps {
|
2021-03-05 14:03:48 +00:00
|
|
|
className?: string;
|
|
|
|
currency: Currency;
|
2021-05-17 14:00:57 +00:00
|
|
|
showRateAfterTaxName: boolean;
|
2021-03-05 14:03:48 +00:00
|
|
|
values: Values | Record< string, never >;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TotalsTaxes = ( {
|
|
|
|
currency,
|
|
|
|
values,
|
|
|
|
className,
|
2021-05-17 14:00:57 +00:00
|
|
|
showRateAfterTaxName,
|
2021-03-05 14:03:48 +00:00
|
|
|
}: TotalsTaxesProps ): ReactElement | null => {
|
2020-04-24 13:44:44 +00:00
|
|
|
const { total_tax: totalTax, tax_lines: taxLines } = values;
|
2020-03-06 12:27:54 +00:00
|
|
|
|
2021-09-16 12:16:21 +00:00
|
|
|
if (
|
|
|
|
! getSetting( 'taxesEnabled', true ) &&
|
|
|
|
parseInt( totalTax, 10 ) <= 0
|
|
|
|
) {
|
2020-04-22 13:16:17 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-05-26 13:04:42 +00:00
|
|
|
const showItemisedTaxes = getSetting(
|
2021-05-17 14:00:57 +00:00
|
|
|
'displayItemizedTaxes',
|
|
|
|
false
|
2021-05-26 13:04:42 +00:00
|
|
|
) as boolean;
|
|
|
|
|
2021-06-02 10:14:42 +00:00
|
|
|
const itemisedTaxItems: ReactElement | null =
|
|
|
|
showItemisedTaxes && taxLines.length > 0 ? (
|
2022-04-01 13:45:18 +00:00
|
|
|
<>
|
2021-06-02 10:14:42 +00:00
|
|
|
{ taxLines.map( ( { name, rate, price }, i ) => {
|
|
|
|
const label = `${ name }${
|
|
|
|
showRateAfterTaxName ? ` ${ rate }` : ''
|
|
|
|
}`;
|
|
|
|
return (
|
|
|
|
<TotalsItem
|
|
|
|
key={ `tax-line-${ i }` }
|
2024-05-31 03:49:36 +00:00
|
|
|
className={ clsx(
|
2022-04-01 13:45:18 +00:00
|
|
|
'wc-block-components-totals-taxes',
|
|
|
|
className
|
|
|
|
) }
|
2021-06-02 10:14:42 +00:00
|
|
|
currency={ currency }
|
|
|
|
label={ label }
|
|
|
|
value={ parseInt( price, 10 ) }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} ) }{ ' ' }
|
2022-04-01 13:45:18 +00:00
|
|
|
</>
|
2021-06-02 10:14:42 +00:00
|
|
|
) : null;
|
2021-05-17 14:00:57 +00:00
|
|
|
|
2021-05-26 13:04:42 +00:00
|
|
|
return showItemisedTaxes ? (
|
|
|
|
itemisedTaxItems
|
|
|
|
) : (
|
2021-05-17 14:00:57 +00:00
|
|
|
<>
|
2020-04-24 13:44:44 +00:00
|
|
|
<TotalsItem
|
2024-05-31 03:49:36 +00:00
|
|
|
className={ clsx(
|
2021-01-20 20:35:53 +00:00
|
|
|
'wc-block-components-totals-taxes',
|
|
|
|
className
|
|
|
|
) }
|
2020-04-24 13:44:44 +00:00
|
|
|
currency={ currency }
|
2023-12-12 23:05:20 +00:00
|
|
|
label={ __( 'Taxes', 'woocommerce' ) }
|
2020-04-24 13:44:44 +00:00
|
|
|
value={ parseInt( totalTax, 10 ) }
|
2021-05-26 13:04:42 +00:00
|
|
|
description={ null }
|
2020-04-24 13:44:44 +00:00
|
|
|
/>
|
|
|
|
</>
|
2020-03-06 12:27:54 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-12-09 07:29:34 +00:00
|
|
|
export default TotalsTaxes;
|