Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit ea812d3

Browse files
authored
Cart and Checkout block transforms for classic shortcodes (#11228)
* Add transforms for checkout block * Block to shortcode switcher in notice * cart transforms * Fix target block for switching * Remove switcher UI for classic cart/checkout * Set isPreview when generating block preview in switcher * Onboarding task * Action on click * Focus on block after replacement * Update notice styling and wording * Undo functionality * Look for woocommerce/classic-shortcode when determining if task list item should display * Enable focus on the cart/checkout block when visiting from the task list * Classic Cart/Checkout Updated Title * Add missing translations * Refactor modal content to avoid sprintf * Improve pickBlockClientId * Tracks events for switching to classic shortcode block * TaskList support for non-block themes * Updated placeholder to work on non-white page backgrounds * Find blocks using findBlock utility * Add TabbableContainer for buttons * Add align to wrapper * Update modal content * Update modal usage * Removed undo link when converting from classic shortcode * Check if block was selected * Revert "Removed undo link when converting from classic shortcode" This reverts commit 2babbab. * update snackbar text
1 parent 44244e3 commit ea812d3

File tree

27 files changed

+805
-182
lines changed

27 files changed

+805
-182
lines changed

assets/js/blocks/cart-checkout-shared/sidebar-notices/index.tsx

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,62 @@ const withSidebarNotices = createHigherOrderComponent(
4848
setIsIncompatibleExtensionsNoticeDismissed( isDismissed );
4949
};
5050

51-
const { isCart, isCheckout, isPaymentMethodsBlock, hasPaymentMethods } =
52-
useSelect( ( select ) => {
53-
const { getBlockParentsByBlockName, getBlockName } =
54-
select( blockEditorStore );
55-
const parent = getBlockParentsByBlockName( clientId, [
56-
'woocommerce/cart',
57-
'woocommerce/checkout',
58-
] ).map( getBlockName );
59-
const currentBlockName = getBlockName( clientId );
60-
return {
61-
isCart:
62-
parent.includes( 'woocommerce/cart' ) ||
63-
currentBlockName === 'woocommerce/cart',
64-
isCheckout:
65-
parent.includes( 'woocommerce/checkout' ) ||
66-
currentBlockName === 'woocommerce/checkout',
67-
isPaymentMethodsBlock:
68-
currentBlockName ===
69-
'woocommerce/checkout-payment-block',
70-
hasPaymentMethods:
71-
select(
72-
PAYMENT_STORE_KEY
73-
).paymentMethodsInitialized() &&
74-
Object.keys(
75-
select(
76-
PAYMENT_STORE_KEY
77-
).getAvailablePaymentMethods()
78-
).length > 0,
79-
};
80-
} );
51+
const {
52+
isCart,
53+
isCheckout,
54+
isPaymentMethodsBlock,
55+
hasPaymentMethods,
56+
parentId,
57+
} = useSelect( ( select ) => {
58+
const { getBlockParentsByBlockName, getBlockName } =
59+
select( blockEditorStore );
60+
61+
const parents = getBlockParentsByBlockName( clientId, [
62+
'woocommerce/cart',
63+
'woocommerce/checkout',
64+
] ).reduce(
65+
(
66+
accumulator: Record< string, string >,
67+
parentClientId: string
68+
) => {
69+
const parentName = getBlockName( parentClientId );
70+
accumulator[ parentName ] = parentClientId;
71+
return accumulator;
72+
},
73+
{}
74+
);
75+
76+
const currentBlockName = getBlockName( clientId );
77+
const parentBlockIsCart =
78+
Object.keys( parents ).includes( 'woocommerce/cart' );
79+
const parentBlockIsCheckout = Object.keys( parents ).includes(
80+
'woocommerce/checkout'
81+
);
82+
const currentBlockIsCart =
83+
currentBlockName === 'woocommerce/cart' || parentBlockIsCart;
84+
const currentBlockIsCheckout =
85+
currentBlockName === 'woocommerce/checkout' ||
86+
parentBlockIsCheckout;
87+
const targetParentBlock = currentBlockIsCart
88+
? 'woocommerce/cart'
89+
: 'woocommerce/checkout';
90+
91+
return {
92+
isCart: currentBlockIsCart,
93+
isCheckout: currentBlockIsCheckout,
94+
parentId:
95+
currentBlockName === targetParentBlock
96+
? clientId
97+
: parents[ targetParentBlock ],
98+
isPaymentMethodsBlock:
99+
currentBlockName === 'woocommerce/checkout-payment-block',
100+
hasPaymentMethods:
101+
select( PAYMENT_STORE_KEY ).paymentMethodsInitialized() &&
102+
Object.keys(
103+
select( PAYMENT_STORE_KEY ).getAvailablePaymentMethods()
104+
).length > 0,
105+
};
106+
} );
81107

82108
// Show sidebar notices only when a WooCommerce block is selected.
83109
if (
@@ -96,10 +122,9 @@ const withSidebarNotices = createHigherOrderComponent(
96122
toggleIncompatibleExtensionsNoticeDismissedStatus
97123
}
98124
block={
99-
isCheckout
100-
? 'woocommerce/checkout'
101-
: 'woocommerce/cart'
125+
isCart ? 'woocommerce/cart' : 'woocommerce/checkout'
102126
}
127+
clientId={ parentId }
103128
/>
104129

105130
<DefaultNotice block={ isCheckout ? 'checkout' : 'cart' } />

assets/js/blocks/cart/edit.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundar
1313
import { EditorProvider, CartProvider } from '@woocommerce/base-context';
1414
import { previewCart } from '@woocommerce/resource-previews';
1515
import { SlotFillProvider } from '@woocommerce/blocks-checkout';
16+
import { useEffect, useRef } from '@wordpress/element';
17+
import { getQueryArg } from '@wordpress/url';
18+
import { dispatch, select } from '@wordpress/data';
1619

1720
/**
1821
* Internal dependencies
@@ -37,7 +40,7 @@ const ALLOWED_BLOCKS = [
3740
'woocommerce/empty-cart-block',
3841
];
3942

40-
export const Edit = ( { className, attributes, setAttributes } ) => {
43+
export const Edit = ( { clientId, className, attributes, setAttributes } ) => {
4144
const { hasDarkControls, currentView, isPreview = false } = attributes;
4245
const defaultTemplate = [
4346
[ 'woocommerce/filled-cart-block', {}, [] ],
@@ -49,6 +52,22 @@ export const Edit = ( { className, attributes, setAttributes } ) => {
4952
} ),
5053
} );
5154

55+
// This focuses on the block when a certain query param is found. This is used on the link from the task list.
56+
const focus = useRef( getQueryArg( window.location.href, 'focus' ) );
57+
58+
useEffect( () => {
59+
if (
60+
focus.current === 'cart' &&
61+
! select( 'core/block-editor' ).hasSelectedBlock()
62+
) {
63+
dispatch( 'core/block-editor' ).selectBlock( clientId );
64+
dispatch( 'core/interface' ).enableComplementaryArea(
65+
'core/edit-site',
66+
'edit-site/block-inspector'
67+
);
68+
}
69+
}, [ clientId ] );
70+
5271
return (
5372
<div { ...blockProps }>
5473
<InspectorControls>

assets/js/blocks/cart/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ const settings = {
4545
attributes: blockAttributes,
4646
edit: Edit,
4747
save: Save,
48+
transforms: {
49+
to: [
50+
{
51+
type: 'block',
52+
blocks: [ 'woocommerce/classic-shortcode' ],
53+
transform: ( attributes ) => {
54+
return createBlock(
55+
'woocommerce/classic-shortcode',
56+
{
57+
shortcode: 'cart',
58+
align: attributes.align,
59+
},
60+
[]
61+
);
62+
},
63+
},
64+
],
65+
},
4866
// Migrates v1 to v2 checkout.
4967
deprecated: [
5068
{

assets/js/blocks/checkout/edit.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import {
2121
} from '@wordpress/components';
2222
import { SlotFillProvider } from '@woocommerce/blocks-checkout';
2323
import type { TemplateArray } from '@wordpress/blocks';
24+
import { useEffect, useRef } from '@wordpress/element';
25+
import { getQueryArg } from '@wordpress/url';
26+
import { dispatch, select } from '@wordpress/data';
2427

2528
/**
2629
* Internal dependencies
@@ -46,9 +49,11 @@ const ALLOWED_BLOCKS: string[] = [
4649
];
4750

4851
export const Edit = ( {
52+
clientId,
4953
attributes,
5054
setAttributes,
5155
}: {
56+
clientId: string;
5257
attributes: Attributes;
5358
setAttributes: ( attributes: Record< string, unknown > ) => undefined;
5459
} ): JSX.Element => {
@@ -66,6 +71,22 @@ export const Edit = ( {
6671
isPreview = false,
6772
} = attributes;
6873

74+
// This focuses on the block when a certain query param is found. This is used on the link from the task list.
75+
const focus = useRef( getQueryArg( window.location.href, 'focus' ) );
76+
77+
useEffect( () => {
78+
if (
79+
focus.current === 'checkout' &&
80+
! select( 'core/block-editor' ).hasSelectedBlock()
81+
) {
82+
dispatch( 'core/block-editor' ).selectBlock( clientId );
83+
dispatch( 'core/interface' ).enableComplementaryArea(
84+
'core/edit-site',
85+
'edit-site/block-inspector'
86+
);
87+
}
88+
}, [ clientId ] );
89+
6990
const defaultTemplate = [
7091
[ 'woocommerce/checkout-fields-block', {}, [] ],
7192
[ 'woocommerce/checkout-totals-block', {}, [] ],

assets/js/blocks/checkout/index.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ const settings = {
3131
},
3232
edit: Edit,
3333
save: Save,
34+
transforms: {
35+
to: [
36+
{
37+
type: 'block',
38+
blocks: [ 'woocommerce/classic-shortcode' ],
39+
transform: ( attributes ) => {
40+
return createBlock(
41+
'woocommerce/classic-shortcode',
42+
{
43+
shortcode: 'checkout',
44+
align: attributes.align,
45+
},
46+
[]
47+
);
48+
},
49+
},
50+
],
51+
},
3452
// Migrates v1 to v2 checkout.
3553
deprecated: [
3654
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "woocommerce/classic-shortcode",
3+
"version": "1.0.0",
4+
"title": "Classic Shortcode",
5+
"description": "Renders classic WooCommerce shortcodes.",
6+
"category": "woocommerce",
7+
"keywords": [ "WooCommerce" ],
8+
"supports": {
9+
"align": true,
10+
"html": false,
11+
"multiple": false,
12+
"reusable": false,
13+
"inserter": true
14+
},
15+
"attributes": {
16+
"shortcode": {
17+
"type": "string",
18+
"default": "cart",
19+
"enum": [ "cart", "checkout" ]
20+
},
21+
"align": {
22+
"type": "string",
23+
"default": "wide"
24+
}
25+
},
26+
"textdomain": "woo-gutenberg-products-block",
27+
"apiVersion": 2,
28+
"$schema": "https://schemas.wp.org/trunk/block.json"
29+
}

assets/js/blocks/classic-shortcode/cart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const onClickCallback = ( {
5353
* Title shown within the block itself.
5454
*/
5555
const getTitle = () => {
56-
return __( 'Cart Shortcode', 'woo-gutenberg-products-block' );
56+
return __( 'Classic Cart', 'woo-gutenberg-products-block' );
5757
};
5858

5959
/**

assets/js/blocks/classic-shortcode/checkout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const onClickCallback = ( {
5050
};
5151

5252
const getTitle = () => {
53-
return __( 'Checkout Shortcode', 'woo-gutenberg-products-block' );
53+
return __( 'Classic Checkout', 'woo-gutenberg-products-block' );
5454
};
5555

5656
const getDescription = () => {

assets/js/blocks/classic-shortcode/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const TEMPLATES: TemplateDetails = {
3131
},
3232
checkout: {
3333
type: TYPES.checkout,
34-
title: __( 'Checkout Shortcode', 'woo-gutenberg-products-block' ),
34+
title: __( 'Checkout Cart', 'woo-gutenberg-products-block' ),
3535
description: __(
3636
'Renders the classic checkout shortcode.',
3737
'woo-gutenberg-products-block'

assets/js/blocks/classic-shortcode/editor.scss

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,19 @@
5858
pointer-events: none;
5959

6060
// Image based placeholders should fill horizontal width.
61-
> img {
61+
> img,
62+
> svg {
6263
width: 100%;
64+
height: auto;
65+
color: $universal-border-light;
6366
}
6467
}
6568

6669
.wp-block-woocommerce-classic-shortcode {
6770
.components-placeholder {
6871
box-shadow: none;
6972
padding: 0;
73+
background-color: transparent;
7074
}
7175
}
7276

@@ -79,8 +83,8 @@
7983
opacity: 0.5;
8084

8185
* {
82-
color: $gray-200 !important;
83-
border-color: $gray-200 !important;
86+
color: $universal-border-light !important;
87+
border-color: $universal-border-light !important;
8488
}
8589
}
8690
.wp-block-woocommerce-classic-shortcode__placeholder-copy {

0 commit comments

Comments
 (0)