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

Add Catalog Sorting filter block #8122

Merged
merged 30 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
214049c
WIP
albarin Jan 3, 2023
88e0e6e
Render in editor and add styles
albarin Jan 9, 2023
55361f8
Simplify select
albarin Jan 9, 2023
27434e2
Undo change
albarin Jan 9, 2023
37c6904
Update titles and description
albarin Jan 9, 2023
030d73e
Wrapped text for translation
albarin Jan 10, 2023
0597f7c
Remove from products inner blocks
albarin Jan 10, 2023
98a360a
Allow the block in the inserter
albarin Jan 10, 2023
0280143
Rename test file and add new tests for catalog-sorting block
albarin Jan 10, 2023
74393de
Register on the site editor only
albarin Jan 11, 2023
137fb10
Fix tests
albarin Jan 12, 2023
a437c98
Merge branch 'trunk' into add/8061-sorting-filter-block
albarin Jan 13, 2023
9e14054
Disable block server side
albarin Jan 13, 2023
3cf2cb5
Override the `enqueue_assets`
albarin Jan 13, 2023
e6b315f
Fix TS error
albarin Jan 13, 2023
ff5f63e
Merge branch 'trunk' into add/8061-sorting-filter-block
albarin Jan 13, 2023
8fc37d0
Simplify edit
albarin Jan 16, 2023
f338b1f
Disable background color
albarin Jan 16, 2023
0fc9f12
Add custom classes
albarin Jan 16, 2023
2b8ff32
Remove alignment
albarin Jan 16, 2023
e40ecf4
Merge branch 'trunk' into add/8061-sorting-filter-block
albarin Jan 16, 2023
593211a
Inherit color and font properties
albarin Jan 16, 2023
65b313f
Merge edit and block files
albarin Jan 16, 2023
f8ce5c7
Merge branch 'trunk' into add/8061-sorting-filter-block
albarin Jan 16, 2023
47f72cb
Use sprintf to improve code readability
albarin Jan 17, 2023
aeb294d
Make fontsize small by default
albarin Jan 17, 2023
cb5b019
Merge branch 'trunk' into add/8061-sorting-filter-block
albarin Jan 17, 2023
253380f
Add order placeholders and woocommerce class
albarin Jan 17, 2023
cc652c1
Fix floating issue
albarin Jan 17, 2023
0f84f70
Add woocommerce class
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
27 changes: 27 additions & 0 deletions assets/js/blocks/catalog-sorting/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "woocommerce/catalog-sorting",
"version": "1.0.0",
"title": "Catalog Sorting",
"description": "Enable customers to change the sorting order of the products.",
"category": "woocommerce",
"keywords": [ "WooCommerce" ],
"supports": {
"color": {
"text": true,
"background": false
},
"typography": {
"fontSize": true,
"__experimentalFontFamily": true
}
},
"attributes": {
"fontSize": {
"type": "string",
"default": "small"
}
},
"textdomain": "woo-gutenberg-products-block",
"apiVersion": 2,
"$schema": "https://schemas.wp.org/trunk/block.json"
}
34 changes: 34 additions & 0 deletions assets/js/blocks/catalog-sorting/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import { Disabled } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

const CatalogSorting = () => {
return (
<select>
<option>
{ __( 'Default sorting', 'woo-gutenberg-products-block' ) }
</option>
</select>
);
};

const Edit = () => {
const blockProps = useBlockProps( {
className: 'woocommerce wc-block-catalog-sorting',
} );

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

export default Edit;
30 changes: 30 additions & 0 deletions assets/js/blocks/catalog-sorting/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 }
albarin marked this conversation as resolved.
Show resolved Hide resolved
className="wc-block-editor-components-block-icon"
/>
),
},
attributes: {
...metadata.attributes,
},
edit,
save() {
return null;
},
} );
11 changes: 11 additions & 0 deletions assets/js/blocks/catalog-sorting/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.woocommerce.wc-block-catalog-sorting {
form {
float: none;
}

select {
color: inherit !important;
font-size: inherit;
font-family: inherit;
}
}
1 change: 1 addition & 0 deletions bin/webpack-entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const blocks = {
},
'attribute-filter': {},
cart: {},
'catalog-sorting': {},
checkout: {},
'customer-account': {},
'featured-category': {
Expand Down
56 changes: 56 additions & 0 deletions src/BlockTypes/CatalogSorting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Automattic\WooCommerce\Blocks\BlockTypes;

use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;

/**
* CatalogSorting class.
*/
class CatalogSorting extends AbstractBlock {

/**
* Block name.
*
* @var string
*/
protected $block_name = 'catalog-sorting';

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

if ( ! $catalog_sorting ) {
return;
}

$classname = isset( $attributes['className'] ) ? $attributes['className'] : '';
$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
return sprintf(
'<div class="woocommerce wc-block-catalog-sorting %1$s %2$s" style="%3$s">%4$s</div>',
esc_attr( $classes_and_styles['classes'] ),
esc_attr( $classname ),
esc_attr( $classes_and_styles['styles'] ),
$catalog_sorting
);
}

/**
* 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;
}
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 @@ -168,6 +168,7 @@ protected function get_block_types() {
'AllProducts',
'AllReviews',
'AttributeFilter',
'CatalogSorting',
'ClassicTemplate',
'CustomerAccount',
'FeaturedCategory',
Expand Down Expand Up @@ -246,6 +247,7 @@ protected function get_block_types() {
$block_types = array_diff(
$block_types,
[
'CatalogSorting',
'ClassicTemplate',
]
);
Expand Down
66 changes: 66 additions & 0 deletions tests/e2e/specs/backend/catalog-sorting.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: 'Catalog Sorting',
slug: 'woocommerce/catalog-sorting',
class: '.wc-block-catalog-sorting',
};

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.' );
} );
} );

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 );
} );
} );
} );