A customizable, open-source ecommerce platform built on WordPress. Build any commerce solution you can imagine.
Go to file
Manish Menaria 4ae60196ea
Product Collection: Make attributes available in rest_product_query hook (#44150)
* Make attributes available in rest_product_query hook

This commit introduces the 'includeInQueryContext' attribute to the 'woocommerce/product-collection' block and updates the 'woocommerce/product-template' block to consume this new attribute.

Key Changes:
1. `woocommerce/product-collection` Block:
   - A new attribute 'includeInQueryContext' is added in `block.json`. This attribute is designed to hold a list of attribute names relevant for the query context.
   - The 'includeInQueryContext' attribute is included in the `providesContext` field to ensure its availability to child blocks.
   - In `constants.ts`, default values for 'includeInQueryContext' are defined, specifying 'collection' and 'id' as initial attributes.
   - The `types.ts` file is updated with a comment explaining the purpose of 'includeInQueryContext'.

2. `woocommerce/product-template` Block:
   - Modified `block.json` to utilize the 'includeInQueryContext' context provided by the parent `woocommerce/product-collection` block.
   - The `edit.tsx` file is updated to handle the new context. It uses a newly added utility function `useProductCollectionBlockAttributes` from `utils.tsx` to access parent block attributes.
   - The `utils.tsx` file is introduced, containing the `useProductCollectionBlockAttributes` hook. This hook is responsible for finding the parent 'woocommerce/product-collection' block and returning its attributes.
   - Within `edit.tsx`, logic is added to create a query context object based on the attributes specified in 'includeInQueryContext', enhancing the block's ability to dynamically adapt to changes.

* Remove commented code

* Rename query context attribute and optimize parent block detection

This commit introduces two significant changes aimed at improving code readability and efficiency.

1. **Renaming of Query Context Attribute:**
   - The attribute `includeInQueryContext` has been renamed to `queryContextIncludes` across various files, including block JSON configurations and TypeScript definitions. This change makes the attribute's purpose more intuitive, indicating it specifies which attributes to include in the query context.

2. **Optimized Parent Block Detection:**
   - Replaced the use of `getBlockParents` with `getBlockParentsByBlockName` in utility functions to find the closest Product Collection block. This optimization allows for a more direct and efficient way to identify the relevant parent block by specifying the block name, reducing unnecessary iterations and improving code performance.

* Streamline query context construction in product template

Key Changes:
- **Introduction of `useProductCollectionQueryContext` Hook:** This new hook takes the `clientId` and `queryContextIncludes` as inputs and returns a query context object. It encapsulates the logic for fetching parent product collection block attributes and constructing the query context accordingly. This abstraction simplifies the edit component's logic, focusing on the essentials and improving code readability.
- **Optimization of Parent Block Detection:** The hook uses `getBlockParentsByBlockName` to accurately and efficiently find the closest parent `Product Collection` block, minimizing the overhead previously associated with traversing the block hierarchy.

* Always include `collection` and `id` in query context

* Minor refactor

* Enhance query context handling for more maintainable code

- Introduced `DEFAULT_QUERY_CONTEXT_ATTRIBUTES` in `edit.tsx` to maintain a clear list of default query context attributes.
- Modified `ProductTemplateEdit` to automatically include these default attributes in `queryContextIncludes`, ensuring they are always part of the query context without manual initialization.
- Simplified `useProductCollectionQueryContext` in `utils.tsx` by removing static initialization of 'collection' and 'id', relying instead on the dynamic addition of necessary attributes from `queryContextIncludes`.

This refactor enhances the maintainability and clarity of the code, ensuring a solid foundation for future enhancements and features.

* Add E2E tests for Product Collection query context

- Added a new test suite 'Query Context in Editor' to validate the correctness of query context parameters when the Product Collection block is used. This suite ensures that:
  - For the 'Product Catalog', only the ID is sent in the query context, confirming that collection-specific parameters are excluded when not relevant.
  - For collections such as 'On Sale', the collection name is correctly passed in the query context, validating that the block dynamically adjusts query parameters based on its settings.

- Introduced a new utility method `setupAndFetchQueryContextURL` in `product-collection.page.ts`. This method automates the setup of a post with the Product Collection block and fetches the URL with query context parameters, facilitating the validation of query context handling.

* Add changefile(s) from automation for the following project(s): woocommerce-blocks

* Fix edge case when `queryContextIncludes` is not defined

- Initializing `queryContextIncludes` with a default empty array directly in the destructuring assignment of the component's props. This approach ensures that `queryContextIncludes` is always an array, simplifying downstream logic.
- Creating a new constant `queryContextIncludesWithDefaults` to hold the combination of `queryContextIncludes` and `DEFAULT_QUERY_CONTEXT_ATTRIBUTES`. This step avoids directly mutating the `queryContextIncludes` prop, aligning with best practices for functional purity and making the code easier to understand and debug.
- Updating the `useProductCollectionQueryContext` hook call to use `queryContextIncludesWithDefaults`. This ensures that the default query context attributes are consistently included without altering the original prop.

These adjustments not only enhance the code's maintainability but also ensure more predictable behavior by avoiding side effects related to parameter mutation.

---------

Co-authored-by: github-actions <github-actions@github.com>
2024-02-12 14:15:24 +05:30
.github Skip e2e tests for not relevant changes (e.g. docs) (#44316) 2024-02-09 15:06:52 -04:00
.husky Remove the post-checkout hook it was causing too much friction. (#37024) 2023-03-03 12:43:18 +13:00
bin Migrate Puppeteer test action from Blocks repo (#42733) 2023-12-13 16:01:34 -08:00
changelog Create changelog/39948-patch-4 2023-09-27 10:01:56 +01:00
docs Update check-if-woo-is-active.md (#44306) 2024-02-05 20:08:30 +00:00
packages Refactor section and subsection blocks (#44502) 2024-02-09 15:47:02 -03:00
plugins Product Collection: Make attributes available in rest_product_query hook (#44150) 2024-02-12 14:15:24 +05:30
tools Package Release: Prepare PHP packages for release (#44420) 2024-02-08 22:23:13 +13:00
.codecov.yml Turn on code-cov for PR checking of unit test coverage (#36548) 2023-02-09 22:08:36 +13:00
.editorconfig JS formatting files 2021-10-19 10:35:46 +13:00
.eslintignore JS formatting files 2021-10-19 10:35:46 +13:00
.eslintrc.js Designate root level eslint config 2021-12-13 08:13:40 -08:00
.gitattributes Use `ci-jobs` Utility For `ci.yml` Matrix (#43532) 2024-01-12 20:32:14 -08:00
.gitignore Migrate merchant local-pickup e2e tests to Playwright (#43057) 2024-01-11 21:25:12 +07:00
.markdownlint.json Add reusable blocks documentation for remaining blocks (#40521) 2023-10-03 12:49:46 +00:00
.markdownlintignore undo changes to test fixtures 2023-11-10 15:49:08 +01:00
.npmrc Upgrade to pnpm 7 (#34661) 2022-09-16 09:21:42 +12:00
.nvmrc Update package and node version 2021-12-15 07:08:37 -08:00
.pnpmfile.cjs Optimized `wireit` Fingerprinting File Inputs (#42684) 2023-12-11 16:21:35 -08:00
.prettierrc.js JS formatting files 2021-10-19 10:35:46 +13:00
.stylelintrc Deprecate stylelint-config-wordpress with current package 2021-12-14 09:14:54 -08:00
.syncpackrc Update `wireit` & Optimize Builds (#43595) 2024-01-15 14:06:55 -08:00
CODEOWNERS Add codeowners file (#35765) 2022-11-29 13:18:45 -08:00
CODE_OF_CONDUCT.md appease the linter 2023-11-06 14:20:05 +01:00
DEVELOPMENT.md Removed Unnecessary `run` From `pnpm` 2023-12-09 14:36:22 -08:00
README.md Removed Unnecessary `run` From `pnpm` 2023-12-09 14:36:22 -08:00
SECURITY.md replace developer.woocommerce.com with developer.woo.com 2023-11-10 16:09:27 +01:00
changelog.txt Update changelog.txt from release 8.5.2 (#44095) 2024-01-25 20:22:24 +00:00
package.json tools/create-extension: Remove old tool (#44374) 2024-02-06 10:47:31 +13:00
phpcs.xml add exclusion for short array syntax 2023-12-14 10:07:41 -04:00
pnpm-lock.yaml Added `packages/php/remote-specs-validation` to Lock File (#44523) 2024-02-09 12:22:53 -08:00
pnpm-workspace.yaml Remote Specs Testing package: Add new package (#44419) 2024-02-08 12:46:26 +13:00
renovate.json Converted Spaces To Tabs In JSON 2022-05-10 13:31:01 -07:00
tsconfig.base.json Removed TypeScript Incremental Build Support (#37374) 2023-03-23 11:25:42 -07:00

README.md

WooCommerce Monorepo

WooCommerce

Welcome to the WooCommerce Monorepo on GitHub. Here you can find all of the plugins, packages, and tools used in the development of the core WooCommerce plugin as well as WooCommerce extensions. You can browse the source, look at open issues, contribute code, and keep tracking of ongoing development.

We recommend all developers to follow the WooCommerce development blog to stay up to date about everything happening in the project. You can also follow @DevelopWC on Twitter for the latest development updates.

Getting Started

To get up and running within the WooCommerce Monorepo, you will need to make sure that you have installed all of the prerequisites.

Prerequisites

  • NVM: While you can always install Node through other means, we recommend using NVM to ensure you're aligned with the version used by our development teams. Our repository contains an .nvmrc file which helps ensure you are using the correct version of Node.
  • PNPM: Our repository utilizes PNPM to manage project dependencies and run various scripts involved in building and testing projects.
  • PHP 7.4+: WooCommerce Core currently features a minimum PHP version of 7.4. It is also needed to run Composer and various project build scripts. See troubleshooting for troubleshooting problems installing PHP.
  • Composer: We use Composer to manage all of the dependencies for PHP packages and plugins.

Once you've installed all of the prerequisites, you can run the following commands to get everything working.

# Ensure that you're using the correct version of Node
nvm use
# Install the PHP and Composer dependencies for all of the plugins, packages, and tools
pnpm install
# Build all of the plugins, packages, and tools in the monorepo
pnpm build

At this point you are now ready to begin developing and testing. All of the build outputs are cached running pnpm build again will only build the plugins, packages, and tools that have changed since the last time you ran the command.

Check out our development guide if you would like a more comprehensive look at working in our repository.

Repository Structure

  • Plugins: Our repository contains plugins that relate to or otherwise aid in the development of WooCommerce.
    • WooCommerce Core: The core WooCommerce plugin is available in the plugins directory.
  • Packages: Contained within the packages directory are all of the PHP and JavaScript provided for the community. Some of these are internal dependencies and are marked with an internal- prefix.
  • Tools: We also have a growing number of tools within our repository. Many of these are intended to be utilities and scripts for use in the monorepo, but, this directory may also contain external tools.

Reporting Security Issues

To disclose a security issue to our team, please submit a report via HackerOne here.

Support

This repository is not suitable for support. Please don't use our issue tracker for support requests, but for core WooCommerce issues only. Support can take place through the appropriate channels:

NOTE: Unfortunately, we are unable to honor support requests in issues on this repository; as a result, any requests submitted in this manner will be closed.

Community

For peer to peer support, real-time announcements, and office hours, please join our slack community!

Contributing to WooCommerce

If you have a patch or have stumbled upon an issue with WooCommerce core, you can contribute this back to the code. Please read our contributor guidelines for more information on how you can do this.