2019-04-30 20:30:43 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { min } from 'lodash';
|
2019-08-27 15:25:32 +00:00
|
|
|
import { DEFAULT_COLUMNS, DEFAULT_ROWS } from '@woocommerce/block-settings';
|
2019-04-30 20:30:43 +00:00
|
|
|
|
2019-01-30 21:27:56 +00:00
|
|
|
export default function getQuery( blockAttributes, name ) {
|
|
|
|
const {
|
|
|
|
attributes,
|
|
|
|
attrOperator,
|
|
|
|
categories,
|
|
|
|
catOperator,
|
2019-07-09 13:42:22 +00:00
|
|
|
tags,
|
|
|
|
tagOperator,
|
2019-01-30 21:27:56 +00:00
|
|
|
orderby,
|
|
|
|
products,
|
|
|
|
} = blockAttributes;
|
2019-08-17 09:14:11 +00:00
|
|
|
const columns = blockAttributes.columns || DEFAULT_COLUMNS;
|
|
|
|
const rows = blockAttributes.rows || DEFAULT_ROWS;
|
2019-04-30 20:30:43 +00:00
|
|
|
const apiMax = Math.floor( 100 / columns ) * columns; // Prevent uneven final row.
|
2018-11-19 16:33:17 +00:00
|
|
|
|
|
|
|
const query = {
|
2018-11-26 16:03:26 +00:00
|
|
|
status: 'publish',
|
2019-04-30 20:30:43 +00:00
|
|
|
per_page: min( [ rows * columns, apiMax ] ),
|
2019-02-19 16:09:16 +00:00
|
|
|
catalog_visibility: 'visible',
|
2018-11-19 16:33:17 +00:00
|
|
|
};
|
|
|
|
|
2018-12-13 17:19:55 +00:00
|
|
|
if ( categories && categories.length ) {
|
2018-11-19 16:33:17 +00:00
|
|
|
query.category = categories.join( ',' );
|
2019-11-07 14:45:45 +00:00
|
|
|
if ( catOperator && catOperator === 'all' ) {
|
2019-02-28 18:07:38 +00:00
|
|
|
query.category_operator = 'and';
|
2018-12-17 20:16:01 +00:00
|
|
|
}
|
2018-11-19 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 13:42:22 +00:00
|
|
|
if ( tags && tags.length > 0 ) {
|
|
|
|
query.tag = tags.join( ',' );
|
2019-11-07 14:45:45 +00:00
|
|
|
if ( tagOperator && tagOperator === 'all' ) {
|
2019-07-09 13:42:22 +00:00
|
|
|
query.tag_operator = 'and';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 17:19:55 +00:00
|
|
|
if ( orderby ) {
|
2019-11-07 14:45:45 +00:00
|
|
|
if ( orderby === 'price_desc' ) {
|
2018-12-13 17:19:55 +00:00
|
|
|
query.orderby = 'price';
|
|
|
|
query.order = 'desc';
|
2019-11-07 14:45:45 +00:00
|
|
|
} else if ( orderby === 'price_asc' ) {
|
2018-12-13 17:19:55 +00:00
|
|
|
query.orderby = 'price';
|
|
|
|
query.order = 'asc';
|
2019-11-07 14:45:45 +00:00
|
|
|
} else if ( orderby === 'title' ) {
|
2018-12-13 17:19:55 +00:00
|
|
|
query.orderby = 'title';
|
|
|
|
query.order = 'asc';
|
2019-11-07 14:45:45 +00:00
|
|
|
} else if ( orderby === 'menu_order' ) {
|
2018-12-13 17:19:55 +00:00
|
|
|
query.orderby = 'menu_order';
|
|
|
|
query.order = 'asc';
|
|
|
|
} else {
|
|
|
|
query.orderby = orderby;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:26:47 +00:00
|
|
|
if ( attributes && attributes.length > 0 ) {
|
|
|
|
query.attribute_term = attributes.map( ( { id } ) => id ).join( ',' );
|
|
|
|
query.attribute = attributes[ 0 ].attr_slug;
|
2019-01-30 21:27:56 +00:00
|
|
|
|
|
|
|
if ( attrOperator ) {
|
2019-11-07 14:45:45 +00:00
|
|
|
query.attribute_operator = attrOperator === 'all' ? 'and' : 'in';
|
2019-01-30 21:27:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 00:10:17 +00:00
|
|
|
// Toggle query parameters depending on block type.
|
2018-12-13 17:19:55 +00:00
|
|
|
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;
|
2018-12-14 23:47:16 +00:00
|
|
|
case 'woocommerce/product-on-sale':
|
|
|
|
query.on_sale = 1;
|
2018-12-14 20:21:56 +00:00
|
|
|
break;
|
2018-12-18 19:45:49 +00:00
|
|
|
case 'woocommerce/product-new':
|
|
|
|
query.orderby = 'date';
|
|
|
|
break;
|
2018-12-22 00:10:17 +00:00
|
|
|
case 'woocommerce/handpicked-products':
|
|
|
|
query.include = products;
|
|
|
|
query.per_page = products.length;
|
|
|
|
break;
|
2018-11-19 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|