2022-12-15 13:04:28 +00:00
/ * *
* External dependencies
* /
2024-02-28 21:00:52 +00:00
import { registerPlugin } from '@wordpress/plugins' ;
2022-12-15 13:04:28 +00:00
import { _ _ } from '@wordpress/i18n' ;
import interpolateComponents from '@automattic/interpolate-components' ;
2024-02-28 21:00:52 +00:00
import { Button , Card , CardBody , createSlotFill } from '@wordpress/components' ;
2022-12-15 13:04:28 +00:00
import { Icon , closeSmall } from '@wordpress/icons' ;
import { useEffect , useState } from '@wordpress/element' ;
import { useDispatch } from '@wordpress/data' ;
import { store as noticesStore } from '@wordpress/notices' ;
2022-12-16 08:57:09 +00:00
import { recordEvent } from '@woocommerce/tracks' ;
2022-12-15 13:04:28 +00:00
/ * *
* Internal dependencies
* /
import './conflict-error-slotfill.scss' ;
import warningIcon from './alert-triangle-icon.svg' ;
2024-02-28 21:00:52 +00:00
import { SETTINGS _SLOT _FILL _CONSTANT } from './settings-slots' ;
2022-12-15 13:04:28 +00:00
2024-02-28 21:00:52 +00:00
const { Fill } = createSlotFill ( SETTINGS _SLOT _FILL _CONSTANT ) ;
2022-12-15 13:04:28 +00:00
const LearnMore = ( ) => (
< Button
2023-11-06 11:45:26 +00:00
href = "https://woo.com/document/setting-up-taxes-in-woocommerce/"
2022-12-15 13:04:28 +00:00
target = "_blank"
>
{ _ _ ( 'Learn more' , 'woocommerce' ) }
< / B u t t o n >
) ;
const SettingsErrorFill = ( ) => {
const [ dismissedConflictWarning , setDismissedConflictWarning ] =
useState ( false ) ;
const [ pricesEnteredWithTaxSetting , setMainVal ] = useState (
document . forms . mainform . elements . woocommerce _prices _include _tax
2024-02-28 21:00:52 +00:00
? . value === 'yes'
2022-12-15 13:04:28 +00:00
? 'incl'
: 'excl'
) ;
const [ displayPricesInShopWithTaxSetting , setDisplayShop ] = useState (
/** We're using jQuery in this file because the select boxes are implemented using select2 and can only be interacted with using jQuery */
window . jQuery ( '#woocommerce_tax_display_shop' ) . val ( )
) ;
const [ displayPricesInCartWithTaxSetting , setDisplayCart ] = useState (
/** We're using jQuery in this file because the select boxes are implemented using select2 and can only be interacted with using jQuery */
window . jQuery ( '#woocommerce_tax_display_cart' ) . val ( )
) ;
const { createNotice } = useDispatch ( noticesStore ) ;
const handleApplyRecommendedSettings = ( ) => {
/** We're using jQuery in this file because the select boxes are implemented using select2 and can only be interacted with using jQuery */
// eslint-disable-next-line no-undef
window
. jQuery ( '#woocommerce_tax_display_shop' )
. val ( pricesEnteredWithTaxSetting )
. trigger ( 'change' ) ;
window
. jQuery ( '#woocommerce_tax_display_cart' )
. val ( pricesEnteredWithTaxSetting )
. trigger ( 'change' ) ;
createNotice (
'success' ,
_ _ ( 'Recommended settings applied.' , 'woocommerce' )
) ;
2022-12-16 08:57:09 +00:00
recordEvent ( 'tax_settings_conflict_recommended_settings_clicked' ) ;
2022-12-15 13:04:28 +00:00
} ;
const ApplyRecommendedSettingsButton = ( ) => (
< Button variant = "primary" onClick = { handleApplyRecommendedSettings } >
{ _ _ ( 'Use recommended settings' , 'woocommerce' ) }
< / B u t t o n >
) ;
useEffect ( ( ) => {
document
. querySelectorAll ( "input[name='woocommerce_prices_include_tax']" )
. forEach ( ( input ) => {
input . addEventListener ( 'change' , ( ) =>
setMainVal (
document . forms . mainform . elements
. woocommerce _prices _include _tax . value === 'yes'
? 'incl'
: 'excl'
)
) ;
} ) ;
} , [ ] ) ;
useEffect ( ( ) => {
window
. jQuery ( '#woocommerce_tax_display_shop' )
. on ( 'click change' , ( ) =>
setDisplayShop (
document . getElementById ( 'woocommerce_tax_display_shop' )
. value
)
) ;
} , [ ] ) ;
useEffect ( ( ) => {
window
. jQuery ( '#woocommerce_tax_display_cart' )
. on ( 'click change' , ( ) =>
setDisplayCart (
document . getElementById ( 'woocommerce_tax_display_cart' )
. value
)
) ;
} , [ ] ) ;
const [ isConflict , setIsConflict ] = useState ( false ) ;
useEffect ( ( ) => {
if (
displayPricesInShopWithTaxSetting === pricesEnteredWithTaxSetting &&
displayPricesInCartWithTaxSetting === pricesEnteredWithTaxSetting
) {
setIsConflict ( false ) ;
} else {
setIsConflict ( true ) ;
2022-12-16 08:57:09 +00:00
recordEvent ( 'tax_settings_conflict' , {
main : pricesEnteredWithTaxSetting ,
shop : displayPricesInShopWithTaxSetting ,
cart : displayPricesInCartWithTaxSetting ,
} ) ;
2022-12-15 13:04:28 +00:00
}
} , [
displayPricesInCartWithTaxSetting ,
displayPricesInShopWithTaxSetting ,
pricesEnteredWithTaxSetting ,
] ) ;
if ( ! isConflict || dismissedConflictWarning ) {
return < Fill > < / F i l l > ;
}
return (
< Fill >
< div className = "woocommerce_tax_settings_conflict_error" >
< Card >
< CardBody className = "woocommerce_tax_settings_conflict_error_card_body" >
< div >
< img
className = "woocommerce_tax_settings_conflict_error_card_body__warning_icon"
src = { warningIcon }
alt = "Warning Icon"
/ >
< / d i v >
< div >
< div className = "woocommerce_tax_settings_conflict_error_card_body__body_text" >
< p style = { { fontSize : 13 } } >
{ interpolateComponents ( {
mixedString : _ _ (
'{{b}}Inconsistent tax settings:{{/b}} To avoid possible rounding errors, prices should be entered and displayed consistently in all locations either including, or excluding taxes.' ,
'woocommerce'
) ,
components : {
b : < b / > ,
} ,
} ) }
< / p >
< / d i v >
< div className = "woocommerce_tax_settings_conflict_error_card_body__buttons" >
< ApplyRecommendedSettingsButton / > < LearnMore / >
< / d i v >
< / d i v >
< div >
< Button
className = "woocommerce_tax_settings_conflict_error_card_body__close_icon"
2022-12-16 08:57:09 +00:00
onClick = { ( ) => {
setDismissedConflictWarning ( true ) ;
recordEvent (
'tax_settings_conflict_dismissed'
) ;
} }
2022-12-15 13:04:28 +00:00
>
< Icon icon = { closeSmall } / >
< / B u t t o n >
< / d i v >
< / C a r d B o d y >
< / C a r d >
< / d i v >
< / F i l l >
) ;
} ;
2024-02-28 21:00:52 +00:00
export const registerTaxSettingsConflictErrorFill = ( ) => {
registerPlugin ( 'woocommerce-admin-tax-settings-conflict-warning' , {
scope : 'woocommerce-tax-settings' ,
render : SettingsErrorFill ,
} ) ;
2022-12-15 13:04:28 +00:00
} ;