This repository was archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
Filter all products block by attribute #1127
Merged
Merged
Changes from 1 commit
Commits
Show all changes
69 commits
Select commit
Hold shift + click to select a range
47a36b2
Block setup
mikejolley dd1fe39
Working filtering and intersection of arrays
mikejolley 7f36fdf
Implement block settings and no attribute placeholder
mikejolley 63c3489
Correctly toggle counts
mikejolley 2a4bf48
Implement filtering
mikejolley 3f2e1df
Fix price slider constraints
mikejolley 378d861
Fix price slider constraints
mikejolley 8941166
Edit mode
mikejolley d1695c5
Rename ProductAttributeControl to ProductAttributeTermControl
mikejolley b92b99d
Attribute ID saving
mikejolley 7d90236
fix incorrect test fixtures
nerrad 98fae73
fix incorrect regex for parsing model (or resource names) from the ro…
nerrad 4aeaed1
Fix query classes for some endpoints
mikejolley dcb704c
Style improvements
mikejolley 7c26a00
Update inline comments
mikejolley 2792bac
use previous tests
mikejolley 9e3ef5d
Show attribute control in sidebar
mikejolley 56f8d95
Remove displayStyle option
mikejolley 62711cf
Sort attributes by name
mikejolley 505ce96
Merge branch 'master' into update/price-slider-constraints
mikejolley b02150c
Merge branch 'master' into experiment/filter-by-attribute
mikejolley b8a7780
Merge branch 'update/price-slider-constraints' into experiment/filter…
mikejolley e95e19c
Show more/less toggle
mikejolley d11aa29
Merge branch 'master' into experiment/filter-by-attribute
mikejolley 18562a0
Use renderFrontend
mikejolley c5dfef4
Only sort when adding values
mikejolley 9b955e8
Rename memo placeholder
mikejolley 21c244d
More specific CSS for pointer
mikejolley df96edb
Update assets/js/base/hooks/use-query-state.js
mikejolley 8aa6811
Remove always true taxonomy check
mikejolley 0109336
Update assets/js/blocks/attribute-filter/block.js
mikejolley 24bba5c
Merge branch 'experiment/filter-by-attribute' of https://github.com/w…
mikejolley 0af54f9
Remove lodash join
mikejolley e830631
native js for string casting
mikejolley a0dc217
Move internal deps
mikejolley 97561a6
hyphenate attributes
mikejolley 171fb41
Correct data set names
mikejolley 18cb5d8
Remove unwanted dependency
mikejolley 81b8abb
Moving imports
mikejolley 0ba0591
Missing deps
mikejolley ae21f20
replace yoda conditonal
mikejolley d9a691c
Missing deps
mikejolley 1a8e4af
Missing deps
mikejolley b1a24d6
Check value exists
mikejolley b4bcd39
Remove undefined filter
mikejolley 9c14ae7
renderedOptions usememo
mikejolley 2270310
Set defaults in checkbox list
mikejolley bc8dbfd
Show more/less refactor
mikejolley ca426f4
Use getAdminLink
mikejolley 4a02e64
Fix object length check
mikejolley cdb3f09
Correct AND query handling for counts
mikejolley 0867c94
Merge branch 'master' into experiment/filter-by-attribute
mikejolley 1d53d56
useQueryStateByContext
mikejolley e250eba
Add store rest endpoints
mikejolley fba12a2
Update assets/js/base/components/checkbox-list/index.js
mikejolley 7631fb8
Update assets/js/base/components/checkbox-list/index.js
mikejolley 4e1f250
Update assets/js/base/components/checkbox-list/index.js
mikejolley dc492a2
Update assets/js/blocks/attribute-filter/block.js
mikejolley 7785207
Feedback
mikejolley 53ba361
Merge branch 'master' into experiment/filter-by-attribute
mikejolley 8c22ba0
Merge conflict
mikejolley 129aebe
feedback
mikejolley 4a77711
API readme
mikejolley 0432ef7
Fix API relation queries for multiple attributes
mikejolley 607aeee
Prevent all options flashing visible during loads
mikejolley ab2351f
null check
mikejolley ded36b6
Improve loading state
mikejolley e87da8c
Remove null options change - it's no longer needed
mikejolley bbcebcb
update from master
mikejolley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add store rest endpoints
- Loading branch information
commit e250eba1eb95680d4961510fd85c8aa59f1d79af
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { find } from 'lodash'; | ||
| import { ATTRIBUTES } from '@woocommerce/block-settings'; | ||
|
|
||
| /** | ||
| * Get the ID of the first image attached to a product (the featured image). | ||
| * | ||
| * @param {number} attributeId The attribute ID. | ||
| * @return {string} The taxonomy name. | ||
| */ | ||
| export function getTaxonomyFromAttributeId( attributeId ) { | ||
| if ( ! attributeId ) { | ||
| return null; | ||
| } | ||
|
|
||
| const productAttribute = find( ATTRIBUTES, [ | ||
| 'attribute_id', | ||
| attributeId.toString(), | ||
| ] ); | ||
|
|
||
| return productAttribute.attribute_name | ||
| ? 'pa_' + productAttribute.attribute_name | ||
| : null; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
src/RestApi/StoreApi/Controllers/ProductAttributeTerms.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| <?php | ||
| /** | ||
| * Product Attribute Terms Controller. | ||
| * | ||
| * @internal This API is used internally by Blocks--it is still in flux and may be subject to revisions. | ||
| * @package WooCommerce/Blocks | ||
| */ | ||
|
|
||
| namespace Automattic\WooCommerce\Blocks\RestApi\StoreApi\Controllers; | ||
|
|
||
| defined( 'ABSPATH' ) || exit; | ||
|
|
||
| use \WP_Error as RestError; | ||
| use \WP_REST_Server as RestServer; | ||
| use \WP_REST_Controller as RestContoller; | ||
| use \WC_REST_Exception as RestException; | ||
| use Automattic\WooCommerce\Blocks\RestApi\StoreApi\Schemas\TermSchema; | ||
| use Automattic\WooCommerce\Blocks\RestApi\StoreApi\Utilities\TermQuery; | ||
|
|
||
| /** | ||
| * Product attribute terms API. | ||
| * | ||
| * @since $VID:$ | ||
| */ | ||
| class ProductAttributeTerms extends RestContoller { | ||
| /** | ||
| * Endpoint namespace. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $namespace = 'wc/store'; | ||
|
|
||
| /** | ||
| * Route base. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $rest_base = 'products/attributes/(?P<attribute_id>[\d]+)/terms'; | ||
|
|
||
| /** | ||
| * Schema class instance. | ||
| * | ||
| * @var TermSchema | ||
| */ | ||
| protected $schema; | ||
|
|
||
| /** | ||
| * Query class instance. | ||
| * | ||
| * @var TermQuery | ||
| */ | ||
| protected $term_query; | ||
|
|
||
| /** | ||
| * Setup API class. | ||
| */ | ||
| public function __construct() { | ||
| $this->schema = new TermSchema(); | ||
| $this->term_query = new TermQuery(); | ||
| } | ||
|
|
||
| /** | ||
| * Register the routes. | ||
| */ | ||
| public function register_routes() { | ||
| register_rest_route( | ||
| $this->namespace, | ||
| '/' . $this->rest_base, | ||
| array( | ||
| 'args' => array( | ||
| 'attribute_id' => array( | ||
| 'description' => __( 'Unique identifier for the attribute.', 'woo-gutenberg-products-block' ), | ||
| 'type' => 'integer', | ||
| ), | ||
| ), | ||
| array( | ||
| 'methods' => RestServer::READABLE, | ||
| 'callback' => [ $this, 'get_items' ], | ||
| 'args' => $this->get_collection_params(), | ||
| ), | ||
| 'schema' => array( $this, 'get_public_item_schema' ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Item schema. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function get_item_schema() { | ||
| return $this->schema->get_item_schema(); | ||
| } | ||
|
|
||
| /** | ||
| * Prepare a single item for response. | ||
| * | ||
| * @param \WP_Term $item Term object. | ||
| * @param \WP_REST_Request $request Request object. | ||
| * @return \WP_REST_Response $response Response data. | ||
| */ | ||
| public function prepare_item_for_response( $item, $request ) { | ||
| return rest_ensure_response( $this->schema->get_item_response( $item ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Get a collection of attribute terms. | ||
| * | ||
| * @param \WP_REST_Request $request Full details about the request. | ||
| * @return RestError|\WP_REST_Response | ||
| */ | ||
| public function get_items( $request ) { | ||
| $attribute = wc_get_attribute( $request['attribute_id'] ); | ||
|
|
||
| if ( ! $attribute || ! taxonomy_exists( $attribute->slug ) ) { | ||
| return new \WP_Error( 'woocommerce_rest_taxonomy_invalid', __( 'Attribute does not exist.', 'woo-gutenberg-products-block' ), array( 'status' => 404 ) ); | ||
| } | ||
|
|
||
| $request['taxonomy'] = $attribute->slug; | ||
| $objects = $this->term_query->get_objects( $request ); | ||
|
|
||
| foreach ( $objects as $object ) { | ||
| $data = $this->prepare_item_for_response( $object, $request ); | ||
| $return[] = $this->prepare_response_for_collection( $data ); | ||
| } | ||
|
|
||
| return rest_ensure_response( $return ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the query params for collections of attributes. | ||
| * | ||
| * @return array | ||
| */ | ||
| public function get_collection_params() { | ||
| $params = array(); | ||
| $params['context'] = $this->get_context_param(); | ||
| $params['context']['default'] = 'view'; | ||
|
|
||
| $params['order'] = array( | ||
| 'description' => __( 'Order sort attribute ascending or descending.', 'woo-gutenberg-products-block' ), | ||
| 'type' => 'string', | ||
| 'default' => 'asc', | ||
| 'enum' => array( 'asc', 'desc' ), | ||
| 'validate_callback' => 'rest_validate_request_arg', | ||
| ); | ||
|
|
||
| $params['orderby'] = array( | ||
| 'description' => __( 'Sort collection by object attribute.', 'woo-gutenberg-products-block' ), | ||
| 'type' => 'string', | ||
| 'default' => 'name', | ||
| 'enum' => array( | ||
| 'name', | ||
| 'slug', | ||
| 'count', | ||
| ), | ||
| 'validate_callback' => 'rest_validate_request_arg', | ||
| ); | ||
|
|
||
| return $params; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should you still do a check here and return a default queryState if
taxonomyis null? That will guard against any the lack of any taxonomy for the given attribute id coming into this component.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.
Without a taxonomy this query will be broken... I think the change to the useCollection hook would be good so we can avoid the query altogether if missing (since we cannot add a conditional before the hooks). Follow up?
Uh oh!
There was an error while loading. Please reload this page.
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.
👍 to follow up.