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

[Blockifying Product Archive Templates]: Implement the blockified template conversion for the Classic Template Block. #8248

Merged
merged 57 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
2fddc21
First try to render Product Arhcive template as blocks
kmanijak Jan 20, 2023
d5eb4e3
First iteration of the whole blockified product archive template
kmanijak Jan 20, 2023
5f95c8d
Extract a blockified Product Archive Template to the templates
kmanijak Jan 21, 2023
7b94fc5
Add templates to tsconfig.json so the files are resolved as part of t…
kmanijak Jan 21, 2023
a5b2a79
Add a encouragement note to use blockified version IF migration is av…
kmanijak Jan 21, 2023
c3bbc70
Add a Row block wrapping Product Results Count and Catalog Sorting bl…
kmanijak Jan 24, 2023
a185597
Move blockified product-archive from templates/ to assets/ directory
kmanijak Jan 24, 2023
e9dee03
Remove unnecessary margin from Product Results Count block
kmanijak Jan 24, 2023
6e49f88
Fix TypeScript error
kmanijak Jan 24, 2023
e6922a9
Update the description of the Classic Template Editor placeholder
kmanijak Jan 24, 2023
012a03f
Remove unnecessary entry in tsconfig.json to include templates directory
kmanijak Jan 24, 2023
12d4c62
Move the to
kmanijak Jan 24, 2023
52d1313
Differentiate the Classic Template placeholder description depending …
kmanijak Jan 24, 2023
a52107d
Add E2E test that the Classic Template can be replaced with the block…
kmanijak Jan 24, 2023
b57cc98
Abandon writing E2E tests for now, as there's a problem to run them l…
kmanijak Jan 25, 2023
610eeda
Merge branch 'trunk' into add/convert-classic-template
kmanijak Jan 25, 2023
a203496
Syntax improvement
kmanijak Jan 25, 2023
b603490
Set margin for Catalog Sorting to 0, so it aligns properly when used …
kmanijak Jan 25, 2023
847fb46
Make the blockification config, so it covers the Product Archive as w…
kmanijak Jan 25, 2023
c812ba5
Merge branch 'add/convert-classic-template' of github.com:woocommerce…
kmanijak Jan 25, 2023
bda27ac
Move the product-archive specific functions from classic-template/ind…
kmanijak Jan 25, 2023
88ddb2a
Add alignment option to the Store Notices block and make the blockifi…
kmanijak Jan 26, 2023
7427a4b
Merge branch 'trunk' into add/convert-classic-template
kmanijak Jan 26, 2023
a64835e
Create single-product.ts file which is a placeholder for the blockifi…
kmanijak Jan 26, 2023
36f740b
Make Blockified Product Archive template inherit the align attibute
kmanijak Jan 27, 2023
c2c5657
Simplify the interface of blockified templates
kmanijak Jan 27, 2023
7f1d2eb
Add a BlockifiedTemplate type
kmanijak Jan 27, 2023
4a4b8e5
Rename and simplify the function checking if conversion of classic te…
kmanijak Jan 27, 2023
f14f657
Merge branch 'add/convert-classic-template' of github.com:woocommerce…
kmanijak Jan 27, 2023
5505f02
Align the variable naming to use instead of
kmanijak Jan 27, 2023
6d8607c
Pass the Classic Template attributes to the blockified template inste…
kmanijak Jan 30, 2023
3afd81e
Merge branch 'trunk' into add/convert-classic-template
kmanijak Jan 30, 2023
a3b50bd
Include Breadcrumbs block in the Blockified Product Archive Template
kmanijak Jan 30, 2023
435b7f4
Consume alignment attributes of Catalog Sorting in the PHP render fun…
kmanijak Jan 30, 2023
8a36881
Consume alignment attributes of Breadcrumbs in the PHP render function
kmanijak Jan 30, 2023
11c587a
Remove align support from Catalog Sorting and add to Store Notices block
kmanijak Jan 30, 2023
cd71a6a
Extend the get_classes_and_styles_by_attributes method with align and…
kmanijak Jan 31, 2023
fe5f1a8
Add Archive Title block to the Blockified Classic Template
kmanijak Jan 31, 2023
adcec0b
Minor getRowBlock function refactor
kmanijak Jan 31, 2023
31a5d4e
Add property to the classic templates and base the config on it inst…
kmanijak Jan 31, 2023
9e2d999
Add separate blockified template for a Product Search Results
kmanijak Jan 31, 2023
295e601
Pass attribute from classic template to No Results block in Product …
kmanijak Jan 31, 2023
18dea27
Extract the common functions between blockified archive-template and …
kmanijak Feb 1, 2023
39e6c73
Enable 'Inherit query from template' in Products block by default whe…
kmanijak Feb 1, 2023
713cc5e
Merge branch 'trunk' into add/convert-classic-template
kmanijak Feb 1, 2023
1722726
Improve the naming of BlockifiedTemplateConfig type
kmanijak Feb 2, 2023
11a90df
Differentiate Product Catalog and Products By * templates. The latter…
kmanijak Feb 2, 2023
35c8dc1
Merge branch 'add/convert-classic-template' of github.com:woocommerce…
kmanijak Feb 2, 2023
7f6a438
Change unclear ProductsBy to ProductTaxonomy in regards to classic te…
kmanijak Feb 3, 2023
3ba2516
Revert the margin fix which was added already on trunk
kmanijak Feb 14, 2023
5819006
Merge branch 'trunk' into add/convert-classic-template
kmanijak Feb 14, 2023
f925c7b
Move the surrounding blocks as inner blocks of Products
kmanijak Feb 22, 2023
72ac96f
Merge branch 'trunk' into add/convert-classic-template
kmanijak Feb 22, 2023
462fa4a
Hide the conversion behind the experimental build flag (as it was bef…
kmanijak Feb 23, 2023
e79b6f6
Merge branch 'trunk' into add/convert-classic-template
kmanijak Feb 23, 2023
431b78a
Merge branch 'trunk' into add/convert-classic-template
kmanijak Feb 27, 2023
a384c13
Merge branch 'trunk' into add/convert-classic-template
kmanijak Feb 27, 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
1 change: 1 addition & 0 deletions assets/js/blocks/catalog-sorting/style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.woocommerce.wc-block-catalog-sorting {
form {
float: none;
margin: 0;
}

select {
Expand Down
112 changes: 112 additions & 0 deletions assets/js/blocks/classic-template/archive-product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* External dependencies
*/
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
type BlockInstance,
} from '@wordpress/blocks';
import { isWpVersion } from '@woocommerce/settings';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import {
INNER_BLOCKS_TEMPLATE as productsInnerBlocksTemplate,
QUERY_DEFAULT_ATTRIBUTES as productsQueryDefaultAttributes,
} from '../product-query/constants';
import { VARIATION_NAME as productsVariationName } from '../product-query/variations/product-query';
import { createArchiveTitleBlock, createRowBlock } from './utils';
import { type InheritedAttributes } from './types';

const createProductsBlock = ( inheritedAttributes: InheritedAttributes ) =>
createBlock(
'core/query',
{
...productsQueryDefaultAttributes,
...inheritedAttributes,
namespace: productsVariationName,
query: {
...productsQueryDefaultAttributes.query,
inherit: true,
},
},
createBlocksFromInnerBlocksTemplate( productsInnerBlocksTemplate )
);

const getBlockifiedTemplate = (
inheritedAttributes: InheritedAttributes,
withTermDescription = false
) =>
[
createBlock( 'woocommerce/breadcrumbs', inheritedAttributes ),
createArchiveTitleBlock( 'archive-title', inheritedAttributes ),
withTermDescription
? createBlock( 'core/term-description', inheritedAttributes )
: null,
createBlock( 'woocommerce/store-notices', inheritedAttributes ),
createRowBlock(
[
createBlock( 'woocommerce/product-results-count' ),
createBlock( 'woocommerce/catalog-sorting' ),
],
inheritedAttributes
),
createProductsBlock( inheritedAttributes ),
].filter( Boolean ) as BlockInstance[];

const getBlockifiedTemplateWithTermDescription = (
inheritedAttributes: InheritedAttributes
) => getBlockifiedTemplate( inheritedAttributes, true );

const isConversionPossible = () => {
// Blockification is possible for the WP version 6.1 and above,
// which are the versions the Products block supports.
return isWpVersion( '6.1', '>=' );
dinhtungdu marked this conversation as resolved.
Show resolved Hide resolved
};

const getDescriptionAllowingConversion = ( templateTitle: string ) =>
sprintf(
/* translators: %s is the template title */
__(
"This block serves as a placeholder for your %s. We recommend upgrading to the Products block for more features to edit your products visually. Don't worry, you can always revert back.",
'woo-gutenberg-products-block'
),
templateTitle
);

const getDescriptionDisallowingConversion = ( templateTitle: string ) =>
sprintf(
/* translators: %s is the template title */
__(
'This block serves as a placeholder for your %s. It will display the actual product image, title, price in your store. You can move this placeholder around and add more blocks around to customize the template.',
'woo-gutenberg-products-block'
),
templateTitle
);
kmanijak marked this conversation as resolved.
Show resolved Hide resolved

const getDescription = ( templateTitle: string, canConvert: boolean ) => {
if ( canConvert ) {
return getDescriptionAllowingConversion( templateTitle );
}

return getDescriptionDisallowingConversion( templateTitle );
};

const getButtonLabel = () =>
__( 'Upgrade to Products block', 'woo-gutenberg-products-block' );

export const blockifiedProductCatalogConfig = {
getBlockifiedTemplate,
isConversionPossible,
getDescription,
getButtonLabel,
};

export const blockifiedProductsByConfig = {
getBlockifiedTemplate: getBlockifiedTemplateWithTermDescription,
isConversionPossible,
getDescription,
getButtonLabel,
};
31 changes: 24 additions & 7 deletions assets/js/blocks/classic-template/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,71 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
export const BLOCK_SLUG = 'woocommerce/legacy-template';

/**
* Internal dependencies
*/
import { TemplateDetails } from './types';

export const BLOCK_SLUG = 'woocommerce/legacy-template';
export const TYPES = {
singleProduct: 'single-product',
productCatalog: 'product-catalog',
productsBy: 'products-by',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, it's a bit unclear what productsBy means. Can we consider other alternative here? Somethings like productTaxonomy, productLoop, productGrid? or even keep archiveProduct for shared template, and use productCatalog just for the catalog page.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, it's a bit unclear what productsBy means.

Thanks for feedback, makes sense! I Changed that to productTaxonomy (commit)

productSearchResults: 'product-search-results',
};
export const PLACEHOLDERS = {
singleProduct: 'single-product',
archiveProduct: 'archive-product',
};

export const TEMPLATES: TemplateDetails = {
'single-product': {
type: TYPES.singleProduct,
title: __(
'WooCommerce Single Product Block',
'woo-gutenberg-products-block'
),
placeholder: 'single-product',
placeholder: PLACEHOLDERS.singleProduct,
},
'archive-product': {
type: TYPES.productCatalog,
title: __(
'WooCommerce Product Grid Block',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
placeholder: PLACEHOLDERS.archiveProduct,
},
'taxonomy-product_cat': {
type: TYPES.productsBy,
title: __(
'WooCommerce Product Taxonomy Block',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
placeholder: PLACEHOLDERS.archiveProduct,
},
'taxonomy-product_tag': {
type: TYPES.productsBy,
title: __(
'WooCommerce Product Tag Block',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
placeholder: PLACEHOLDERS.archiveProduct,
},
'taxonomy-product_attribute': {
type: TYPES.productsBy,
title: __(
'WooCommerce Product Attribute Block',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
placeholder: PLACEHOLDERS.archiveProduct,
},
'product-search-results': {
type: TYPES.productSearchResults,
title: __(
'WooCommerce Product Search Results Block',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
placeholder: PLACEHOLDERS.archiveProduct,
},
};
74 changes: 39 additions & 35 deletions assets/js/blocks/classic-template/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import {
createBlock,
getBlockType,
registerBlockType,
unregisterBlockType,
Expand All @@ -14,7 +13,7 @@ import {
} from '@woocommerce/block-settings';
import { useBlockProps } from '@wordpress/block-editor';
import { Button, Placeholder } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';
import { box, Icon } from '@wordpress/icons';
import { select, useDispatch, subscribe } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
Expand All @@ -24,18 +23,40 @@ import { useEffect } from '@wordpress/element';
*/
import './editor.scss';
import './style.scss';
import { BLOCK_SLUG, TEMPLATES } from './constants';
import { BLOCK_SLUG, TEMPLATES, TYPES } from './constants';
import {
isClassicTemplateBlockRegisteredWithAnotherTitle,
hasTemplateSupportForClassicTemplateBlock,
getTemplateDetailsBySlug,
} from './utils';
import {
blockifiedProductCatalogConfig,
blockifiedProductsByConfig,
} from './archive-product';
import * as blockifiedSingleProduct from './single-product';
import * as blockifiedProductSearchResults from './product-search-results';
import type { BlockifiedTemplateConfig } from './types';

type Attributes = {
template: string;
align: string;
};

const blockifiedFallbackConfig = {
isConversionPossible: () => false,
getBlockifiedTemplate: () => [],
getDescription: () => '',
getButtonLabel: () => '',
};

const conversionConfig: { [ key: string ]: BlockifiedTemplateConfig } = {
[ TYPES.productCatalog ]: blockifiedProductCatalogConfig,
[ TYPES.productsBy ]: blockifiedProductsByConfig,
[ TYPES.singleProduct ]: blockifiedSingleProduct,
[ TYPES.productSearchResults ]: blockifiedProductSearchResults,
fallback: blockifiedFallbackConfig,
};

const Edit = ( {
clientId,
attributes,
Expand All @@ -50,6 +71,7 @@ const Edit = ( {
);
const templateTitle = templateDetails?.title ?? attributes.template;
const templatePlaceholder = templateDetails?.placeholder ?? 'fallback';
const templateType = templateDetails?.type ?? 'fallback';

useEffect(
() =>
Expand All @@ -60,6 +82,16 @@ const Edit = ( {
[ attributes.align, attributes.template, setAttributes ]
);

const {
getBlockifiedTemplate,
isConversionPossible,
getDescription,
getButtonLabel,
} = conversionConfig[ templateType ];

const canConvert = isConversionPossible();
const placeholderDescription = getDescription( templateTitle, canConvert );

return (
<div { ...blockProps }>
<Placeholder
Expand All @@ -68,48 +100,20 @@ const Edit = ( {
className="wp-block-woocommerce-classic-template__placeholder"
>
<div className="wp-block-woocommerce-classic-template__placeholder-copy">
<p className="wp-block-woocommerce-classic-template__placeholder-warning">
<strong>
{ __(
'Do not remove this block!',
'woo-gutenberg-products-block'
) }
</strong>{ ' ' }
{ __(
'Removing this will cause unintended effects on your store.',
'woo-gutenberg-products-block'
) }
</p>
<p>
{ sprintf(
/* translators: %s is the template title */
__(
'This is a placeholder for the %s. In your store it will display the actual product image, title, price, etc. You can move this placeholder around and add more blocks around it to customize the template.',
'woo-gutenberg-products-block'
),
templateTitle
) }
</p>
<p>{ placeholderDescription }</p>
</div>
<div className="wp-block-woocommerce-classic-template__placeholder-wireframe">
{ isExperimentalBuild() && (
{ canConvert && (
kmanijak marked this conversation as resolved.
Show resolved Hide resolved
<div className="wp-block-woocommerce-classic-template__placeholder-migration-button-container">
<Button
isPrimary
onClick={ () => {
replaceBlock(
clientId,
// TODO: Replace with the blockified version of the Product Grid Block when it will be available.
createBlock( 'core/paragraph', {
content:
'Instead of this block, the new Product Grid Block will be rendered',
} )
getBlockifiedTemplate( attributes )
);
} }
text={ __(
'Use the blockified Product Grid Block',
'woo-gutenberg-products-block'
) }
text={ getButtonLabel() }
/>
</div>
) }
Expand Down
Loading