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

Add basic taxonomy block templates #5063

Merged
merged 4 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
38 changes: 29 additions & 9 deletions assets/js/blocks/legacy-template/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,33 @@
*/
import { __ } from '@wordpress/i18n';

export const TEMPLATE_TITLES: Record< string, string > = {
'single-product': __(
'WooCommerce Single Product Template',
'woo-gutenberg-products-block'
),
'archive-product': __(
'WooCommerce Product Archive Template',
'woo-gutenberg-products-block'
),
export const TEMPLATES: Record< string, Record< string, string > > = {
'single-product': {
title: __(
'WooCommerce Single Product Template',
'woo-gutenberg-products-block'
),
placeholder: 'single-product',
},
'archive-product': {
title: __(
'WooCommerce Product Archive Template',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
},
'taxonomy-product_cat': {
title: __(
'WooCommerce Product Taxonomy Template',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
},
'taxonomy-product_tag': {
title: __(
'WooCommerce Product Tag Template',
'woo-gutenberg-products-block'
),
placeholder: 'archive-product',
},
};
2 changes: 1 addition & 1 deletion assets/js/blocks/legacy-template/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
}

.editor-styles-wrapper .wp-block-woocommerce-legacy-template__placeholder-image {
.wp-block-woocommerce-legacy-template__placeholder .wp-block-woocommerce-legacy-template__placeholder-image {
display: none;
width: 100%;
height: auto;
Expand Down
8 changes: 5 additions & 3 deletions assets/js/blocks/legacy-template/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { page } from '@wordpress/icons';
* Internal dependencies
*/
import './editor.scss';
import { TEMPLATE_TITLES } from './constants';
import { TEMPLATES } from './constants';

interface Props {
attributes: {
Expand All @@ -25,7 +25,9 @@ interface Props {
const Edit = ( { attributes }: Props ) => {
const blockProps = useBlockProps();
const templateTitle =
TEMPLATE_TITLES[ attributes.template ] ?? attributes.template;
TEMPLATES[ attributes.template ]?.title ?? attributes.template;
const templatePlaceholder =
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice work, well considered!

TEMPLATES[ attributes.template ]?.placeholder ?? 'fallback';
return (
<div { ...blockProps }>
<Placeholder
Expand All @@ -46,7 +48,7 @@ const Edit = ( { attributes }: Props ) => {
<div className="wp-block-woocommerce-legacy-template__placeholder-wireframe">
<img
className="wp-block-woocommerce-legacy-template__placeholder-image"
src={ `${ WC_BLOCKS_IMAGE_URL }template-placeholders/${ attributes.template }.svg` }
src={ `${ WC_BLOCKS_IMAGE_URL }template-placeholders/${ templatePlaceholder }.svg` }
alt={ templateTitle }
/>
</div>
Expand Down
3 changes: 3 additions & 0 deletions images/template-placeholders/fallback.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,17 @@ public function render_block_template() {
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
is_tax() &&
( is_product_taxonomy() && is_tax( 'product_cat' ) ) &&
! $this->theme_has_template( 'taxonomy-product_cat' ) &&
$this->default_block_template_is_available( 'taxonomy-product_cat' )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
( is_product_taxonomy() && is_tax( 'product_tag' ) ) &&
! $this->theme_has_template( 'taxonomy-product_tag' ) &&
$this->default_block_template_is_available( 'taxonomy-product_tag' )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) &&
! $this->theme_has_template( 'archive-product' ) &&
Expand Down
2 changes: 1 addition & 1 deletion src/BlockTypes/LegacyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function render( $attributes, $content ) {

if ( 'single-product' === $attributes['template'] ) {
return $this->render_single_product();
} elseif ( 'archive-product' === $attributes['template'] ) {
} elseif ( 'archive-product' === $attributes['template'] || 'taxonomy-product_cat' === $attributes['template'] || 'taxonomy-product_tag' === $attributes['template'] ) {
frontdevde marked this conversation as resolved.
Show resolved Hide resolved
return $this->render_archive_product();
} else {
ob_start();
Expand Down
4 changes: 3 additions & 1 deletion src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ public static function convert_slug_to_title( $template_slug ) {
case 'archive-product':
return __( 'Product Archive Page', 'woo-gutenberg-products-block' );
case 'taxonomy-product_cat':
return __( 'Product Taxonomy Page', 'woo-gutenberg-products-block' );
return __( 'Product Category Page', 'woo-gutenberg-products-block' );
case 'taxonomy-product_tag':
return __( 'Product Tag Page', 'woo-gutenberg-products-block' );
default:
// Replace all hyphens and underscores with spaces.
return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) );
Expand Down
3 changes: 3 additions & 0 deletions templates/block-templates/taxonomy-product_cat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:template-part {"slug":"header"} /-->
<!-- wp:woocommerce/legacy-template {"template":"taxonomy-product_cat"} /-->
<!-- wp:template-part {"slug":"footer"} /-->
3 changes: 3 additions & 0 deletions templates/block-templates/taxonomy-product_tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- wp:template-part {"slug":"header"} /-->
<!-- wp:woocommerce/legacy-template {"template":"taxonomy-product_tag"} /-->
<!-- wp:template-part {"slug":"footer"} /-->