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

Cart and Checkout block transforms for classic shortcodes #11228

Merged
merged 29 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
64ea78e
Add transforms for checkout block
mikejolley Oct 12, 2023
cbac3af
Block to shortcode switcher in notice
mikejolley Oct 12, 2023
2256e97
cart transforms
mikejolley Oct 12, 2023
07b9a1c
Fix target block for switching
mikejolley Oct 16, 2023
ac821fb
Remove switcher UI for classic cart/checkout
mikejolley Oct 16, 2023
42735bf
Set isPreview when generating block preview in switcher
mikejolley Oct 16, 2023
0314cea
Onboarding task
mikejolley Oct 16, 2023
e5a3705
Action on click
mikejolley Oct 16, 2023
b3769e9
Focus on block after replacement
mikejolley Oct 16, 2023
35393c1
Update notice styling and wording
mikejolley Oct 16, 2023
4d571a9
Undo functionality
mikejolley Oct 17, 2023
9943f69
Look for woocommerce/classic-shortcode when determining if task list …
mikejolley Oct 18, 2023
5871548
Enable focus on the cart/checkout block when visiting from the task list
mikejolley Oct 18, 2023
851ea44
Classic Cart/Checkout Updated Title
mikejolley Oct 18, 2023
f1fbd8d
Add missing translations
mikejolley Oct 18, 2023
45d9543
Refactor modal content to avoid sprintf
mikejolley Oct 18, 2023
b16cd4c
Improve pickBlockClientId
mikejolley Oct 18, 2023
659926a
Tracks events for switching to classic shortcode block
mikejolley Oct 18, 2023
bdf0de6
TaskList support for non-block themes
mikejolley Oct 18, 2023
bb1cecd
Updated placeholder to work on non-white page backgrounds
mikejolley Oct 18, 2023
6d8673e
Find blocks using findBlock utility
mikejolley Oct 18, 2023
9128af9
Add TabbableContainer for buttons
mikejolley Oct 18, 2023
a43da7d
Add align to wrapper
mikejolley Oct 18, 2023
7038b0c
Update modal content
mikejolley Oct 19, 2023
6f2c76c
Update modal usage
mikejolley Oct 19, 2023
2babbab
Removed undo link when converting from classic shortcode
mikejolley Oct 19, 2023
fd5f1c1
Check if block was selected
mikejolley Oct 19, 2023
c279093
Revert "Removed undo link when converting from classic shortcode"
mikejolley Oct 19, 2023
a82f9d1
update snackbar text
mikejolley Oct 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 58 additions & 33 deletions assets/js/blocks/cart-checkout-shared/sidebar-notices/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,62 @@ const withSidebarNotices = createHigherOrderComponent(
setIsIncompatibleExtensionsNoticeDismissed( isDismissed );
};

const { isCart, isCheckout, isPaymentMethodsBlock, hasPaymentMethods } =
useSelect( ( select ) => {
const { getBlockParentsByBlockName, getBlockName } =
select( blockEditorStore );
const parent = getBlockParentsByBlockName( clientId, [
'woocommerce/cart',
'woocommerce/checkout',
] ).map( getBlockName );
const currentBlockName = getBlockName( clientId );
return {
isCart:
parent.includes( 'woocommerce/cart' ) ||
currentBlockName === 'woocommerce/cart',
isCheckout:
parent.includes( 'woocommerce/checkout' ) ||
currentBlockName === 'woocommerce/checkout',
isPaymentMethodsBlock:
currentBlockName ===
'woocommerce/checkout-payment-block',
hasPaymentMethods:
select(
PAYMENT_STORE_KEY
).paymentMethodsInitialized() &&
Object.keys(
select(
PAYMENT_STORE_KEY
).getAvailablePaymentMethods()
).length > 0,
};
} );
const {
isCart,
isCheckout,
isPaymentMethodsBlock,
hasPaymentMethods,
parentId,
} = useSelect( ( select ) => {
const { getBlockParentsByBlockName, getBlockName } =
select( blockEditorStore );

const parents = getBlockParentsByBlockName( clientId, [
'woocommerce/cart',
'woocommerce/checkout',
] ).reduce(
(
accumulator: Record< string, string >,
parentClientId: string
) => {
const parentName = getBlockName( parentClientId );
accumulator[ parentName ] = parentClientId;
return accumulator;
},
{}
);

const currentBlockName = getBlockName( clientId );
const parentBlockIsCart =
Object.keys( parents ).includes( 'woocommerce/cart' );
const parentBlockIsCheckout = Object.keys( parents ).includes(
'woocommerce/checkout'
);
const currentBlockIsCart =
currentBlockName === 'woocommerce/cart' || parentBlockIsCart;
const currentBlockIsCheckout =
currentBlockName === 'woocommerce/checkout' ||
parentBlockIsCheckout;
const targetParentBlock = currentBlockIsCart
? 'woocommerce/cart'
: 'woocommerce/checkout';

return {
isCart: currentBlockIsCart,
isCheckout: currentBlockIsCheckout,
parentId:
currentBlockName === targetParentBlock
? clientId
: parents[ targetParentBlock ],
isPaymentMethodsBlock:
currentBlockName === 'woocommerce/checkout-payment-block',
hasPaymentMethods:
select( PAYMENT_STORE_KEY ).paymentMethodsInitialized() &&
Object.keys(
select( PAYMENT_STORE_KEY ).getAvailablePaymentMethods()
).length > 0,
};
} );

