2018-12-13 17:19:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import Gridicon from 'gridicons';
|
|
|
|
import { registerBlockType } from '@wordpress/blocks';
|
|
|
|
import { RawHTML } from '@wordpress/element';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import '../css/product-category-block.scss';
|
|
|
|
import getShortcode from './utils/get-shortcode';
|
|
|
|
import ProductBestSellersBlock from './product-best-sellers';
|
|
|
|
import ProductByCategoryBlock from './product-category-block';
|
2018-12-14 19:45:19 +00:00
|
|
|
import ProductTopRatedBlock from './product-top-rated';
|
2018-12-14 23:47:16 +00:00
|
|
|
import ProductOnSaleBlock from './product-on-sale';
|
2018-12-13 17:19:55 +00:00
|
|
|
import sharedAttributes from './utils/shared-attributes';
|
|
|
|
|
|
|
|
const validAlignments = [ 'wide', 'full' ];
|
|
|
|
|
|
|
|
const getEditWrapperProps = ( attributes ) => {
|
|
|
|
const { align } = attributes;
|
|
|
|
if ( -1 !== validAlignments.indexOf( align ) ) {
|
|
|
|
return { 'data-align': align };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register and run the "Products by Category" block.
|
|
|
|
*/
|
|
|
|
registerBlockType( 'woocommerce/product-category', {
|
|
|
|
title: __( 'Products by Category', 'woo-gutenberg-products-block' ),
|
|
|
|
icon: 'category',
|
|
|
|
category: 'widgets',
|
|
|
|
keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ],
|
|
|
|
description: __(
|
|
|
|
'Display a grid of products from your selected categories.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
attributes: {
|
|
|
|
...sharedAttributes,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle for edit mode in the block preview.
|
|
|
|
*/
|
|
|
|
editMode: {
|
|
|
|
type: 'boolean',
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* How to order the products: 'date', 'popularity', 'price_asc', 'price_desc' 'rating', 'title'.
|
|
|
|
*/
|
|
|
|
orderby: {
|
|
|
|
type: 'string',
|
|
|
|
default: 'date',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getEditWrapperProps,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders and manages the block.
|
|
|
|
*/
|
|
|
|
edit( props ) {
|
|
|
|
return <ProductByCategoryBlock { ...props } />;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the block content in the post content. Block content is saved as a products shortcode.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
save( props ) {
|
|
|
|
const {
|
|
|
|
align,
|
|
|
|
} = props.attributes; /* eslint-disable-line react/prop-types */
|
|
|
|
return (
|
|
|
|
<RawHTML className={ align ? `align${ align }` : '' }>
|
|
|
|
{ getShortcode( props, 'woocommerce/product-category' ) }
|
|
|
|
</RawHTML>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
registerBlockType( 'woocommerce/product-best-sellers', {
|
|
|
|
title: __( 'Best Selling Products', 'woo-gutenberg-products-block' ),
|
|
|
|
icon: <Gridicon icon="stats-up-alt" />,
|
|
|
|
category: 'widgets',
|
|
|
|
keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ],
|
|
|
|
description: __(
|
|
|
|
'Display a grid of your all-time best selling products.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
attributes: {
|
|
|
|
...sharedAttributes,
|
|
|
|
},
|
|
|
|
getEditWrapperProps,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders and manages the block.
|
|
|
|
*/
|
|
|
|
edit( props ) {
|
|
|
|
return <ProductBestSellersBlock { ...props } />;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the block content in the post content. Block content is saved as a products shortcode.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
save( props ) {
|
|
|
|
const {
|
|
|
|
align,
|
|
|
|
} = props.attributes; /* eslint-disable-line react/prop-types */
|
|
|
|
return (
|
|
|
|
<RawHTML className={ align ? `align${ align }` : '' }>
|
|
|
|
{ getShortcode( props, 'woocommerce/product-best-sellers' ) }
|
|
|
|
</RawHTML>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} );
|
2018-12-14 19:45:19 +00:00
|
|
|
|
|
|
|
registerBlockType( 'woocommerce/product-top-rated', {
|
|
|
|
title: __( 'Top Rated Products', 'woo-gutenberg-products-block' ),
|
|
|
|
icon: <Gridicon icon="trophy" />,
|
|
|
|
category: 'widgets',
|
|
|
|
keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ],
|
|
|
|
description: __(
|
|
|
|
'Display a grid of your top rated products.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
attributes: {
|
|
|
|
...sharedAttributes,
|
|
|
|
},
|
|
|
|
getEditWrapperProps,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders and manages the block.
|
|
|
|
*/
|
|
|
|
edit( props ) {
|
|
|
|
return <ProductTopRatedBlock { ...props } />;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the block content in the post content. Block content is saved as a products shortcode.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
save( props ) {
|
|
|
|
const {
|
|
|
|
align,
|
|
|
|
} = props.attributes; /* eslint-disable-line react/prop-types */
|
|
|
|
return (
|
|
|
|
<RawHTML className={ align ? `align${ align }` : '' }>
|
|
|
|
{ getShortcode( props, 'woocommerce/product-top-rated' ) }
|
|
|
|
</RawHTML>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} );
|
2018-12-15 02:08:58 +00:00
|
|
|
|
2018-12-14 23:47:16 +00:00
|
|
|
registerBlockType( 'woocommerce/product-on-sale', {
|
|
|
|
title: __( 'On Sale Products', 'woo-gutenberg-products-block' ),
|
|
|
|
icon: <Gridicon icon="tag" />,
|
|
|
|
category: 'widgets',
|
|
|
|
keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ],
|
|
|
|
description: __(
|
|
|
|
'Display a grid of on sale products.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
attributes: {
|
|
|
|
...sharedAttributes,
|
|
|
|
/**
|
|
|
|
* How to order the products: 'date', 'popularity', 'price_asc', 'price_desc' 'rating', 'title'.
|
|
|
|
*/
|
|
|
|
orderby: {
|
|
|
|
type: 'string',
|
|
|
|
default: 'date',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getEditWrapperProps,
|
2018-12-15 16:41:28 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-14 23:47:16 +00:00
|
|
|
* Renders and manages the block.
|
|
|
|
*/
|
|
|
|
edit( props ) {
|
|
|
|
return <ProductOnSaleBlock { ...props } />;
|
|
|
|
},
|
2018-12-15 16:41:28 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-14 23:47:16 +00:00
|
|
|
* Save the block content in the post content. Block content is saved as a products shortcode.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
save( props ) {
|
|
|
|
const {
|
|
|
|
align,
|
|
|
|
} = props.attributes; /* eslint-disable-line react/prop-types */
|
|
|
|
return (
|
|
|
|
<RawHTML className={ align ? `align${ align }` : '' }>
|
|
|
|
{ getShortcode( props, 'woocommerce/product-on-sale' ) }
|
|
|
|
</RawHTML>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} );
|