2019-08-17 09:14:11 +00:00
|
|
|
/**
|
2019-08-22 20:56:47 +00:00
|
|
|
* External dependencies
|
2019-08-17 09:14:11 +00:00
|
|
|
*/
|
2019-08-27 15:25:32 +00:00
|
|
|
import { DEFAULT_COLUMNS, DEFAULT_ROWS } from '@woocommerce/block-settings';
|
2019-08-17 09:14:11 +00:00
|
|
|
|
2019-02-07 20:26:47 +00:00
|
|
|
export default function getShortcode( props, name ) {
|
|
|
|
const blockAttributes = props.attributes;
|
2019-01-31 22:55:54 +00:00
|
|
|
const {
|
2019-02-07 20:26:47 +00:00
|
|
|
attributes,
|
|
|
|
attrOperator,
|
2019-01-31 22:55:54 +00:00
|
|
|
categories,
|
|
|
|
catOperator,
|
|
|
|
orderby,
|
|
|
|
products,
|
2019-02-07 20:26:47 +00:00
|
|
|
} = blockAttributes;
|
2019-08-17 09:14:11 +00:00
|
|
|
const columns = blockAttributes.columns || DEFAULT_COLUMNS;
|
|
|
|
const rows = blockAttributes.rows || DEFAULT_ROWS;
|
2018-11-19 16:33:17 +00:00
|
|
|
|
|
|
|
const shortcodeAtts = new Map();
|
|
|
|
shortcodeAtts.set( 'limit', rows * columns );
|
|
|
|
shortcodeAtts.set( 'columns', columns );
|
|
|
|
|
2018-12-13 17:19:55 +00:00
|
|
|
if ( categories && categories.length ) {
|
|
|
|
shortcodeAtts.set( 'category', categories.join( ',' ) );
|
2019-11-07 14:45:45 +00:00
|
|
|
if ( catOperator && catOperator === 'all' ) {
|
2018-12-17 20:16:01 +00:00
|
|
|
shortcodeAtts.set( 'cat_operator', 'AND' );
|
|
|
|
}
|
2018-12-13 17:19:55 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 20:26:47 +00:00
|
|
|
if ( attributes && attributes.length ) {
|
2019-09-05 15:09:31 +00:00
|
|
|
shortcodeAtts.set(
|
|
|
|
'terms',
|
|
|
|
attributes.map( ( { id } ) => id ).join( ',' )
|
|
|
|
);
|
2019-02-07 20:26:47 +00:00
|
|
|
shortcodeAtts.set( 'attribute', attributes[ 0 ].attr_slug );
|
2019-11-07 14:45:45 +00:00
|
|
|
if ( attrOperator && attrOperator === 'all' ) {
|
2019-02-07 20:26:47 +00:00
|
|
|
shortcodeAtts.set( 'terms_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
|
|
|
shortcodeAtts.set( 'orderby', 'price' );
|
|
|
|
shortcodeAtts.set( 'order', 'DESC' );
|
2019-11-07 14:45:45 +00:00
|
|
|
} else if ( orderby === 'price_asc' ) {
|
2018-12-13 17:19:55 +00:00
|
|
|
shortcodeAtts.set( 'orderby', 'price' );
|
|
|
|
shortcodeAtts.set( 'order', 'ASC' );
|
2019-11-07 14:45:45 +00:00
|
|
|
} else if ( orderby === 'date' ) {
|
2018-12-13 17:19:55 +00:00
|
|
|
shortcodeAtts.set( 'orderby', 'date' );
|
|
|
|
shortcodeAtts.set( 'order', 'DESC' );
|
|
|
|
} else {
|
|
|
|
shortcodeAtts.set( 'orderby', orderby );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Toggle shortcode atts depending on block type.
|
|
|
|
switch ( name ) {
|
|
|
|
case 'woocommerce/product-best-sellers':
|
|
|
|
shortcodeAtts.set( 'best_selling', '1' );
|
|
|
|
break;
|
2018-12-14 19:45:19 +00:00
|
|
|
case 'woocommerce/product-top-rated':
|
2018-12-15 01:48:46 +00:00
|
|
|
shortcodeAtts.set( 'orderby', 'rating' );
|
2018-12-14 20:21:56 +00:00
|
|
|
break;
|
2018-12-14 23:47:16 +00:00
|
|
|
case 'woocommerce/product-on-sale':
|
|
|
|
shortcodeAtts.set( 'on_sale', '1' );
|
|
|
|
break;
|
2018-12-18 19:45:49 +00:00
|
|
|
case 'woocommerce/product-new':
|
|
|
|
shortcodeAtts.set( 'orderby', 'date' );
|
|
|
|
shortcodeAtts.set( 'order', 'DESC' );
|
|
|
|
break;
|
2018-12-22 00:10:17 +00:00
|
|
|
case 'woocommerce/handpicked-products':
|
|
|
|
if ( ! products.length ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
shortcodeAtts.set( 'ids', products.join( ',' ) );
|
|
|
|
shortcodeAtts.set( 'limit', products.length );
|
|
|
|
break;
|
2019-01-04 21:34:37 +00:00
|
|
|
case 'woocommerce/product-category':
|
|
|
|
if ( ! categories || ! categories.length ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
break;
|
2019-02-07 20:26:47 +00:00
|
|
|
case 'woocommerce/products-by-attribute':
|
|
|
|
if ( ! attributes || ! attributes.length ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
break;
|
2018-11-19 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the shortcode string out of the set shortcode attributes.
|
|
|
|
let shortcode = '[products';
|
|
|
|
for ( const [ key, value ] of shortcodeAtts ) {
|
2019-11-01 13:56:14 +00:00
|
|
|
/* eslint-disable-line */
|
2018-11-19 16:33:17 +00:00
|
|
|
shortcode += ' ' + key + '="' + value + '"';
|
|
|
|
}
|
|
|
|
shortcode += ']';
|
|
|
|
|
|
|
|
return shortcode;
|
|
|
|
}
|