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

Commit 9b288cd

Browse files
authored
Featured Product: Try using Button block for button (#398)
* Try using Button block for the product button * Use the permalink for the selected product * Fix use of templateLock prop * Fix button component alignment
1 parent a36db08 commit 9b288cd

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

assets/js/blocks/featured-product/block.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import apiFetch from '@wordpress/api-fetch';
66
import {
77
AlignmentToolbar,
88
BlockControls,
9+
InnerBlocks,
910
InspectorControls,
1011
MediaUpload,
1112
MediaUploadCheck,
1213
PanelColorSettings,
13-
RichText,
1414
withColors,
1515
} from '@wordpress/editor';
1616
import {
@@ -218,7 +218,6 @@ class FeaturedProduct extends Component {
218218
dimRatio,
219219
editMode,
220220
height,
221-
linkText,
222221
showDesc,
223222
showPrice,
224223
} = attributes;
@@ -318,13 +317,19 @@ class FeaturedProduct extends Component {
318317
dangerouslySetInnerHTML={ { __html: product.price_html } }
319318
/>
320319
) }
321-
<div className="wc-block-featured-product__link wp-block-button">
322-
<RichText
323-
value={ linkText }
324-
onChange={ ( value ) => setAttributes( { linkText: value } ) }
325-
formattingControls={ [ 'bold', 'italic', 'strikethrough' ] }
326-
className="wp-block-button__link"
327-
keepPlaceholderOnFocus
320+
<div className="wc-block-featured-product__link">
321+
<InnerBlocks
322+
template={ [
323+
[
324+
'core/button',
325+
{
326+
text: __( 'Shop now', 'woo-gutenberg-products-block' ),
327+
url: product.permalink,
328+
align: 'center',
329+
},
330+
],
331+
] }
332+
templateLock="all"
328333
/>
329334
</div>
330335
</div>

assets/js/blocks/featured-product/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* External dependencies
33
*/
44
import { __ } from '@wordpress/i18n';
5+
import { InnerBlocks } from '@wordpress/editor';
56
import { registerBlockType } from '@wordpress/blocks';
67

78
/**
@@ -131,6 +132,6 @@ registerBlockType( 'woocommerce/featured-product', {
131132
* Block content is rendered in PHP, not via save function.
132133
*/
133134
save() {
134-
return null;
135+
return <InnerBlocks.Content />;
135136
},
136137
} );

assets/js/blocks/featured-product/style.scss

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535

3636
.wc-block-featured-product__title,
3737
.wc-block-featured-product__description,
38-
.wc-block-featured-product__price,
39-
.wc-block-featured-product__link {
38+
.wc-block-featured-product__price {
4039
margin-left: 0;
4140
text-align: left;
4241
}
@@ -47,23 +46,18 @@
4746

4847
.wc-block-featured-product__title,
4948
.wc-block-featured-product__description,
50-
.wc-block-featured-product__price,
51-
.wc-block-featured-product__link {
49+
.wc-block-featured-product__price {
5250
margin-right: 0;
5351
text-align: right;
5452
}
5553
}
5654

5755
.wc-block-featured-product__title,
5856
.wc-block-featured-product__description,
59-
.wc-block-featured-product__price,
60-
.wc-block-featured-product__link {
57+
.wc-block-featured-product__price {
6158
color: $white;
6259
line-height: 1.25;
63-
z-index: 1;
6460
margin-bottom: 0;
65-
width: 100%;
66-
padding: 0 48px 16px 48px;
6761
text-align: center;
6862

6963
a,
@@ -74,6 +68,15 @@
7468
}
7569
}
7670

71+
.wc-block-featured-product__title,
72+
.wc-block-featured-product__description,
73+
.wc-block-featured-product__price,
74+
.wc-block-featured-product__link {
75+
width: 100%;
76+
padding: 0 48px 16px 48px;
77+
z-index: 1;
78+
}
79+
7780
.wc-block-featured-product__title {
7881
margin-top: 0;
7982

includes/blocks/class-wc-block-featured-product.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class WC_Block_Featured_Product {
3030
'contentAlign' => 'center',
3131
'dimRatio' => 50,
3232
'height' => false,
33-
'linkText' => false,
3433
'mediaId' => 0,
3534
'mediaSrc' => '',
3635
'showDesc' => true,
@@ -51,9 +50,6 @@ public static function render( $attributes, $content ) {
5150
return '';
5251
}
5352
$attributes = wp_parse_args( $attributes, self::$defaults );
54-
if ( ! $attributes['linkText'] ) {
55-
$attributes['linkText'] = __( 'Shop now', 'woo-gutenberg-products-block' );
56-
}
5753
if ( ! $attributes['height'] ) {
5854
$attributes['height'] = wc_get_theme_support( 'featured_block::default_height', 500 );
5955
}
@@ -73,14 +69,6 @@ public static function render( $attributes, $content ) {
7369
$product->get_price_html()
7470
);
7571

76-
$link_str = sprintf(
77-
'<div class="wc-block-featured-product__link wp-block-button"><a class="wp-block-button__link" href="%1$s" aria-label="%2$s">%3$s</a></div>',
78-
$product->get_permalink(),
79-
/* translators: %s is product name */
80-
sprintf( __( 'View product %s', 'woo-gutenberg-products-block' ), $product->get_name() ),
81-
$attributes['linkText']
82-
);
83-
8472
$output = sprintf( '<div class="%1$s" style="%2$s">', self::get_classes( $attributes ), self::get_styles( $attributes, $product ) );
8573

8674
$output .= $title;
@@ -90,7 +78,7 @@ public static function render( $attributes, $content ) {
9078
if ( $attributes['showPrice'] ) {
9179
$output .= $price_str;
9280
}
93-
$output .= $link_str;
81+
$output .= '<div class="wc-block-featured-product__link">' . $content . '</div>';
9482
$output .= '</div>';
9583

9684
return $output;

includes/class-wgpb-products-controller.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ protected function get_product_data( $product, $context = 'view' ) {
276276

277277
$data['id'] = $raw_data['id'];
278278
$data['name'] = $raw_data['name'];
279+
$data['permalink'] = $raw_data['permalink'];
279280
$data['sku'] = $raw_data['sku'];
280281
$data['description'] = $raw_data['description'];
281282
$data['short_description'] = $raw_data['short_description'];
@@ -330,6 +331,7 @@ public function get_item_schema() {
330331

331332
$schema['properties']['id'] = $raw_schema['properties']['id'];
332333
$schema['properties']['name'] = $raw_schema['properties']['name'];
334+
$schema['properties']['permalink'] = $raw_schema['properties']['permalink'];
333335
$schema['properties']['sku'] = $raw_schema['properties']['sku'];
334336
$schema['properties']['description'] = $raw_schema['properties']['description'];
335337
$schema['properties']['short_description'] = $raw_schema['properties']['short_description'];

0 commit comments

Comments
 (0)