Commit 3d6e63bf07e for woocommerce
commit 3d6e63bf07e320c07cd9c894b8ae776d8502b779
Author: Tom Cafferkey <tjcafferkey@gmail.com>
Date: Fri Nov 20 11:08:44 2026 +0000
Cart and checkout button block styles support (#71105)
* Block level styles
* Remove block-level style support from Coupon Form blocks
* Remove changelog entry for button block styles
* Move type to types.ts
* Remove space
* Remove space
* Fix button style support test formatting
diff --git a/docs/block-development/reference/block-references.md b/docs/block-development/reference/block-references.md
index 5f1e2b4e702..ee52f96e687 100644
--- a/docs/block-development/reference/block-references.md
+++ b/docs/block-development/reference/block-references.md
@@ -378,7 +378,7 @@ Allow customers proceed to Checkout.
- **Name:** woocommerce/proceed-to-checkout-block
- **Category:** woocommerce
- **Parent:** woocommerce/cart-totals-block
-- **Supports:** ~~align~~, ~~html~~, ~~inserter~~, ~~lock~~, ~~multiple~~, ~~reusable~~
+- **Supports:** color (background, gradients, text, ~~link~~), spacing (padding), typography (fontSize, lineHeight, textAlign), ~~align~~, ~~html~~, ~~inserter~~, ~~lock~~, ~~multiple~~, ~~reusable~~
- **Attributes:** lock
## Cart Link - woocommerce/cart-link
@@ -433,7 +433,7 @@ Place order button, with optional Terms and Conditions and Return to Cart links.
- **Name:** woocommerce/checkout-actions-block
- **Category:** woocommerce
- **Parent:** woocommerce/checkout-fields-block
-- **Supports:** ~~align~~, ~~html~~, ~~inserter~~, ~~lock~~, ~~multiple~~, ~~reusable~~
+- **Supports:** color (background, gradients, text, ~~link~~), spacing (padding), typography (fontSize, lineHeight, textAlign), ~~align~~, ~~html~~, ~~inserter~~, ~~lock~~, ~~multiple~~, ~~reusable~~
- **Attributes:** cartPageId, className, lock, priceSeparator, showReturnToCart, showTerms, termsCheckbox, termsText
## Additional information - woocommerce/checkout-additional-information-block
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/place-order-button/index.tsx b/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/place-order-button/index.tsx
index 644c6518c70..2f2123d3544 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/place-order-button/index.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/base/components/cart-checkout/place-order-button/index.tsx
@@ -16,6 +16,7 @@ import {
import { useValidateCheckout } from '@woocommerce/blocks-checkout';
import type { CustomPlaceOrderButtonComponent } from '@woocommerce/types';
import { useEditorContext } from '@woocommerce/base-context';
+import type { StyleProps } from '@woocommerce/base-hooks';
/**
* Internal dependencies
@@ -27,6 +28,7 @@ interface PlaceOrderButtonProps {
fullWidth?: boolean;
showPrice?: boolean;
priceSeparator?: string;
+ buttonStyleProps?: StyleProps;
// eslint-disable-next-line @typescript-eslint/naming-convention
CustomButtonComponent?: CustomPlaceOrderButtonComponent;
}
@@ -37,6 +39,7 @@ const PlaceOrderButton = ( {
showPrice = false,
priceSeparator = '·',
CustomButtonComponent,
+ buttonStyleProps,
}: PlaceOrderButtonProps ): JSX.Element => {
const {
onSubmit,
@@ -77,6 +80,7 @@ const PlaceOrderButton = ( {
<Button
className={ clsx(
'wc-block-components-checkout-place-order-button',
+ buttonStyleProps?.className,
{
'wc-block-components-checkout-place-order-button--full-width':
fullWidth,
@@ -86,6 +90,7 @@ const PlaceOrderButton = ( {
waitingForProcessing || waitingForRedirect,
}
) }
+ style={ buttonStyleProps?.style }
onClick={ onSubmit }
disabled={
isCalculating ||
diff --git a/plugins/woocommerce/client/blocks/assets/js/base/hooks/use-typography-props.ts b/plugins/woocommerce/client/blocks/assets/js/base/hooks/use-typography-props.ts
index 0557f9a3c97..730161e3120 100644
--- a/plugins/woocommerce/client/blocks/assets/js/base/hooks/use-typography-props.ts
+++ b/plugins/woocommerce/client/blocks/assets/js/base/hooks/use-typography-props.ts
@@ -3,6 +3,7 @@
*/
import { isObject, isString } from '@woocommerce/types';
import type { Style as StyleEngineProperties } from '@wordpress/style-engine/src/types';
+import type { CSSProperties } from 'react';
/**
* Internal dependencies
@@ -18,7 +19,8 @@ type blockAttributes = {
};
export const useTypographyProps = ( props: blockAttributes ): StyleProps => {
- const typography = isObject( props.style.typography )
+ const typography: NonNullable< StyleEngineProperties[ 'typography' ] > &
+ Pick< CSSProperties, 'textAlign' > = isObject( props.style.typography )
? props.style.typography
: {};
const classNameFallback = isString( typography.fontFamily )
@@ -40,6 +42,7 @@ export const useTypographyProps = ( props: blockAttributes ): StyleProps => {
lineHeight: typography.lineHeight,
textDecoration: typography.textDecoration,
textTransform: typography.textTransform,
+ textAlign: typography.textAlign,
},
};
};
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/test/button-style-supports.ts b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/test/button-style-supports.ts
new file mode 100644
index 00000000000..70769bbbd16
--- /dev/null
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/test/button-style-supports.ts
@@ -0,0 +1,46 @@
+/**
+ * Internal dependencies
+ */
+import checkoutActions from '../../checkout/inner-blocks/checkout-actions-block/block.json';
+import proceedToCheckout from '../../cart/inner-blocks/proceed-to-checkout-block/block.json';
+
+describe.each( [ checkoutActions, proceedToCheckout ] )(
+ '$name button style controls',
+ ( { supports } ) => {
+ it( 'shows the primary style controls by default', () => {
+ expect( supports.color.__experimentalDefaultControls ).toEqual( {
+ background: true,
+ text: true,
+ } );
+ expect(
+ supports.typography.__experimentalDefaultControls.fontSize
+ ).toBe( true );
+ expect( supports.typography.textAlign ).toBe( true );
+ expect(
+ supports.typography.__experimentalDefaultControls.textAlign
+ ).toBe( true );
+ expect(
+ supports.__experimentalBorder.__experimentalDefaultControls
+ ).toEqual( {
+ color: true,
+ radius: true,
+ style: true,
+ width: true,
+ } );
+ expect(
+ supports.spacing.__experimentalDefaultControls.padding
+ ).toBe( true );
+ } );
+
+ it( 'does not serialize button styles onto the block wrapper', () => {
+ for ( const feature of [
+ supports.color,
+ supports.typography,
+ supports.__experimentalBorder,
+ supports.spacing,
+ ] ) {
+ expect( feature.__experimentalSkipSerialization ).toBe( true );
+ }
+ } );
+ }
+);
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/types.ts b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/types.ts
index af521dc3f67..f5f157616ee 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/types.ts
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/cart-checkout-shared/types.ts
@@ -1,3 +1,24 @@
+/**
+ * External dependencies
+ */
+import type { Style } from '@wordpress/style-engine/src/types';
+import type { CSSProperties } from 'react';
+
+export type ButtonStyleAttributes = {
+ style?:
+ | ( Style & {
+ typography?: Style[ 'typography' ] &
+ Pick< CSSProperties, 'textAlign' >;
+ } )
+ | string;
+ backgroundColor?: string;
+ textColor?: string;
+ gradient?: string;
+ borderColor?: string;
+ fontSize?: string;
+ fontFamily?: string;
+};
+
export type ExpressCheckoutAttributes = {
className?: string;
buttonHeight: string;
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/block.json b/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/block.json
index 87b21e359c8..d278ecd8c8d 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/block.json
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/block.json
@@ -4,6 +4,53 @@
"description": "Allow customers proceed to Checkout.",
"category": "woocommerce",
"supports": {
+ "color": {
+ "text": true,
+ "background": true,
+ "gradients": true,
+ "link": false,
+ "__experimentalDefaultControls": {
+ "background": true,
+ "text": true
+ },
+ "__experimentalSkipSerialization": true
+ },
+ "typography": {
+ "fontSize": true,
+ "lineHeight": true,
+ "textAlign": true,
+ "__experimentalFontFamily": true,
+ "__experimentalFontWeight": true,
+ "__experimentalFontStyle": true,
+ "__experimentalTextTransform": true,
+ "__experimentalTextDecoration": true,
+ "__experimentalLetterSpacing": true,
+ "__experimentalDefaultControls": {
+ "fontSize": true,
+ "textAlign": true
+ },
+ "__experimentalSkipSerialization": true
+ },
+ "__experimentalBorder": {
+ "color": true,
+ "radius": true,
+ "style": true,
+ "width": true,
+ "__experimentalDefaultControls": {
+ "color": true,
+ "radius": true,
+ "style": true,
+ "width": true
+ },
+ "__experimentalSkipSerialization": true
+ },
+ "spacing": {
+ "padding": true,
+ "__experimentalDefaultControls": {
+ "padding": true
+ },
+ "__experimentalSkipSerialization": true
+ },
"align": false,
"html": false,
"multiple": false,
@@ -11,6 +58,13 @@
"inserter": false,
"lock": false
},
+ "selectors": {
+ "root": ".wp-block-woocommerce-proceed-to-checkout-block",
+ "color": ".wp-block-woocommerce-proceed-to-checkout-block .wc-block-cart__submit-button",
+ "typography": ".wp-block-woocommerce-proceed-to-checkout-block .wc-block-cart__submit-button",
+ "border": ".wp-block-woocommerce-proceed-to-checkout-block .wc-block-cart__submit-button",
+ "spacing": ".wp-block-woocommerce-proceed-to-checkout-block .wc-block-cart__submit-button"
+ },
"attributes": {
"lock": {
"type": "object",
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/block.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/block.tsx
index 7495be9834e..25e701775ca 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/block.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/block.tsx
@@ -5,7 +5,10 @@ import clsx from 'clsx';
import { useState, useEffect, useMemo } from '@wordpress/element';
import Button from '@woocommerce/base-components/button';
import { CHECKOUT_URL } from '@woocommerce/block-settings';
-import { usePositionRelativeToViewport } from '@woocommerce/base-hooks';
+import {
+ usePositionRelativeToViewport,
+ useStyleProps,
+} from '@woocommerce/base-hooks';
import { getSetting } from '@woocommerce/settings';
import { useSelect } from '@wordpress/data';
import { cartStore, checkoutStore } from '@woocommerce/block-data';
@@ -19,6 +22,7 @@ import { useStoreCart } from '@woocommerce/base-context/hooks';
* Internal dependencies
*/
import { defaultButtonLabel } from './constants';
+import type { ButtonStyleAttributes } from '../../../cart-checkout-shared/types';
/**
* Checkout button rendered in the full cart page.
@@ -27,11 +31,13 @@ const Block = ( {
checkoutPageId,
className,
buttonLabel,
-}: {
+ ...styleAttributes
+}: ButtonStyleAttributes & {
checkoutPageId: number;
className: string;
buttonLabel: string;
} ): JSX.Element => {
+ const buttonStyleProps = useStyleProps( styleAttributes );
const link = getSetting< string >( 'page-' + checkoutPageId, false );
const { cartIsLoading } = useStoreCart();
const isCalculating = useSelect(
@@ -84,9 +90,12 @@ const Block = ( {
const submitContainerContents = (
<Button
- className={ clsx( 'wc-block-cart__submit-button', {
- 'wc-block-cart__submit-button--loading': showSpinner,
- } ) }
+ className={ clsx(
+ 'wc-block-cart__submit-button',
+ buttonStyleProps.className,
+ { 'wc-block-cart__submit-button--loading': showSpinner }
+ ) }
+ style={ buttonStyleProps.style }
href={ filteredLink }
disabled={ isCalculating || cartIsLoading }
onClick={ ( e ) => {
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/edit.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/edit.tsx
index 8a8165c38a7..2dbf3f852f2 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/edit.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/edit.tsx
@@ -8,17 +8,20 @@ import EditableButton from '@woocommerce/editor-components/editable-button';
import { InspectorControls, useBlockProps } from '@wordpress/block-editor';
import PageSelector from '@woocommerce/editor-components/page-selector';
import { CART_PAGE_ID } from '@woocommerce/block-settings';
+import { useStyleProps } from '@woocommerce/base-hooks';
+import clsx from 'clsx';
/**
* Internal dependencies
*/
import { defaultButtonLabel } from './constants';
+import type { ButtonStyleAttributes } from '../../../cart-checkout-shared/types';
export const Edit = ( {
attributes,
setAttributes,
}: {
- attributes: {
+ attributes: ButtonStyleAttributes & {
checkoutPageId: number;
className: string;
buttonLabel: string;
@@ -26,6 +29,7 @@ export const Edit = ( {
setAttributes: ( attributes: Record< string, unknown > ) => void;
} ): JSX.Element => {
const blockProps = useBlockProps();
+ const buttonStyleProps = useStyleProps( attributes );
const { checkoutPageId = 0, buttonLabel } = attributes;
const { current: savedCheckoutPageId } = useRef( checkoutPageId );
@@ -65,7 +69,11 @@ export const Edit = ( {
) }
</InspectorControls>
<EditableButton
- className="wc-block-cart__submit-button"
+ className={ clsx(
+ 'wc-block-cart__submit-button',
+ buttonStyleProps.className
+ ) }
+ style={ buttonStyleProps.style }
value={ buttonLabel }
placeholder={ defaultButtonLabel }
onChange={ ( content ) => {
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/test/block.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/test/block.tsx
index 94b1026c29c..902b1703e77 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/test/block.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/cart/inner-blocks/proceed-to-checkout-block/test/block.tsx
@@ -20,6 +20,43 @@ import Block from '../block';
import { CartEventsProvider } from '../../../../../base/context/providers';
describe( 'Proceed to checkout block', () => {
+ it.each( [ false, true ] )(
+ 'applies button overrides without styling the wrapper (serialized: %s)',
+ ( serialized ) => {
+ const style = {
+ color: { background: '#123456', text: '#ffffff' },
+ border: { radius: '12px', width: '2px', style: 'solid' },
+ spacing: { padding: { top: '20px' } },
+ typography: { fontSize: '24px', textAlign: 'right' as const },
+ };
+ const { container } = render(
+ <Block
+ checkoutPageId={ 0 }
+ buttonLabel="Proceed to Checkout"
+ className=""
+ textColor="contrast"
+ style={ serialized ? JSON.stringify( style ) : style }
+ />
+ );
+ const button = screen.getByRole( 'link' );
+
+ expect( button ).toHaveClass(
+ 'has-contrast-color',
+ 'has-background'
+ );
+ expect( button ).toHaveStyle( {
+ backgroundColor: '#123456',
+ color: '#ffffff',
+ borderRadius: '12px',
+ borderWidth: '2px',
+ paddingTop: '20px',
+ fontSize: '24px',
+ textAlign: 'right',
+ } );
+ expect( container.firstChild ).not.toHaveAttribute( 'style' );
+ }
+ );
+
it( 'allows the text to be filtered', () => {
registerCheckoutFilters( 'test-extension', {
proceedToCheckoutButtonLabel: () => {
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/block.json b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/block.json
index fe099e1efcc..99175be4f2f 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/block.json
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/block.json
@@ -4,6 +4,53 @@
"description": "Place order button, with optional Terms and Conditions and Return to Cart links.",
"category": "woocommerce",
"supports": {
+ "color": {
+ "text": true,
+ "background": true,
+ "gradients": true,
+ "link": false,
+ "__experimentalDefaultControls": {
+ "background": true,
+ "text": true
+ },
+ "__experimentalSkipSerialization": true
+ },
+ "typography": {
+ "fontSize": true,
+ "lineHeight": true,
+ "textAlign": true,
+ "__experimentalFontFamily": true,
+ "__experimentalFontWeight": true,
+ "__experimentalFontStyle": true,
+ "__experimentalTextTransform": true,
+ "__experimentalTextDecoration": true,
+ "__experimentalLetterSpacing": true,
+ "__experimentalDefaultControls": {
+ "fontSize": true,
+ "textAlign": true
+ },
+ "__experimentalSkipSerialization": true
+ },
+ "__experimentalBorder": {
+ "color": true,
+ "radius": true,
+ "style": true,
+ "width": true,
+ "__experimentalDefaultControls": {
+ "color": true,
+ "radius": true,
+ "style": true,
+ "width": true
+ },
+ "__experimentalSkipSerialization": true
+ },
+ "spacing": {
+ "padding": true,
+ "__experimentalDefaultControls": {
+ "padding": true
+ },
+ "__experimentalSkipSerialization": true
+ },
"align": false,
"html": false,
"multiple": false,
@@ -11,6 +58,13 @@
"inserter": false,
"lock": false
},
+ "selectors": {
+ "root": ".wp-block-woocommerce-checkout-actions-block",
+ "color": ".wp-block-woocommerce-checkout-actions-block .wc-block-components-checkout-place-order-button",
+ "typography": ".wp-block-woocommerce-checkout-actions-block .wc-block-components-checkout-place-order-button",
+ "border": ".wp-block-woocommerce-checkout-actions-block .wc-block-components-checkout-place-order-button",
+ "spacing": ".wp-block-woocommerce-checkout-actions-block .wc-block-components-checkout-place-order-button"
+ },
"attributes": {
"lock": {
"type": "object",
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/block.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/block.tsx
index c9bda23063e..8148d43d62b 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/block.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/block.tsx
@@ -14,6 +14,7 @@ import { applyCheckoutFilter } from '@woocommerce/blocks-checkout';
import { CART_URL } from '@woocommerce/block-settings';
import { useSelect } from '@wordpress/data';
import { paymentStore } from '@woocommerce/block-data';
+import { useStyleProps } from '@woocommerce/base-hooks';
/**
* Internal dependencies
@@ -21,9 +22,10 @@ import { paymentStore } from '@woocommerce/block-data';
import { defaultPlaceOrderButtonLabel } from './constants';
import { CheckoutOrderSummarySlot } from '../checkout-order-summary-block/slotfills';
import TermsAndConditions from '../checkout-terms-block/frontend';
+import type { ButtonStyleAttributes } from '../../../cart-checkout-shared/types';
import './style.scss';
-export type BlockAttributes = {
+export type BlockAttributes = ButtonStyleAttributes & {
cartPageId: number;
showReturnToCart: boolean;
className?: string;
@@ -45,7 +47,9 @@ const Block = ( {
showTerms,
termsCheckbox,
termsText,
+ ...styleAttributes
}: BlockAttributes ) => {
+ const buttonStyleProps = useStyleProps( styleAttributes );
const { paymentMethodButtonLabel, paymentMethodPlaceOrderButton } =
useCheckoutSubmit();
@@ -107,6 +111,7 @@ const Block = ( {
</ReturnToCartButton>
) }
<PlaceOrderButton
+ buttonStyleProps={ buttonStyleProps }
CustomButtonComponent={ CustomButtonComponent }
label={ label }
fullWidth
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/edit.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/edit.tsx
index 8b18e5ad0a1..e416e67b6d0 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/edit.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/edit.tsx
@@ -23,6 +23,7 @@ import {
} from '@woocommerce/base-components/cart-checkout';
import clsx from 'clsx';
import { CheckboxControl } from '@woocommerce/blocks-components';
+import { useStyleProps } from '@woocommerce/base-hooks';
/**
* Internal dependencies
@@ -46,6 +47,7 @@ export const Edit = ( {
setAttributes: ( attributes: Record< string, unknown > ) => void;
} ) => {
const blockProps = useBlockProps();
+ const buttonStyleProps = useStyleProps( attributes );
const {
cartPageId = 0,
showReturnToCart = false,
@@ -228,6 +230,7 @@ export const Edit = ( {
</ReturnToCartButton>
) }
<PlaceOrderButton
+ buttonStyleProps={ buttonStyleProps }
label={
<RichText
multiline={ false }
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/test/block.js b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/test/block.js
index 47b37c35e67..88507f55f23 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/test/block.js
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/test/block.js
@@ -29,17 +29,24 @@ jest.mock( '@woocommerce/base-context/hooks', () => ( {
const mockUseSelect = jest.fn();
jest.mock( '@wordpress/data', () => ( {
+ ...jest.requireActual( '@wordpress/data' ),
useSelect: () => mockUseSelect(),
} ) );
jest.mock( '@woocommerce/block-data', () => ( {
+ ...jest.requireActual( '@woocommerce/block-data' ),
paymentStore: 'wc/store/payment',
} ) );
let mockCheckoutShowTerms = true;
jest.mock( '@woocommerce/settings', () => ( {
+ ...jest.requireActual( '@woocommerce/settings' ),
getSetting: jest.fn( ( key, fallback ) =>
- key === 'checkoutShowTerms' ? mockCheckoutShowTerms : fallback
+ key === 'checkoutShowTerms'
+ ? mockCheckoutShowTerms
+ : jest
+ .requireActual( '@woocommerce/settings' )
+ .getSetting( key, fallback )
),
} ) );
@@ -58,6 +65,7 @@ jest.mock( '@woocommerce/blocks-checkout', () => ( {
} ) );
jest.mock( '@woocommerce/block-settings', () => ( {
+ ...jest.requireActual( '@woocommerce/block-settings' ),
CART_URL: '/cart',
} ) );
@@ -105,6 +113,39 @@ describe( 'Checkout Actions Block', () => {
mockUseSelect.mockReturnValue( null );
} );
+ it( 'passes block styles to the Place Order button, not the terms or return link', () => {
+ const { container } = render(
+ <Block
+ { ...defaultProps }
+ showReturnToCart
+ showTerms
+ termsText="Terms and Conditions"
+ backgroundColor="primary"
+ style={ JSON.stringify( {
+ color: { text: '#123456' },
+ spacing: { padding: { top: '20px' } },
+ } ) }
+ />
+ );
+
+ expect( mockPlaceOrderButton ).toHaveBeenCalledWith(
+ expect.objectContaining( {
+ buttonStyleProps: expect.objectContaining( {
+ className: expect.stringContaining(
+ 'has-primary-background-color'
+ ),
+ style: expect.objectContaining( {
+ color: '#123456',
+ paddingTop: '20px',
+ } ),
+ } ),
+ } )
+ );
+ expect( container.firstChild ).not.toHaveAttribute( 'style' );
+ expect( screen.getByTestId( 'terms' ) ).not.toHaveAttribute( 'style' );
+ expect( screen.getByRole( 'link' ) ).not.toHaveAttribute( 'style' );
+ } );
+
it( 'does not pass CustomButtonComponent to PlaceOrderButton when a saved token is active', () => {
mockUseCheckoutSubmit.mockReturnValue( {
paymentMethodButtonLabel: '',
diff --git a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/test/block.tsx b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/test/block.tsx
index 201412cf9f7..9443952bec3 100644
--- a/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/test/block.tsx
+++ b/plugins/woocommerce/client/blocks/assets/js/blocks/checkout/test/block.tsx
@@ -33,6 +33,7 @@ import OrderNote from '../inner-blocks/checkout-order-note-block/block';
import Terms from '../inner-blocks/checkout-terms-block/frontend';
import { termsCheckboxDefaultText } from '../inner-blocks/checkout-terms-block/constants';
import Actions from '../inner-blocks/checkout-actions-block/frontend';
+import type { BlockAttributes as ActionsAttributes } from '../inner-blocks/checkout-actions-block/block';
import Totals from '../inner-blocks/checkout-totals-block/frontend';
import OrderSummary from '../inner-blocks/checkout-order-summary-block/frontend';
import CartItems from '../inner-blocks/checkout-order-summary-cart-items/frontend';
@@ -97,7 +98,11 @@ jest.mock( '../context', () => {
const useCheckoutBlockContext =
jest.requireMock( '../context' ).useCheckoutBlockContext;
-const CheckoutBlock = () => {
+const CheckoutBlock = ( {
+ actionAttributes = {},
+}: {
+ actionAttributes?: Partial< ActionsAttributes >;
+} ) => {
return (
<Checkout attributes={ {} }>
<Fields>
@@ -116,7 +121,7 @@ const CheckoutBlock = () => {
showSeparator={ false }
text={ termsCheckboxDefaultText }
/>
- <Actions />
+ <Actions { ...actionAttributes } />
</Fields>
<Totals>
<OrderSummary>
@@ -188,6 +193,45 @@ describe( 'Testing Checkout', () => {
);
} );
+ it( 'applies and clears Place Order button overrides without styling the terms', async () => {
+ const { rerender } = render(
+ <CheckoutBlock
+ actionAttributes={ {
+ textColor: 'contrast',
+ style: JSON.stringify( {
+ color: { background: '#123456' },
+ border: { radius: '12px' },
+ spacing: { padding: { top: '20px' } },
+ typography: { fontSize: '24px', textAlign: 'right' },
+ } ),
+ } }
+ />
+ );
+ const button = await screen.findByRole( 'button', {
+ name: /Place Order/i,
+ } );
+
+ expect( button ).toHaveClass( 'has-contrast-color' );
+ expect( button ).toHaveStyle( {
+ backgroundColor: '#123456',
+ borderRadius: '12px',
+ paddingTop: '20px',
+ fontSize: '24px',
+ textAlign: 'right',
+ } );
+ expect(
+ button.closest( '.wc-block-checkout__actions' )
+ ).not.toHaveAttribute( 'style' );
+ expect( screen.getByText( /You must accept our/i ) ).not.toHaveClass(
+ 'has-contrast-color'
+ );
+
+ rerender( <CheckoutBlock /> );
+
+ expect( button ).not.toHaveClass( 'has-contrast-color' );
+ expect( button ).not.toHaveStyle( { backgroundColor: '#123456' } );
+ } );
+
it( 'Allows saving payment method if the customer is creating an account or has already logged in', async () => {
const { rerender } = render( <CheckoutBlock /> );