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

Commit d9c1471

Browse files
authored
Add to Cart Button > Update block registration to rely on metadata and enable usage for the Single Product Block (#9404)
* Adapt the Add to Cart Button block to be registered via block.json * Remove the legacy supports.ts file * Ditch all registration methods from the ProductButton class as it is now registered via metadata (a block.json file) * Update get_block_type_uses_context and register_block_type_assets
1 parent 3692f85 commit d9c1471

9 files changed

Lines changed: 98 additions & 144 deletions

File tree

assets/js/atomic/blocks/product-elements/button/attributes.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "woocommerce/product-button",
3+
"version": "1.0.0",
4+
"title": "Add to Cart Button",
5+
"description": "Display a call to action button which either adds the product to the cart, or links to the product page.",
6+
"category": "woocommerce",
7+
"keywords": [ "WooCommerce" ],
8+
"usesContext": [ "query", "queryId", "postId" ],
9+
"textdomain": "woo-gutenberg-products-block",
10+
"attributes": {
11+
"productId": {
12+
"type": "number",
13+
"default": 0
14+
},
15+
"textAlign": {
16+
"type": "string",
17+
"default": ""
18+
},
19+
"width": {
20+
"type": "number"
21+
},
22+
"isDescendentOfSingleProductBlock": {
23+
"type": "boolean",
24+
"default": false
25+
},
26+
"isDescendentOfQueryLoop": {
27+
"type": "boolean",
28+
"default": false
29+
}
30+
},
31+
"supports": {
32+
"align": [ "wide", "full" ],
33+
"color": {
34+
"background": false,
35+
"link": true
36+
},
37+
"html": false,
38+
"typography": {
39+
"fontSize": true,
40+
"lineHeight": true
41+
}
42+
},
43+
"ancestor": [
44+
"woocommerce/all-products",
45+
"woocommerce/single-product",
46+
"core/post-template"
47+
],
48+
"styles": [
49+
{
50+
"name": "fill",
51+
"label": "Fill",
52+
"isDefault": true
53+
},
54+
{
55+
"name": "outline",
56+
"label": "Outline"
57+
}
58+
],
59+
"apiVersion": 2,
60+
"$schema": "https://schemas.wp.org/trunk/block.json"
61+
}

assets/js/atomic/blocks/product-elements/button/constants.tsx

Lines changed: 0 additions & 19 deletions
This file was deleted.

assets/js/atomic/blocks/product-elements/button/index.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

assets/js/atomic/blocks/product-elements/button/supports.ts renamed to assets/js/atomic/blocks/product-elements/button/index.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
/* eslint-disable @wordpress/no-unsafe-wp-apis */
21
/**
32
* External dependencies
43
*/
4+
import { Icon, button } from '@wordpress/icons';
5+
import { registerBlockType } from '@wordpress/blocks';
56
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
67
import { __experimentalGetSpacingClassesAndStyles } from '@wordpress/block-editor';
8+
/**
9+
* Internal dependencies
10+
*/
11+
import edit from './edit';
12+
import save from './save';
13+
import metadata from './block.json';
714

