2018-12-13 17:19:55 +00:00
|
|
|
export default function getQuery( attributes, name ) {
|
2018-12-17 20:16:01 +00:00
|
|
|
const { categories, catOperator, columns, orderby, rows } = attributes;
|
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,
|
|
|
|
};
|
|
|
|
|
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 ) {
|
|
|
|
query.cat_operator = 'AND';
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Toggle shortcode atts 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;
|
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-11-19 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|