Refactored Single Product Block Context Delivery#51390
Refactored Single Product Block Context Delivery#51390ObliviousHarmony wants to merge 9 commits intotrunkfrom
Conversation
By its nature, all context is passed from parent to child. This means there isn't a reason to manually set it, and instead, block should opt-in to using the context.
Rather than relying entirely on the `postId` context, some blocks use the global post to figure out what content to display. This updates the global post so that all inner blocks have the selected product set as the global post.
This should make sure that the client and server are consistent.
Test using WordPress PlaygroundThe changes in this pull request can be previewed and tested using a WordPress Playground instance. Test this pull request with WordPress Playground. Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit. |
|
Size Change: +682 B (+0.03%) Total Size: 2.37 MB
|
Since blocks can be nested we need to use a stack to change the global post. This makes sure each downstream block has the post set to the product that is currently being rendered by the block.
|
While this pull request is ready, unfortunately, it's blocked by an issue with the way that WordPress handles inner block context. This Trac ticket is about the issue. |
|
The referenced upstream work was merged and is part of WordPress 6.8. Once it is covered by the minimum WordPress version we support, this PR can be looked at again. |
Submission Review Guidelines:
Changes proposed in this Pull Request:
By its nature, any context that a block provides will be passed from parent to child. Previously we were setting the context on all inner blocks of a Single Product block. In cases where a block was being passed a
postIdby a parent, this filter would override that value. This manifested in the Product Collection block where the first product shown in the block was being overridden with the Single Product block's selection.After reviewing relevant code in Gutenberg I discovered that last year the Post Template block changed the way it passed context to inner blocks. Using this as inspiration:
postIdon products that may have their own plans for it. I also added the missingsingleProductcontext to the editor.Backward Compatibility Concerns
This does come with some caveats:
postIdandsingleProductcontext is not available unless a block explicitly declares it in theblock.jsonasusesContext.postIdwill now cascade from the Post Template block to all inner blocks.postIdpassed correctly through the block hierarchy.In my opinion these risks are worth accepting. The proposed behavior is better aligned with Gutenberg and ensures that inner blocks of both the Single Product and Product Collection blocks have the correct context and global post. We shouldn't encourage people to rely on secret runtime context that is automatically injected into every block.
Closes #51352.
How to test the changes in this Pull Request:
Using the WooCommerce Testing Instructions Guide, include your detailed testing instructions:
Changelog entry
Changelog Entry Details
Significance
Type
Message
Changelog Entry Comment
Comment