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

Add Results Count block #8078

Merged
merged 33 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2158349
Init setup to add a new block Results Count
kmanijak Jan 2, 2023
249766f
Render template part as a content of ResultsCount block
kmanijak Jan 3, 2023
7a60255
Switch to template part rendering
kmanijak Jan 3, 2023
0aca222
Rename the block to ProductResultsCount
kmanijak Jan 5, 2023
9d421ec
Fix typo in BlockTypesController
kmanijak Jan 5, 2023
f808f1a
Change the ProductResultsCount class name
kmanijak Jan 5, 2023
57e0ead
Remove Product Results Count block from Product Query template
kmanijak Jan 13, 2023
cf2506b
Improve the way Product Results Count is rendered in the editor
kmanijak Jan 13, 2023
77ccba8
Add prefix to places that missed renaming from ResultsCount to Produ…
kmanijak Jan 13, 2023
1c4bb9b
Remove unnecessary frontend.tsx file for ProductResultsCount
kmanijak Jan 13, 2023
1b57a23
Merge branch 'trunk' into add/product-count
kmanijak Jan 13, 2023
446b0a5
Make sure global styles are applied and respected by Product Results …
kmanijak Jan 13, 2023
678e682
Make sure the Product Results Count is available inly in Product Cata…
kmanijak Jan 13, 2023
e2b2ad8
Add basic tests to Product Results Count
kmanijak Jan 13, 2023
b5d9d43
Remove empty line in style file
kmanijak Jan 13, 2023
de6febf
Fix TS issue in Product Results Count
kmanijak Jan 13, 2023
9af76e7
Fix typo
kmanijak Jan 13, 2023
115e237
Override the enqueue_assets method to prevent unnecessary enqueue and…
kmanijak Jan 13, 2023
5cbd230
Add necessary property to block's metadata
kmanijak Jan 13, 2023
b757998
Address code review feedback
kmanijak Jan 13, 2023
715da78
Update description
albarin Jan 16, 2023
6db9cd1
Remove disabled component
albarin Jan 16, 2023
8226dfe
Improve test description
albarin Jan 16, 2023
45d6748
Merge edit and block files
albarin Jan 16, 2023
87ba04e
Merge branch 'trunk' into add/product-count
albarin Jan 16, 2023
0e80c2d
Remove align support
albarin Jan 17, 2023
c6d51d8
Remove background support
albarin Jan 17, 2023
e2dedbb
Simplify edit component
albarin Jan 17, 2023
d024030
Improve readability with sprintf and add custom class
albarin Jan 17, 2023
8ddc2fa
Simplify test case
albarin Jan 17, 2023
b780b07
Merge branch 'trunk' into add/product-count
albarin Jan 17, 2023
c2b1d63
Add styles and order placeholders
albarin Jan 17, 2023
8ec834c
Fix markup to match with the frontend
albarin Jan 17, 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
22 changes: 22 additions & 0 deletions assets/js/blocks/product-results-count/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "woocommerce/product-results-count",
"version": "1.0.0",
"title": "Product Results Count",
"description": "Display the number of products on the archive page or search result page.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"supports": {
"align": true,
albarin marked this conversation as resolved.
Show resolved Hide resolved
"color": {
"text": true
albarin marked this conversation as resolved.
Show resolved Hide resolved
},
"typography": {
"fontSize": true,
"__experimentalFontFamily": true
}
},
"attributes": {},
"textdomain": "woo-gutenberg-products-block",
"apiVersion": 2,
"$schema": "https://schemas.wp.org/trunk/block.json"
}
36 changes: 36 additions & 0 deletions assets/js/blocks/product-results-count/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* External dependencies
*/
import classNames from 'classnames';
import { useBlockProps } from '@wordpress/block-editor';
import type { BlockEditProps } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';

export interface Attributes {
className?: string;
}

const ProductResultsCount = (): JSX.Element => {
return (
<div>
{ __( 'Showing 1-X of X results', 'woo-gutenberg-products-block' ) }
</div>
albarin marked this conversation as resolved.
Show resolved Hide resolved
);
};

const Edit = ( { attributes }: BlockEditProps< Attributes > ) => {
const { className } = attributes;
const blockProps = useBlockProps( {
className: classNames( 'wc-block-product-results-count', className ),
albarin marked this conversation as resolved.
Show resolved Hide resolved
} );

return (
<>
<div { ...blockProps }>
<ProductResultsCount />
</div>
</>
);
};

export default Edit;
30 changes: 30 additions & 0 deletions assets/js/blocks/product-results-count/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* External dependencies
*/
import { registerBlockType } from '@wordpress/blocks';
import { Icon } from '@wordpress/icons';
import { totals } from '@woocommerce/icons';

/**
* Internal dependencies
*/
import metadata from './block.json';
import edit from './edit';

registerBlockType( metadata, {
icon: {
src: (
<Icon
icon={ totals }
className="wc-block-editor-components-block-icon"
/>
),
},
attributes: {
...metadata.attributes,
},
edit,
save() {
return null;
},
} );
9 changes: 9 additions & 0 deletions assets/js/blocks/product-results-count/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// For now, we use the PHP template to render that element, which has
// high specificity styles applied. To overcome that double-class
// selector is used here
.woocommerce.wc-block-product-results-count {
.woocommerce-result-count {
float: none;
font-size: inherit;
}
}
1 change: 1 addition & 0 deletions bin/webpack-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const blocks = {
'product-query': {
isExperimental: true,
},
'product-results-count': {},
'product-search': {},
'product-tag': {},
'product-top-rated': {},
Expand Down
46 changes: 46 additions & 0 deletions src/BlockTypes/ProductResultsCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Automattic\WooCommerce\Blocks\BlockTypes;

use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;

/**
* ProductResultsCount class.
*/
class ProductResultsCount extends AbstractBlock {

/**
* Block name.
*
* @var string
*/
protected $block_name = 'product-results-count';

/**
* Get the frontend script handle for this block type.
*
* @param string $key Data to get, or default to everything.
*/
protected function get_block_type_script( $key = null ) {
return null;
}

/**
* Render the block.
*
* @param array $attributes Block attributes.
* @param string $content Block content.
* @param WP_Block $block Block instance.
*
* @return string Rendered block output.
*/
protected function render( $attributes, $content, $block ) {
ob_start();
woocommerce_result_count();
$product_results_count = ob_get_clean();

$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );

return "<div class='woocommerce wc-block-product-results-count " . esc_attr( $classes_and_styles['classes'] ) . "' style='" . esc_attr( $classes_and_styles['styles'] ) . "'>" . $product_results_count . '</div>';
albarin marked this conversation as resolved.
Show resolved Hide resolved
albarin marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 2 additions & 0 deletions src/BlockTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ protected function get_block_types() {
'ProductPrice',
'ProductQuery',
'ProductRating',
'ProductResultsCount',
'ProductSaleBadge',
'ProductSearch',
'ProductSKU',
Expand Down Expand Up @@ -247,6 +248,7 @@ protected function get_block_types() {
$block_types,
[
'ClassicTemplate',
'ProductResultsCount',
]
);
}
Expand Down
66 changes: 66 additions & 0 deletions tests/e2e/specs/backend/product-results-count.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* External dependencies
*/
import {
canvas,
createNewPost,
insertBlock,
switchUserToAdmin,
} from '@wordpress/e2e-test-utils';
import { searchForBlock } from '@wordpress/e2e-test-utils/build/inserter';

/**
* Internal dependencies
*/
import {
filterCurrentBlocks,
goToSiteEditor,
useTheme,
waitForCanvas,
} from '../../utils.js';

const block = {
name: 'Product Results Count',
slug: 'woocommerce/product-results-count',
class: '.wc-block-product-results-count',
};

describe( `${ block.name } Block`, () => {
describe( 'in a post', () => {
beforeAll( async () => {
await switchUserToAdmin();
} );

it( 'can not be inserted', async () => {
await createNewPost( {
postType: 'post',
title: block.name,
} );
await searchForBlock( block.name );
expect( page ).toMatch( 'No results found.' );
} );
} );
albarin marked this conversation as resolved.
Show resolved Hide resolved

describe( 'in FSE editor', () => {
useTheme( 'emptytheme' );

beforeEach( async () => {
await goToSiteEditor();
await waitForCanvas();
} );

it( 'can be inserted in FSE area', async () => {
await insertBlock( block.name );
await expect( canvas() ).toMatchElement( block.class );
} );

it( 'can be inserted more than once', async () => {
await insertBlock( block.name );
await insertBlock( block.name );
const foo = await filterCurrentBlocks(
( b ) => b.name === block.slug
);
expect( foo ).toHaveLength( 2 );
} );
} );
} );