Skip to content

Commit

Permalink
Display price in place order button (woocommerce#47083)
Browse files Browse the repository at this point in the history
* WIP: Display price in place order button

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

* Fix JS lint error

* Update e2e tests

* Display default place order button including placeholder in page editor

* Update plugins/woocommerce-blocks/assets/js/blocks/checkout/inner-blocks/checkout-actions-block/constants.tsx

Co-authored-by: Seghir Nadir <[email protected]>

* Optimise i18n using sprintf

* Wrap cart totals in “useSelect”

* Temporary skip failing e2e test

---------

Co-authored-by: github-actions <[email protected]>
Co-authored-by: Seghir Nadir <[email protected]>
  • Loading branch information
3 people authored May 8, 2024
1 parent c464afc commit 99bed6d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { useCheckoutSubmit } from '@woocommerce/base-context/hooks';
import { noticeContexts } from '@woocommerce/base-context';
import { StoreNoticesContainer } from '@woocommerce/blocks-components';
import { applyCheckoutFilter } from '@woocommerce/blocks-checkout';
import { CART_STORE_KEY } from '@woocommerce/block-data';
import { useSelect } from '@wordpress/data';
import { formatPrice } from '@woocommerce/price-format';

/**
* Internal dependencies
Expand All @@ -30,14 +33,31 @@ const Block = ( {
placeOrderButtonLabel: string;
} ): JSX.Element => {
const { paymentMethodButtonLabel } = useCheckoutSubmit();
const label = applyCheckoutFilter( {

const cartTotals = useSelect( ( select ) => {
const store = select( CART_STORE_KEY );
return store.getCartTotals();
}, [] );

const totalPrice = formatPrice( cartTotals.total_price );

let label = applyCheckoutFilter( {
filterName: 'placeOrderButtonLabel',
defaultValue:
paymentMethodButtonLabel ||
placeOrderButtonLabel ||
defaultPlaceOrderButtonLabel,
} );

if ( label.includes( '<price/>' ) ) {
if ( cartTotals.total_price === '0' ) {
label = label.replace( '<price/>', '' );
label = label.replace( /[^a-zA-Z\s]/g, '' );
} else {
label = label.replace( '<price/>', totalPrice );
}
}

return (
<div
className={ classnames( 'wc-block-checkout__actions', className ) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';

export const defaultPlaceOrderButtonLabel = __( 'Place Order', 'woocommerce' );
export const defaultPlaceOrderButtonLabel = sprintf(
// translators: %s: is the price.
__( 'Place Order · %s', 'woocommerce' ),
'<price/>'
);
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ const EditableButton = ( {
value,
...props
}: EditableButtonProps ) => {
/**
* If the value contains a placeholder, e.g. "Place Order · <price/>", we need to change it,
* e.g. to "Place Order · &lt;price/>", to ensure it is displayed correctly. This reflects the
* default behaviour of the `RichText` component if we would type "<price/>" directly into it.
*/
value = value.replace( /</g, '&lt;' );

return (
<Button { ...props }>
<RichText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ test.describe( 'Shopper → Translations', () => {
page.getByRole( 'link', { name: 'Terug naar winkelwagen' } )
).toBeVisible();

await expect(
page.getByRole( 'button', { name: 'Bestel en betaal' } )
).toBeVisible();
/**
* @todo Uncomment and update when WooCommerce 9.0.0 is released and a translation for the new string is available.
* @see https://github.com/woocommerce/woocommerce/issues/47260
*/
// await expect(
// page.getByRole( 'button', { name: 'Bestel en betaal' } )
// ).toBeVisible();

await expect(
page.getByRole( 'button', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class CheckoutPage {
// your road?" field.
} );
await this.waitForCheckoutToFinishUpdating();
await this.page.getByText( 'Place Order', { exact: true } ).click();
await this.page.getByText( 'Place Order' ).click();
if ( waitForRedirect ) {
await this.page.waitForURL( /order-received/ );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: update

Display the total price in the place order button.

0 comments on commit 99bed6d

Please sign in to comment.