-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Add products data store #32964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add products data store #32964
Conversation
61a4430 to
d68a8da
Compare
|
📊 Test reports for this pull request have been published and are accessible through the following links:
Latest commit referenced in the reports: Visit the WooCommerce Test Reports homepage to view all published reports. See the FAQs page if you're having problems accessing them. |
joshuatf
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, @louwie17!
Overall this looks very solid. Left a few minor comments, but let me know if you have any questions.
| status: 'any' | 'draft' | 'pending' | 'private' | 'publish' | 'future'; | ||
| type: 'simple' | 'grouped' | 'external' | 'variable'; | ||
| sku: string; | ||
| featured: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this supposed to be a boolean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup it should be, thanks
| ExtensionList, | ||
| ProfileItemsState, | ||
| Product, | ||
| OnboardingProductType, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call on this rename!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, Product seemed like a bad name.
| @@ -0,0 +1,8 @@ | |||
| export enum TYPES { | |||
| SET_PRODUCT = 'SET_PRODUCT', | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all new data stores, I think we should be using the REQUEST|SUCCESS|ERROR patterns. For example - https://github.com/woocommerce/woocommerce/blob/trunk/packages/js/data/src/countries/action-types.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that makes sense, I debated on that, but didn't want to write all the extra actions haha
But it does seem like the best pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I end up just sticking with SUCCESS and ERROR, as we don't really need REQUEST given we have the resolutions for that, and can use isResolutionFinished.
|
|
||
| type ProductGetResponse = Product[] | ( { data: Product[] } & Response ); | ||
|
|
||
| function* request( query: Partial< ProductQuery > ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks very similar to the items data store request method. Any opportunities to dry up some of this logic or are they too unique?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They should be near identical aside from the types, but I can make use of a generic for that.
| shipping_class: string; | ||
| attribute: string; | ||
| attribute_term: string; | ||
| tax_class: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an enum, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure where I can find the enum values for this, the item schema has it listed as a string -> https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/includes/rest-api/Controllers/Version1/class-wc-rest-products-v1-controller.php#L1939-L1942
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The REST API states it should be standard, reduced-rate or zero-rate.
| on_sale: boolean; | ||
| min_price: string; | ||
| max_price: string; | ||
| stock_status: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also an enum I believe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I believe you are right, I think just instock or outofstock
| * @param {Object} query Query for product totals count. | ||
| * @return {string} Resource name for product totals. | ||
| */ | ||
| export function getTotalProductCountResourceName( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice method!
d68a8da to
9e316ab
Compare
|
@joshuatf I did add one extra step to the testing instructions for double checking the |
joshuatf
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @louwie17, looks really great. Sorry, I missed a couple small items my first pass. I left a couple comments, but let me know if you have any questions.
| return items; | ||
| } catch ( error ) { | ||
| yield getProductsError( query, error ); | ||
| return error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to throw error here so that users can catch on getProducts().
getProducts(...).catch(...).
| yield getProductsTotalCountSuccess( query, totalCount ); | ||
| return totalCount; | ||
| } catch ( error ) { | ||
| yield getProductsTotalCountError( query, error ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment about throwing here.
| }, | ||
| }, | ||
| productsCount: { | ||
| 'total-guyisms:{}': 2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lol, this was deff not me haha I copy pasted most of these tests and updated them, I somehow never noticed this.
| shipping_class: string; | ||
| attribute: string; | ||
| attribute_term: string; | ||
| tax_class: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The REST API states it should be standard, reduced-rate or zero-rate.
| date_created_gmt: string; | ||
| date_modified: string; | ||
| date_modified_gmt: string; | ||
| type: 'simple' | 'grouped' | 'external' | 'variable'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can also be extended by hooks/filters on the backend.
9e316ab to
c35f99d
Compare
|
@joshuatf thanks again for you review and suggestions, I addressed the PR feedback if you have time for a re-review? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested various parameters and fetching of totals and all worked as expected. Also double checked to categories for any regresions.
This is a fantastic addition to our data stores. Thank you for all your hard work on this, @louwie17! ![]()
…to handle _fields
c35f99d to
defc559
Compare
|
@joshuatf I just updated this branch with the fixes for the things we discussed in Slack. |
joshuatf
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing those up, @louwie17! Looks good and I'm able to retrieve individual fields without passing id now.
All Submissions:
Changes proposed in this Pull Request:
Adds a new products data store that makes use of the products API -> https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products
For now this only allows for retrieving/search for products and getting the product count.
Partially addresses #32524
How to test the changes in this Pull Request:
wp.data.select('wc/admin/products').getProducts({ status: 'publish'}, undefined);undefinedat first_fields: ['id']for example, which should only return theids, any query param defined here should work: https://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-productsgetProductsTotalCountfunction, by running:wp.data.select('wc/admin/products').getProductsTotalCount({ status: 'publish'}, undefined);, this might run the correct number right away if you recently rangetProductswith the same query params, otherwise it should returnundefinedintiially.wc/admin/productsstore and make sure the state is updated correctly and the actions are triggered correctly.wp.data.select('wc/admin/items').getItems('categories', { });in your console to see if theitemsstore still works correctly, andwp.data.select('wc/admin/items').getItemsTotalCount('categories', { });.Other information:
pnpm nx changelog <project>?FOR PR REVIEWER ONLY: