2024-08-05 15:51:32 +00:00
---
post_title: Registering custom collections in product collection block
menu_title: Registering custom collections
tags: how-to
---
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
# Register Product Collection
2024-09-04 17:11:35 +00:00
The `__experimentalRegisterProductCollection` function is part of the `@woocommerce/blocks-registry` package. This function allows third party developers to register a new collection. This function accepts most of the arguments that are accepted by [Block Variation ](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-variations/#defining-a-block-variation ).
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
> [!WARNING]
> It's experimental and may change in the future. Please use it with caution.
**There are two ways to use this function:**
2024-09-04 17:11:35 +00:00
1. Using `@woocommerce/dependency-extraction-webpack-plugin` in a Webpack configuration: This will allow you to import the function from the package & use it in your code. For example:
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
```tsx
import { __experimentalRegisterProductCollection } from "@woocommerce/blocks-registry";
```
2024-09-04 17:11:35 +00:00
2. Using the global `wc` object: This will allow you to use the function using the global JS object without importing it. For example:
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
```tsx
wc.wcBlocksRegistry.__experimentalRegisterProductCollection({
...
});
```
> [!TIP]
> The first method is recommended if you are using Webpack.
## Defining a Collection
We will explain important arguments that can be passed to `__experimentalRegisterProductCollection` . For other arguments, you can refer to the [Block Variation ](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-variations/#defining-a-block-variation ) documentation.
A Collection is defined by an object that can contain the following fields:
2024-09-12 13:51:32 +00:00
- `name` (type `string` ): A unique and machine-readable collection name. We recommend using the format `<plugin-name>/product-collection/<collection-name>` . Both `<plugin-name>` and `<collection-name>` should consist only of alphanumeric characters and hyphens (e.g., `my-plugin/product-collection/my-collection` ).
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
- `title` (type `string` ): The title of the collection, which will be displayed in various places including the block inserter and collection chooser.
- `description` (optional, type `string` ): A human-readable description of the collection.
- `innerBlocks` (optional, type `Array[]` ): An array of inner blocks that will be added to the collection. If not provided, the default inner blocks will be used.
2024-08-05 15:51:32 +00:00
- `isDefault` : It's set to `false` for all collections. Third party developers don't need to pass this argument.
- `isActive` : It will be managed by us. Third party developers don't need to pass this argument.
2024-08-01 10:03:27 +00:00
- `usesReference` (optional, type `Array[]` ): An array of strings specifying the required reference for the collection. Acceptable values are `product` , `archive` , `cart` , and `order` . When the required reference isn't available on Editor side but will be available in Frontend, we will show a preview label.
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
### Attributes
Attributes are the properties that define the behavior of the collection. All the attributes are *optional* . Here are some of the important attributes that can be passed to `__experimentalRegisterProductCollection` :
- `query` (type `object` ): The query object that defines the query for the collection. It can contain the following fields:
- `offset` (type `number` ): The number of items to offset the query by.
- `order` (type `string` ): The order of the query. Accepted values are `asc` and `desc` .
- `orderBy` (type `string` ): The field to order the query by.
- `pages` (type `number` ): The number of pages to query.
- `perPage` (type `number` ): The number of products per page.
- `search` (type `string` ): The search term to query by.
- `taxQuery` (type `object` ): The tax query to filter the query by. For example, if you wanna fetch products with category `Clothing` & `Accessories` and tag `Summer` then you can pass `taxQuery` as `{"product_cat":[20,17],"product_tag":[36]}` . Where array values are the term IDs.
- `featured` (type `boolean` ): Whether to query for featured items.
- `timeFrame` (type `object` ): The time frame to query by.
- `operator` (type `string` ): The operator to use for the time frame query. Accepted values are `in` and `not-in` .
- `value` (type `string` ): The value to query by. It should be a valid date string that PHP's `strtotime` function can parse.
- `woocommerceOnSale` (type `boolean` ): Whether to query for items on sale.
- `woocommerceStockStatus` (type `array` ): The stock status to query by. Some of the accepted values are `instock` , `outofstock` , `onbackorder` .
- `woocommerceAttributes` (type `array` ): The attributes to query by.
- For example, if you wanna fetch products with color `blue` & `gray` and size `Large` then you can pass `woocommerceAttributes` as `[{"termId":23,"taxonomy":"pa_color"},{"termId":26,"taxonomy":"pa_size"},{"termId":29,"taxonomy":"pa_color"}]` .
- `woocommerceHandPickedProducts` (type `array` ): The hand-picked products to query by. It should contain the product IDs.
- `priceRange` (type `object` ): The price range to query by.
- `min` (type `number` ): The minimum price.
- `max` (type `number` ): The maximum price.
- `displayLayout` (type `object` ): The display layout object that defines the layout of the collection. It can contain the following fields:
- `type` (type `string` ): The type of layout. Accepted values are `grid` and `stack` .
- `columns` (type `number` ): The number of columns to display.
- `shrinkColumns` (type `boolean` ): Whether the layout should be responsive.
2024-07-23 07:03:27 +00:00
- `hideControls` (type `array` ): The controls to hide. Possible values:
- `order` - "Order by" setting
- `attributes` - "Product Attributes" filter
- `created` - "Created" filter
- `featured` - "Featured" filter
- `hand-picked` - "Hand-picked Products" filter
- `keyword` - "Keyword" filter
- `on-sale` - "On Sale" filter
- `stock-status` - "Stock Status" filter
- `taxonomy` - "Product Categories", "Product Tags" and custom taxonomies filters
- `price-range` - "Price Range" filter
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
#### Preview Attribute
The `preview` attribute is optional, and it is used to set the preview state of the collection. It can contain the following fields:
- `initialPreviewState` (type `object` ): The initial preview state of the collection. It can contain the following fields:
- `isPreview` (type `boolean` ): Whether the collection is in preview mode.
- `previewMessage` (type `string` ): The message to be displayed in the Tooltip when the user hovers over the preview label.
- `setPreviewState` (optional, type `function` ): The function to set the preview state of the collection. It receives the following arguments:
- `setState` (type `function` ): The function to set the preview state. You can pass a new preview state to this function containing `isPreview` and `previewMessage` .
- `attributes` (type `object` ): The current attributes of the collection.
- `location` (type `object` ): The location of the collection. Accepted values are `product` , `archive` , `cart` , `order` , `site` .
2024-08-05 15:51:32 +00:00
For more info, you may check [PR #46369 ](https://github.com/woocommerce/woocommerce/pull/46369 ), in which the Preview feature was added
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
## Examples
### Example 1: Registering a new collection
```tsx
__experimentalRegisterProductCollection({
name: "your-plugin-name/product-collection/my-custom-collection",
title: "My Custom Collection",
icon: "games",
description: "This is a custom collection.",
keywords: ["custom collection", "product collection"],
});
```
2024-09-04 17:11:35 +00:00
As you can see in the example above, we are registering a new collection with the name `woocommerce/product-collection/my-custom-collection` & title `My Custom Collection` . Here is screenshot of how it will look like:
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
![image ](https://github.com/woocommerce/woocommerce/assets/16707866/7fddbc02-a6cd-494e-b2f4-13dd5ef9cf96 )
### Example 2: Register a new collection with a preview
As you can see below, setting the initial preview state is possible. In the example below, we are setting `isPreview` and `previewMessage` .
```tsx
__experimentalRegisterProductCollection({
name: "your-plugin-name/product-collection/my-custom-collection-with-preview",
title: "My Custom Collection with Preview",
icon: "games",
description: "This is a custom collection with preview.",
keywords: ["My Custom Collection with Preview", "product collection"],
preview: {
initialPreviewState: {
isPreview: true,
previewMessage:
"This is a preview message for my custom collection with preview.",
},
},
attributes: {
query: {
perPage: 5,
featured: true,
},
displayLayout: {
type: "grid",
columns: 3,
shrinkColumns: true,
},
2024-07-23 07:03:27 +00:00
hideControls: [ "created", "stock-status" ]
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
},
});
```
Here is how it will look like:
![image ](https://github.com/woocommerce/woocommerce/assets/16707866/5fc1aa20-552a-4e09-b811-08babab46665 )
### Example 3: Advanced usage of preview
As you can see below, it's also possible to use `setPreviewState` to set the preview state. In the example below, we are setting `initialPreviewState` and using `setPreviewState` to change the preview state after 5 seconds.
**This example shows:**
- How to access current attributes and location in the preview state
- How to use async operations
- We are using `setTimeout` to change the preview state after 5 seconds. You can use any async operation, like fetching data from an API, to decide the preview state.
- How to use the cleanup function as a return value
- We are returning a cleanup function that will clear the timeout after the component is unmounted. You can use this to clean up any resources you have used in `setPreviewState` .
```tsx
__experimentalRegisterProductCollection({
name: "your-plugin-name/product-collection/my-custom-collection-with-advanced-preview",
title: "My Custom Collection with Advanced Preview",
icon: "games",
description: "This is a custom collection with advanced preview.",
keywords: [
"My Custom Collection with Advanced Preview",
"product collection",
],
preview: {
setPreviewState: ({
setState,
attributes: currentAttributes,
location,
}) => {
// setPreviewState has access to the current attributes and location.
// console.log( currentAttributes, location );
const timeoutID = setTimeout(() => {
setState({
isPreview: false,
previewMessage: "",
});
}, 5000);
return () => clearTimeout(timeoutID);
},
initialPreviewState: {
isPreview: true,
previewMessage:
"This is a preview message for my custom collection with advanced preview.",
},
},
});
```
### Example 4: Collection with inner blocks
As you can see below, it's also possible to define inner blocks for the collection. In the example below, we are defining inner blocks for the collection.
```tsx
__experimentalRegisterProductCollection({
name: "your-plugin-name/product-collection/my-custom-collection-with-inner-blocks",
title: "My Custom Collection with Inner Blocks",
icon: "games",
description: "This is a custom collection with inner blocks.",
keywords: ["My Custom Collection with Inner Blocks", "product collection"],
innerBlocks: [
[
"core/heading",
{
textAlign: "center",
level: 2,
content: "Title of the collection",
},
],
[
"woocommerce/product-template",
{},
[
["woocommerce/product-image"],
[
"woocommerce/product-price",
{
textAlign: "center",
fontSize: "small",
},
],
],
],
],
});
```
This will create a collection with a heading, product image, and product price. Here is how it will look like:
![image ](https://github.com/woocommerce/woocommerce/assets/16707866/3d92c084-91e9-4872-a898-080b4b93afca )
2024-07-23 08:28:42 +00:00
> [!TIP]
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
> You can learn more about inner blocks template in the [Inner Blocks](https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/nested-blocks-inner-blocks/#template) documentation.
2024-08-01 10:03:27 +00:00
### Example 5: Collection with `usesReference` argument
When a collection requires a reference to work properly, you can specify it using the `usesReference` argument. In the example below, we are defining a collection that requires a `product` reference.
```tsx
__experimentalRegisterProductCollection({
name: "your-plugin-name/product-collection/my-custom-collection",
title: "My Custom Collection",
icon: "games",
description: "This is a custom collection.",
keywords: ["custom collection", "product collection"],
usesReference: ["product"],
});
```
This will create a collection that requires a `product` reference. If the required reference isn't available on the Editor side but will be available in the Frontend, we will show a preview label.
When a collection need one of the multiple references, you can specify it using the `usesReference` argument. In the example below, we are defining a collection that requires either a `product` or `order` reference.
```tsx
__experimentalRegisterProductCollection({
name: "your-plugin-name/product-collection/my-custom-collection",
title: "My Custom Collection",
icon: "games",
description: "This is a custom collection.",
keywords: ["custom collection", "product collection"],
usesReference: ["product", "order"],
});
```
---
2024-07-23 08:28:42 +00:00
> [!TIP]
Expose `__experimentalRegisterProductCollection` in @woocommerce/blocks-registry Package (#48141)
* Expose registerProductCollection in @woocommerce/blocks-registry Package
This commit exposes the `registerProductCollection` function as part of the `@woocommerce/blocks-registry` package. This enhancement facilitates the registration of new product collections by 3PDs, promoting better modularity and extensibility within the WooCommerce Blocks ecosystem.
Changes include:
- Migration of `register-product-collection.tsx` to `packages/checkout/blocks-registry`.
- Export `registerProductCollection` from `@woocommerce/blocks-registry/index.ts`.
- Updated related imports and references to the new path.
This update enables 3PDs to register product collections more seamlessly, enhancing the extensibility of Product Collection block.
* Replace @woocommerce/blocks-checkout with @woocommerce/blocks-registry
* Add __experimental prefix
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Improve registerproductcollection for 3pds
* Set isDefault value to false
* Don't export all the types
* Update changelog
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Add plugin to test __experimentalRegisterProductCollection
* Add E2E tests
* Fix Lint errors
* Improve E2E tests for __experimentalRegisterProductCollection
- Reduced preview timeout from 2000ms to 1000ms.
- Expanded E2E tests to cover new attributes and preview functionalities.
* Refactor code to improve readability and maintainability
- Added a warning comment to indicate that `__experimentalRegisterProductCollection` is an experimental API.
- Refactored variable names and imports in `register-product-collection.tsx` and `index.tsx` for clarity.
- Simplified and reorganized type definitions and imports in `types.ts` and `utils.tsx`.
- Renamed function in `register-product-collection-tester.php` for consistency.
* E2E: Also test the Frontend
* Use alias for import statement
* Don't pass isActive to registerProductCollection
Now it's handle by registerProductCollection itself.
* Update registerproductcollection API structure
Refactored the product collection block to enhance attribute management and ensure consistency in query defaults. This change includes:
- Importing `DEFAULT_QUERY` from constants and using it to set default query attributes.
- Removing `DEFAULT_ATTRIBUTES` from specific collections and directly defining required attributes.
- Ensuring `postType` and `isProductCollectionBlock` are set to default values in the query object.
- Setting `inherit` attribute to `false` by default in all collections.
* Hide inherit control in collections
Ensure the "inherit" control is always hidden, as collections should not be able to change this attribute. This includes:
- Adding `CoreFilterNames.INHERIT` to the `hideControls` set in `register-product-collection.tsx`.
- Adjusting the `hideControls` attribute in individual collection files to remove redundant hiding of the `INHERIT` control.
* Fix: Filters not showing in inspector controls
* Set inherit to false for all collections
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Product Collection: Add validation for `__experimentalRegisterProductCollection` arguments (#48513)
* Add validation for `__experimentalRegisterProductCollection` arguments
Introduced comprehensive validation for the `ProductCollectionConfig` object in `__experimentalRegisterProductCollection` to ensure correct data types and values, enhancing error handling and robustness.
- Added a new function `isValidProductCollectionConfig` to perform various checks on the `ProductCollectionConfig` object.
- Validates properties such as `name`, `title`, `description`, `category`, `keywords`, `icon`, `isDefault`, `innerBlocks`, `example`, `scope`, `isActive`, `attributes`, and `preview`.
- Ensures correct data types and provides detailed console error messages for invalid configurations.
- Updated `__experimentalRegisterProductCollection` to use the validation function before proceeding with the registration process.
**Impact**
- Improves stability and prevents invalid configurations from causing runtime errors.
- Provides clearer error messages for developers, aiding in quicker debugging and development.
* Fix typo
* Refactor: Replace console.error with console.warn
Updated the error logging in the isValidProductCollectionConfig function to use console.warn instead of console.error for invalid configuration properties. This address the feedback from the PR review.
- Replaced console.error with console.warn for various validation checks in isValidProductCollectionConfig.
- Removed redundant return statements after console.warn calls.
- Improved logging messages to better inform about invalid configuration properties without treating them as critical errors.
- Simplified the logic in __experimentalRegisterProductCollection by combining query and attribute properties and ensuring defaults are set properly.
* Refactor: Rename isValidProductCollectionConfig to isValidCollectionConfig
Updated the function name from isValidProductCollectionConfig to isValidCollectionConfig for better clarity and consistency. Also, renamed related variables for improved readability.
* Add validation for name property
* Add changefile(s) from automation for the following project(s): woocommerce-blocks
* Title is required for new collection
* Update comments
* Fix E2E tests
* Address PR feedback
---------
Co-authored-by: github-actions <github-actions@github.com>
* Add README file for __experimentalRegisterProductCollection
* Add screenshots in README file
* Try to fix lint issue
* Docs: add example for collection with inner blocks
Enhanced the documentation for `__experimentalRegisterProductCollection` to include an example demonstrating how to define a collection with inner blocks. This example shows how to create a custom collection with nested blocks, including a heading and product elements, providing a clear guide for developers.
New content added:
- Example 4: Collection with inner blocks
- Sample code for defining a collection with inner blocks
- Tips and links to further resources on inner blocks and core collection definitions
* Fix Lint errors
* Address PR feedback
* Reduce number of JS files on /shop page
**Problem:** There was increase in number of JS files on /shop page after exposing `registerProductCollection` function in `@woocommerce/blocks-registry` package. This package is loaded on the frontend. For example, previously 45 JS files were loaded on /shop page but now 55 JS files are loaded on /shop page.
**Solution:**
1. After a bit of debugging I found out that constant file which we are importing i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts` contain some heavy dependencies & it's not pure. Therefore, I decided to split this file into two files. I moved all the constants that are used in `registerProductCollection` function to a new file i.e. `plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants-register-product-collection.ts`. This way, we don't need to load all the constants on the frontend i.e. /shop page.
- This reduced 4 JS files i.e. 51 JS files are loaded on /shop page.
2. After some more investigation, I found out that importing `registerBlockVariation` function is increasing number of JS files on Frontend. Therefore, I decided to use global `wp` object to call `registerBlockVariation` function. This way, we don't need to import it. This reduced last 6 files i.e. 45 JS files are loaded on /shop page.
This way, I was able to reduce number of JS files on /shop page from 55 to 45, which is same as before this PR.
* Refactor: product collection constants
- Moved constants from `constants-register-product-collection.ts` to `constants.ts`
- Deleted `constants-register-product-collection.ts`
- Updated import paths in relevant files to reflect the changes
- Moved utility functions to `utils.ts`
---------
Co-authored-by: github-actions <github-actions@github.com>
2024-07-05 11:25:35 +00:00
> You can also take a look at how we are defining our core collections at `plugins/woocommerce-blocks/assets/js/blocks/product-collection/collections` directory. Our core collections will also evolve over time.