woocommerce/plugins/woocommerce-blocks/assets/js/utils/get-query.js

91 lines
2.1 KiB
JavaScript
Raw Normal View History

/**
* External dependencies
*/
import { min } from 'lodash';
import { DEFAULT_COLUMNS, DEFAULT_ROWS } from '../constants';
export default function getQuery( blockAttributes, name ) {
const {
attributes,
attrOperator,
categories,
catOperator,
Introduce a new Products by Tag(s) block (https://github.com/woocommerce/woocommerce-blocks/pull/554) * Introduced WGPB_Extend_Core class to modify shortcodes and API requests of core * Require the new class * WC_REST_Blocks_Products_Controller_V2 to override the wc-blocks API to support new tags properties * Register new products by tag block type * Modify utils to support tags and tag_operators * Add ProductTagControl to handle tag searching * Add the actual products by tag block * Set limitTags to 100 * Create Package class and use in main plugin file * Move and refactor library class - split asset methods into new Assets class. * Add jetpack autoloader dependency * fix tests * Update from master * AbstractBlock class for general block registration * remove test autoloader so tests do not break * Create AbstractProductGrid * FeaturedProduct * HandpickedProducts * ProductBestSellers * ProductCategory * ProductNew * ProductOnSale * ProductTopRated * ProductsByAttribute * Remove old base and render functions * Allow non-dynamic blocks and register category block * Fix products-by-attribute due to wrong naming * Remove no dev * test phpunit dir * Update testing framework * Update with new abstract classes and build in API * Undo edit to attribute block * Move edit mode * No need to support shortcodes * correct linting errors * Update tests/bootstrap.php Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Update code comment to make more sense. * Correct test package * docblock * Fix cancel button class * Fix classname schema * Set loading state so spinner is shown * Add placeholder element when no tags are selected * No tags placeholder * Update rest endpoints
2019-07-09 13:42:22 +00:00
tags,
tagOperator,
orderby,
products,
} = blockAttributes;
const columns = blockAttributes.columns || DEFAULT_COLUMNS;
const rows = blockAttributes.rows || DEFAULT_ROWS;
const apiMax = Math.floor( 100 / columns ) * columns; // Prevent uneven final row.
const query = {
status: 'publish',
per_page: min( [ rows * columns, apiMax ] ),
catalog_visibility: 'visible',
};
if ( categories && categories.length ) {
query.category = categories.join( ',' );
if ( catOperator && 'all' === catOperator ) {
query.category_operator = 'and';
}
}
Introduce a new Products by Tag(s) block (https://github.com/woocommerce/woocommerce-blocks/pull/554) * Introduced WGPB_Extend_Core class to modify shortcodes and API requests of core * Require the new class * WC_REST_Blocks_Products_Controller_V2 to override the wc-blocks API to support new tags properties * Register new products by tag block type * Modify utils to support tags and tag_operators * Add ProductTagControl to handle tag searching * Add the actual products by tag block * Set limitTags to 100 * Create Package class and use in main plugin file * Move and refactor library class - split asset methods into new Assets class. * Add jetpack autoloader dependency * fix tests * Update from master * AbstractBlock class for general block registration * remove test autoloader so tests do not break * Create AbstractProductGrid * FeaturedProduct * HandpickedProducts * ProductBestSellers * ProductCategory * ProductNew * ProductOnSale * ProductTopRated * ProductsByAttribute * Remove old base and render functions * Allow non-dynamic blocks and register category block * Fix products-by-attribute due to wrong naming * Remove no dev * test phpunit dir * Update testing framework * Update with new abstract classes and build in API * Undo edit to attribute block * Move edit mode * No need to support shortcodes * correct linting errors * Update tests/bootstrap.php Co-Authored-By: Albert Juhé Lluveras <aljullu@gmail.com> * Update code comment to make more sense. * Correct test package * docblock * Fix cancel button class * Fix classname schema * Set loading state so spinner is shown * Add placeholder element when no tags are selected * No tags placeholder * Update rest endpoints
2019-07-09 13:42:22 +00:00
if ( tags && tags.length > 0 ) {
query.tag = tags.join( ',' );
if ( tagOperator && 'all' === tagOperator ) {
query.tag_operator = 'and';
}
}
if ( orderby ) {
if ( 'price_desc' === orderby ) {
query.orderby = 'price';
query.order = 'desc';
} else if ( 'price_asc' === orderby ) {
query.orderby = 'price';
query.order = 'asc';
} else if ( 'title' === orderby ) {
query.orderby = 'title';
query.order = 'asc';
} else if ( 'menu_order' === orderby ) {
query.orderby = 'menu_order';
query.order = 'asc';
} else {
query.orderby = orderby;
}
}
if ( attributes && attributes.length > 0 ) {
query.attribute_term = attributes.map( ( { id } ) => id ).join( ',' );
query.attribute = attributes[ 0 ].attr_slug;
if ( attrOperator ) {
query.attribute_operator = 'all' === attrOperator ? 'and' : 'in';
}
}
// Toggle query parameters depending on block type.
switch ( name ) {
case 'woocommerce/product-best-sellers':
query.orderby = 'popularity';
break;
2018-12-14 19:45:19 +00:00
case 'woocommerce/product-top-rated':
2018-12-14 20:21:56 +00:00
query.orderby = 'rating';
2018-12-15 02:12:13 +00:00
break;
case 'woocommerce/product-on-sale':
query.on_sale = 1;
2018-12-14 20:21:56 +00:00
break;
case 'woocommerce/product-new':
query.orderby = 'date';
break;
case 'woocommerce/handpicked-products':
query.include = products;
query.per_page = products.length;
break;
}
return query;
}