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

Commit 036b1bf

Browse files
authored
Merge branch 'trunk' into remove-proptypes-reviews-by-product
2 parents 4b43aa5 + ef84602 commit 036b1bf

File tree

21 files changed

+633
-77
lines changed

21 files changed

+633
-77
lines changed

assets/js/atomic/blocks/product-elements/add-to-cart-form/block.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
"version": "1.0.0",
44
"title": "Add to Cart with Options",
55
"description": "Display a button so the customer can add a product to their cart. Options will also be displayed depending on product type. e.g. quantity, variation.",
6+
"attributes": {
7+
"isDescendentOfSingleProductBlock": {
8+
"type": "boolean",
9+
"default": false
10+
}
11+
},
612
"category": "woocommerce",
713
"keywords": [ "WooCommerce" ],
814
"usesContext": ["postId"],

assets/js/atomic/blocks/product-elements/add-to-cart-form/edit.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
/**
22
* External dependencies
33
*/
4+
import { useEffect } from '@wordpress/element';
45
import { useBlockProps } from '@wordpress/block-editor';
56
import { __ } from '@wordpress/i18n';
67
import { Button, Disabled, Tooltip } from '@wordpress/components';
78
import { Skeleton } from '@woocommerce/base-components/skeleton';
9+
import { BlockEditProps } from '@wordpress/blocks';
810

911
/**
1012
* Internal dependencies
1113
*/
1214
import './editor.scss';
15+
import { useIsDescendentOfSingleProductBlock } from '../shared/use-is-descendent-of-single-product-block';
1316
export interface Attributes {
1417
className?: string;
18+
isDescendentOfSingleProductBlock: boolean;
1519
}
1620