// Show sidebar notices only when a WooCommerce block is selected.
if (
Expand All @@ -96,10 +122,9 @@ const withSidebarNotices = createHigherOrderComponent(
toggleIncompatibleExtensionsNoticeDismissedStatus
}
block={
isCheckout
? 'woocommerce/checkout'
: 'woocommerce/cart'
isCart ? 'woocommerce/cart' : 'woocommerce/checkout'
}
clientId={ parentId }
/>

<DefaultNotice block={ isCheckout ? 'checkout' : 'cart' } />
Expand Down
18 changes: 17 additions & 1 deletion assets/js/blocks/cart/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundar
import { EditorProvider, CartProvider } from '@woocommerce/base-context';
import { previewCart } from '@woocommerce/resource-previews';
import { SlotFillProvider } from '@woocommerce/blocks-checkout';
import { useEffect } from '@wordpress/element';
import { getQueryArg } from '@wordpress/url';
import { dispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -37,7 +40,7 @@ const ALLOWED_BLOCKS = [
'woocommerce/empty-cart-block',
];

export const Edit = ( { className, attributes, setAttributes } ) => {
export const Edit = ( { clientId, className, attributes, setAttributes } ) => {
const { hasDarkControls, currentView, isPreview = false } = attributes;
const defaultTemplate = [
[ 'woocommerce/filled-cart-block', {}, [] ],
Expand All @@ -49,6 +52,19 @@ export const Edit = ( { className, attributes, setAttributes } ) => {
} ),
} );

// This focuses on the block when a certain query param is found. This is used on the link from the task list.
const focus = getQueryArg( window.location.href, 'focus' );

useEffect( () => {
if ( focus === 'cart' ) {
dispatch( 'core/block-editor' ).selectBlock( clientId );
dispatch( 'core/interface' ).enableComplementaryArea(
'core/edit-site',
'edit-site/block-inspector'
);
}
}, [ clientId, focus ] );
mikejolley marked this conversation as resolved.
Show resolved Hide resolved

return (
<div { ...blockProps }>
<InspectorControls>
Expand Down
18 changes: 18 additions & 0 deletions assets/js/blocks/cart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ const settings = {
attributes: blockAttributes,
edit: Edit,
save: Save,
transforms: {
to: [
{
type: 'block',
blocks: [ 'woocommerce/classic-shortcode' ],
transform: ( attributes ) => {
return createBlock(
'woocommerce/classic-shortcode',
{
shortcode: 'cart',
align: attributes.align,
},
[]
);
},
},
],
},
// Migrates v1 to v2 checkout.
deprecated: [
{
Expand Down
18 changes: 18 additions & 0 deletions assets/js/blocks/checkout/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import {
} from '@wordpress/components';
import { SlotFillProvider } from '@woocommerce/blocks-checkout';
import type { TemplateArray } from '@wordpress/blocks';
import { useEffect } from '@wordpress/element';
import { getQueryArg } from '@wordpress/url';
import { dispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -46,9 +49,11 @@ const ALLOWED_BLOCKS: string[] = [
];

export const Edit = ( {
clientId,
attributes,
setAttributes,
}: {
clientId: string;
attributes: Attributes;
setAttributes: ( attributes: Record< string, unknown > ) => undefined;
} ): JSX.Element => {
Expand All @@ -66,6 +71,19 @@ export const Edit = ( {
isPreview = false,
} = attributes;

// This focuses on the block when a certain query param is found. This is used on the link from the task list.
const focus = getQueryArg( window.location.href, 'focus' );

useEffect( () => {
if ( focus === 'checkout' ) {
dispatch( 'core/block-editor' ).selectBlock( clientId );
dispatch( 'core/interface' ).enableComplementaryArea(
'core/edit-site',
'edit-site/block-inspector'
);
}
}, [ clientId, focus ] );

const defaultTemplate = [
[ 'woocommerce/checkout-fields-block', {}, [] ],
[ 'woocommerce/checkout-totals-block', {}, [] ],
Expand Down
18 changes: 18 additions & 0 deletions assets/js/blocks/checkout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ const settings = {
},
edit: Edit,
save: Save,
transforms: {
to: [
{
type: 'block',
blocks: [ 'woocommerce/classic-shortcode' ],
transform: ( attributes ) => {
return createBlock(
'woocommerce/classic-shortcode',
{
shortcode: 'checkout',
align: attributes.align,
},
[]
);
},
},
],
},
// Migrates v1 to v2 checkout.
deprecated: [
{
Expand Down
29 changes: 29 additions & 0 deletions assets/js/blocks/classic-shortcode/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "woocommerce/classic-shortcode",
"version": "1.0.0",
"title": "Classic Shortcode",
"description": "Renders classic WooCommerce shortcodes.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"supports": {
"align": true,
"html": false,
"multiple": false,
"reusable": false,
"inserter": true
},
"attributes": {
"shortcode": {
"type": "string",
"default": "cart",
"enum": [ "cart", "checkout" ]
},
"align": {
"type": "string",
"default": "wide"
}
},
"textdomain": "woo-gutenberg-products-block",
"apiVersion": 2,
"$schema": "https://schemas.wp.org/trunk/block.json"
}
2 changes: 1 addition & 1 deletion assets/js/blocks/classic-shortcode/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const onClickCallback = ( {
* Title shown within the block itself.
*/
const getTitle = () => {
return __( 'Cart Shortcode', 'woo-gutenberg-products-block' );
return __( 'Classic Cart', 'woo-gutenberg-products-block' );
};

/**
Expand Down
2 changes: 1 addition & 1 deletion assets/js/blocks/classic-shortcode/checkout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const onClickCallback = ( {
};

const getTitle = () => {
return __( 'Checkout Shortcode', 'woo-gutenberg-products-block' );
return __( 'Classic Checkout', 'woo-gutenberg-products-block' );
};
mikejolley marked this conversation as resolved.
Show resolved Hide resolved

const getDescription = () => {
Expand Down
2 changes: 1 addition & 1 deletion assets/js/blocks/classic-shortcode/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const TEMPLATES: TemplateDetails = {
},
checkout: {
type: TYPES.checkout,
title: __( 'Checkout Shortcode', 'woo-gutenberg-products-block' ),
title: __( 'Checkout Cart', 'woo-gutenberg-products-block' ),
description: __(
'Renders the classic checkout shortcode.',
'woo-gutenberg-products-block'
Expand Down
10 changes: 7 additions & 3 deletions assets/js/blocks/classic-shortcode/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,19 @@
pointer-events: none;

// Image based placeholders should fill horizontal width.
> img {
> img,
> svg {
width: 100%;
height: auto;
color: $universal-border-light;
}
}

.wp-block-woocommerce-classic-shortcode {
.components-placeholder {
box-shadow: none;
padding: 0;
background-color: transparent;
}
}

Expand All @@ -79,8 +83,8 @@
opacity: 0.5;

* {
color: $gray-200 !important;
border-color: $gray-200 !important;
color: $universal-border-light !important;
border-color: $universal-border-light !important;
}
}
.wp-block-woocommerce-classic-shortcode__placeholder-copy {
Expand Down
Loading
Loading