8-
export const supports = {
15+
const featurePluginSupport = {
16+
...metadata.supports,
917
...( isFeaturePluginBuild() && {
1018
color: {
1119
text: true,
@@ -47,3 +55,22 @@ export const supports = {
4755
},
4856
} ),
4957
};
58+
// @ts-expect-error: `metadata` currently does not have a type definition in WordPress core
59+
registerBlockType( metadata, {
60+
icon: {
61+
src: (
62+
<Icon
63+
icon={ button }
64+
className="wc-block-editor-components-block-icon"
65+
/>
66+
),
67+
},
68+
attributes: {
69+
...metadata.attributes,
70+
},
71+
supports: {
72+
...featurePluginSupport,
73+
},
74+
edit,
75+
save,
76+
} );

assets/js/atomic/blocks/product-elements/button/save.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ type Props = {
1414
};
1515

1616
const Save = ( { attributes }: Props ): JSX.Element | null => {
17-
if ( attributes.isDescendentOfQueryLoop ) {
17+
if (
18+
attributes.isDescendentOfQueryLoop ||
19+
attributes.isDescendentOfSingleProductBlock
20+
) {
1821
return null;
1922
}
2023

assets/js/atomic/blocks/product-elements/button/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface BlockAttributes {
1010
className?: string | undefined;
1111
textAlign?: string | undefined;
1212
isDescendentOfQueryLoop?: boolean | undefined;
13+
isDescendentOfSingleProductBlock?: boolean | undefined;
1314
width?: number | undefined;
1415
}
1516

assets/js/blocks/products/base-utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import classnames from 'classnames';
66
/**
77
* Internal dependencies
88
*/
9-
import { BLOCK_NAME as PRODUCT_BUTTON_BLOCK_NAME } from '../../atomic/blocks/product-elements/button/constants';
9+
import addToCartButtonMetadata from '../../atomic/blocks/product-elements/button/block.json';
1010
import { ImageSizing } from '../../atomic/blocks/product-elements/image/types';
1111

1212
/**
@@ -45,7 +45,7 @@ export const getProductLayoutConfig = ( innerBlocks ) => {
4545
* This is needed to support "Width Setting" controls available in
4646
* "woocommerce/product-button" block.
4747
*/
48-
...( block.name === PRODUCT_BUTTON_BLOCK_NAME && {
48+
...( block.name === addToCartButtonMetadata.name && {
4949
className: classnames( block.attributes.className, {
5050
[ `has-custom-width wp-block-button__width-${ block.attributes?.width }` ]:
5151
block.attributes?.width,

src/BlockTypes/ProductButton.php

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,6 @@ class ProductButton extends AbstractBlock {
1515
*/
1616
protected $block_name = 'product-button';
1717

18-
/**
19-
* API version name.
20-
*
21-
* @var string
22-
*/
23-
protected $api_version = '2';
24-
25-
/**
26-
* Get block supports. Shared with the frontend.
27-
* IMPORTANT: If you change anything here, make sure to update the JS file too.
28-
*
29-
* @return array
30-
*/
31-
protected function get_block_type_supports() {
32-
return array(
33-
'color' =>
34-
array(
35-
'background' => true,
36-
'link' => false,
37-
'text' => true,
38-
'__experimentalSkipSerialization' => true,
39-
),
40-
'__experimentalBorder' =>
41-
array(
42-
'radius' => true,
43-
'__experimentalSkipSerialization' => true,
44-
),
45-
'typography' =>
46-
array(
47-
'fontSize' => true,
48-
'__experimentalFontWeight' => true,
49-
'__experimentalSkipSerialization' => true,
50-
),
51-
'__experimentalSelector' => '.wp-block-button.wc-block-components-product-button .wc-block-components-product-button__button',
52-
);
53-
}
54-
5518
/**
5619
* It is necessary to register and enqueue assets during the render phase because we want to load assets only if the block has the content.
5720
*/
@@ -75,12 +38,6 @@ protected function get_block_type_uses_context() {
7538
* @return string Rendered block type output.
7639
*/
7740
protected function render( $attributes, $content, $block ) {
78-
if ( ! empty( $content ) ) {
79-
parent::register_block_type_assets();
80-
$this->register_chunk_translations( [ $this->block_name ] );
81-
return $content;
82-
}
83-
8441
$post_id = $block->context['postId'];
8542
$product = wc_get_product( $post_id );
8643

@@ -91,7 +48,7 @@ protected function render( $attributes, $content, $block ) {
9148
$html_element = $is_ajax_button ? 'button' : 'a';
9249
$styles_and_classes = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
9350
$text_align_styles_and_classes = StyleAttributesUtils::get_text_align_class_and_style( $attributes );
94-
$classname = isset( $attributes['className'] ) ? $attributes['className'] : '';
51+
$classname = $attributes['className'] ?? '';
9552
$custom_width_classes = isset( $attributes['width'] ) ? 'has-custom-width wp-block-button__width-' . $attributes['width'] : '';
9653
$html_classes = implode(
9754
' ',

0 commit comments

Comments
 (0)