17-
const Edit = () => {
21+
const Edit = ( props: BlockEditProps< Attributes > ) => {
22+
const { setAttributes } = props;
1823
const blockProps = useBlockProps( {
1924
className: 'wc-block-add-to-cart-form',
2025
} );
26+
const { isDescendentOfSingleProductBlock } =
27+
useIsDescendentOfSingleProductBlock( {
28+
blockClientId: blockProps?.id,
29+
} );
30+
31+
useEffect( () => {
32+
setAttributes( {
33+
isDescendentOfSingleProductBlock,
34+
} );
35+
}, [ setAttributes, isDescendentOfSingleProductBlock ] );
2136

2237
return (
2338
<div { ...blockProps }>

assets/js/blocks/product-collection/constants.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,28 @@ export const getDefaultStockStatuses = () => {
3131
: Object.keys( STOCK_STATUS_OPTIONS );
3232
};
3333

34+
export const DEFAULT_QUERY: ProductCollectionQuery = {
35+
perPage: 9,
36+
pages: 0,
37+
offset: 0,
38+
postType: 'product',
39+
order: 'asc',
40+
orderBy: 'title',
41+
author: '',
42+
search: '',
43+
exclude: [],
44+
sticky: '',
45+
inherit: false,
46+
taxQuery: {},
47+
parents: [],
48+
isProductCollectionBlock: true,
49+
woocommerceOnSale: false,
50+
woocommerceStockStatus: getDefaultStockStatuses(),
51+
woocommerceAttributes: [],
52+
};
53+
3454
export const DEFAULT_ATTRIBUTES: Partial< ProductCollectionAttributes > = {
35-
query: {
36-
perPage: 9,
37-
pages: 0,
38-
offset: 0,
39-
postType: 'product',
40-
order: 'asc',
41-
orderBy: 'title',
42-
author: '',
43-
search: '',
44-
exclude: [],
45-
sticky: '',
46-
inherit: false,
47-
taxQuery: '',
48-
parents: [],
49-
isProductCollectionBlock: true,
50-
woocommerceOnSale: false,
51-
woocommerceStockStatus: getDefaultStockStatuses(),
52-
woocommerceAttributes: [],
53-
},
55+
query: DEFAULT_QUERY,
5456
tagName: 'div',
5557
displayLayout: {
5658
type: 'flex',
@@ -65,16 +67,14 @@ export const getDefaultSettings = (
6567
DEFAULT_ATTRIBUTES.displayLayout as ProductCollectionDisplayLayout,
6668
query: {
6769
...currentAttributes.query,
68-
orderBy: ( DEFAULT_ATTRIBUTES.query as ProductCollectionQuery )
69-
.orderBy as TProductCollectionOrderBy,
70-
order: ( DEFAULT_ATTRIBUTES.query as ProductCollectionQuery )
71-
.order as TProductCollectionOrder,
70+
orderBy: DEFAULT_QUERY.orderBy as TProductCollectionOrderBy,
71+
order: DEFAULT_QUERY.order as TProductCollectionOrder,
7272
},
7373
} );
7474

7575
export const DEFAULT_FILTERS = {
76-
woocommerceOnSale: ( DEFAULT_ATTRIBUTES.query as ProductCollectionQuery )
77-
.woocommerceOnSale,
76+
woocommerceOnSale: DEFAULT_QUERY.woocommerceOnSale,
7877
woocommerceStockStatus: getDefaultStockStatuses(),
7978
woocommerceAttributes: [],
79+
taxQuery: DEFAULT_QUERY.taxQuery,
8080
};

assets/js/blocks/product-collection/edit.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { ProductCollectionAttributes } from './types';
1414
import { VARIATION_NAME as PRODUCT_TITLE_ID } from './variations/elements/product-title';
1515
import InspectorControls from './inspector-controls';
1616
import { DEFAULT_ATTRIBUTES } from './constants';
17+
import './editor.scss';
1718

1819
export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [
1920
[
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.wc-block-editor-product-collection-inspector-toolspanel__filters {
2+
.wc-block-editor-product-collection-inspector__taxonomy-control:not(:last-child) {
3+
margin-bottom: $grid-unit-30;
4+
}
5+
}

assets/js/blocks/product-collection/inspector-controls/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import { ProductCollectionAttributes } from '../types';
1919
import ColumnsControl from './columns-control';
2020
import OrderByControl from './order-by-control';
2121
import OnSaleControl from './on-sale-control';
22-
import { setQueryAttribute } from './utils';
22+
import { setQueryAttribute } from '../utils';
2323
import { DEFAULT_FILTERS, getDefaultSettings } from '../constants';
2424
import StockStatusControl from './stock-status-control';
2525
import KeywordControl from './keyword-control';
2626
import AttributesControl from './attributes-control';
27+
import TaxonomyControls from './taxonomy-controls';
2728

2829
const ProductCollectionInspectorControls = (
2930
props: BlockEditProps< ProductCollectionAttributes >
@@ -54,6 +55,7 @@ const ProductCollectionInspectorControls = (
5455
setQueryAttribute( props, DEFAULT_FILTERS );
5556
resetAllFilters.forEach( ( resetFilter ) => resetFilter() );
5657
} }
58+
className="wc-block-editor-product-collection-inspector-toolspanel__filters"
5759
>
5860
<OnSaleControl { ...props } />
5961
<StockStatusControl { ...props } />
@@ -66,6 +68,10 @@ const ProductCollectionInspectorControls = (
6668
}
6769
setQueryAttribute={ setQueryAttributeBind }
6870
/>
71+
<TaxonomyControls
72+
setQueryAttribute={ setQueryAttributeBind }
73+
query={ props.attributes.query }
74+
/>
6975
</ToolsPanel>
7076
</InspectorControls>
7177
);

assets/js/blocks/product-collection/inspector-controls/keyword-control.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
* Internal dependencies
1717
*/
1818
import { ProductCollectionAttributes } from '../types';
19-
import { setQueryAttribute } from './utils';
19+
import { setQueryAttribute } from '../utils';
2020

2121
const KeywordControl = (
2222
props: BlockEditProps< ProductCollectionAttributes >

assets/js/blocks/product-collection/inspector-controls/on-sale-control.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
* Internal dependencies
1515
*/
1616
import { ProductCollectionAttributes } from '../types';
17-
import { setQueryAttribute } from './utils';
17+
import { setQueryAttribute } from '../utils';
1818

1919
const OnSaleControl = (
2020
props: BlockEditProps< ProductCollectionAttributes >

assets/js/blocks/product-collection/inspector-controls/stock-status-control.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
* Internal dependencies
1616
*/
1717
import { ProductCollectionAttributes } from '../types';
18-
import { setQueryAttribute } from './utils';
18+
import { setQueryAttribute } from '../utils';
1919
import { STOCK_STATUS_OPTIONS, getDefaultStockStatuses } from '../constants';
2020

2121
/**
@@ -76,6 +76,7 @@ const StockStatusControl = (
7676
) || []
7777
}
7878
__experimentalExpandOnFocus={ true }
79+
__experimentalShowHowTo={ false }
7980
/>
8081
</ToolsPanelItem>
8182
);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { Taxonomy } from '@wordpress/core-data/src/entity-types';
5+
import { __ } from '@wordpress/i18n';
6+
import { useSelect } from '@wordpress/data';
7+
import { store as coreStore } from '@wordpress/core-data';
8+
import {
9+
// @ts-expect-error Using experimental features
10+
// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
11+
__experimentalToolsPanelItem as ToolsPanelItem,
12+
} from '@wordpress/components';
13+
14+
/**
15+
* Internal dependencies
16+
*/
17+
import TaxonomyItem from './taxonomy-item';
18+
import { ProductCollectionQuery } from '../../types';
19+
20+
interface TaxonomyControlProps {
21+
query: ProductCollectionQuery;
22+
setQueryAttribute: ( value: Partial< ProductCollectionQuery > ) => void;
23+
}
24+
25+
/**
26+
* Hook that returns the taxonomies associated with product post type.
27+
*/
28+
export const useTaxonomies = (): Taxonomy[] => {
29+
const taxonomies = useSelect( ( select ) => {
30+
const { getTaxonomies } = select( coreStore );
31+
const filteredTaxonomies: Taxonomy[] = getTaxonomies( {
32+
type: 'product',
33+
per_page: -1,
34+
context: 'view',
35+
} );
36+
return filteredTaxonomies;
37+
}, [] );
38+
return taxonomies;
39+
};
40+
41+
function TaxonomyControls( {
42+
setQueryAttribute,
43+
query,
44+
}: TaxonomyControlProps ) {
45+
const { taxQuery } = query;
46+
47+
const taxonomies = useTaxonomies();
48+
if ( ! taxonomies || taxonomies.length === 0 ) {
49+
return null;
50+
}
51+
52+
return (
53+
<ToolsPanelItem
54+
label={ __( 'Taxonomies', 'woo-gutenberg-products-block' ) }
55+
hasValue={ () =>
56+
Object.values( taxQuery || {} ).some(
57+
( terms ) => !! terms.length
58+
)
59+
}
60+
onDeselect={ () => setQueryAttribute( { taxQuery: {} } ) }
61+
>
62+
{ taxonomies.map( ( taxonomy: Taxonomy ) => {
63+
const termIds = taxQuery?.[ taxonomy.slug ] || [];
64+
const handleChange = ( newTermIds: number[] ) =>
65+
setQueryAttribute( {
66+
taxQuery: {
67+
...taxQuery,
68+
[ taxonomy.slug ]: newTermIds,
69+
},
70+
} );
71+
72+
return (
73+
<TaxonomyItem
74+
key={ taxonomy.slug }
75+
taxonomy={ taxonomy }
76+
termIds={ termIds }
77+
onChange={ handleChange }
78+
/>
79+
);
80+
} ) }
81+
</ToolsPanelItem>
82+
);
83+
}
84+
85+
export default TaxonomyControls;

0 commit comments

Comments
 (0)