2019-01-30 21:27:56 +00:00
|
|
|
export default function getQuery( blockAttributes, name ) {
|
|
|
|
const {
|
|
|
|
attributes,
|
|
|
|
attrOperator,
|
|
|
|
categories,
|
|
|
|
catOperator,
|
|
|
|
orderby,
|
|
|
|
products,
|
|
|
|
} = blockAttributes;
|
2019-01-31 22:55:54 +00:00
|
|
|
const columns = blockAttributes.columns || wc_product_block_data.default_columns;
|
|
|
|
const rows = blockAttributes.rows || wc_product_block_data.default_rows;
|
2018-11-19 16:33:17 +00:00
|
|
|
|
|
|
|
const query = {
|
2018-11-26 16:03:26 +00:00
|
|
|
status: 'publish',
|
2018-11-19 16:33:17 +00:00
|
|
|
per_page: rows * columns,
|
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( ',' );
|
2018-12-17 20:16:01 +00:00
|
|
|
if ( catOperator && 'all' === catOperator ) {
|
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
|
|
|
}
|
|
|
|
|
2018-12-13 17:19:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-28 18:07:38 +00:00
|
|
|
query.attribute_operator = 'all' === attrOperator ? '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;
|
|
|
|
}
|