forked from woocommerce/woocommerce-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Cross-Sells product list (woocommerce#6645)
* Create Cross-Sells product list * Show “Read more” button for out-of-stock cross-sells products * Update assets/js/blocks/cart/inner-blocks/cart-cross-sells-products/block.tsx Co-authored-by: Thomas Roberts <[email protected]> * Update assets/js/blocks/cart/cart-cross-sells-product-list/index.tsx Co-authored-by: Thomas Roberts <[email protected]> * Remove obsolete isLoading and placeholderRows * Fix TS errors * Rename crossSellsProduct to product * Fix critical error * Lock “Cart Cross-Sells products” inner block * Update assets/js/blocks/cart/inner-blocks/cart-cross-sells-products/block.json Co-authored-by: Saad Tarhi <[email protected]> * Prevent moving of the Cross-Sells block Co-authored-by: Thomas Roberts <[email protected]> Co-authored-by: Saad Tarhi <[email protected]>
- Loading branch information
Showing
46 changed files
with
810 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
assets/js/blocks/cart/cart-cross-sells-product-list/cart-cross-sells-product.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { | ||
InnerBlockLayoutContextProvider, | ||
ProductDataContextProvider, | ||
} from '@woocommerce/shared-context'; | ||
import { ProductResponseItem } from '@woocommerce/type-defs/product-response'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Block as ProductImage } from '../../../atomic/blocks/product-elements/image/block'; | ||
import { Block as ProductName } from '../../../atomic/blocks/product-elements/title/block'; | ||
import { Block as ProductRating } from '../../../atomic/blocks/product-elements/rating/block'; | ||
import { Block as ProductSaleBadge } from '../../../atomic/blocks/product-elements/sale-badge/block'; | ||
import { Block as ProductPrice } from '../../../atomic/blocks/product-elements/price/block'; | ||
import { Block as ProductButton } from '../../../atomic/blocks/product-elements/button/block'; | ||
import AddToCartButton from '../../../atomic/blocks/product-elements/add-to-cart/block'; | ||
|
||
interface CrossSellsProductProps { | ||
product: ProductResponseItem; | ||
isLoading: boolean; | ||
} | ||
|
||
const CartCrossSellsProduct = ( { | ||
product, | ||
}: CrossSellsProductProps ): JSX.Element => { | ||
return ( | ||
<div className="cross-sells-product"> | ||
<InnerBlockLayoutContextProvider | ||
parentName={ 'woocommerce/cart-cross-sells-block' } | ||
parentClassName={ 'wp-block-cart-cross-sells-product' } | ||
> | ||
<ProductDataContextProvider | ||
// Setting isLoading to false, given this parameter is required. | ||
isLoading={ false } | ||
product={ product } | ||
> | ||
<div> | ||
<ProductImage | ||
className={ '' } | ||
showSaleBadge={ false } | ||
/> | ||
<ProductName | ||
align={ '' } | ||
headingLevel={ 2 } | ||
showProductLink={ true } | ||
/> | ||
<ProductRating /> | ||
<ProductSaleBadge /> | ||
<ProductPrice /> | ||
</div> | ||
{ product.is_in_stock ? ( | ||
<AddToCartButton /> | ||
) : ( | ||
<ProductButton /> | ||
) } | ||
</ProductDataContextProvider> | ||
</InnerBlockLayoutContextProvider> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CartCrossSellsProduct; |
37 changes: 37 additions & 0 deletions
37
assets/js/blocks/cart/cart-cross-sells-product-list/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { ProductResponseItem } from '@woocommerce/type-defs/product-response'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import CartCrossSellsProduct from './cart-cross-sells-product'; | ||
|
||
interface CrossSellsProductListProps { | ||
products: ProductResponseItem[]; | ||
className?: string | undefined; | ||
columns: number; | ||
} | ||
|
||
const CartCrossSellsProductList = ( { | ||
products, | ||
columns, | ||
}: CrossSellsProductListProps ): JSX.Element => { | ||
const crossSellsProducts = products.map( ( product, i ) => { | ||
if ( i >= columns ) return null; | ||
|
||
return ( | ||
<CartCrossSellsProduct | ||
// Setting isLoading to false, given this parameter is required. | ||
isLoading={ false } | ||
product={ product } | ||
key={ product.id } | ||
/> | ||
); | ||
} ); | ||
|
||
return <div>{ crossSellsProducts }</div>; | ||
}; | ||
|
||
export default CartCrossSellsProductList; |
25 changes: 25 additions & 0 deletions
25
assets/js/blocks/cart/inner-blocks/cart-cross-sells-block/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "woocommerce/cart-cross-sells-block", | ||
"version": "1.0.0", | ||
"title": "Cart Cross-Sells block", | ||
"description": "Shows the Cross-Sells block.", | ||
"category": "woocommerce", | ||
"supports": { | ||
"align": false, | ||
"html": false, | ||
"multiple": false, | ||
"reusable": false, | ||
"inserter": true | ||
}, | ||
"attributes": { | ||
"lock": { | ||
"type": "object", | ||
"default": { | ||
"move": true | ||
} | ||
} | ||
}, | ||
"parent": [ "woocommerce/cart-items-block" ], | ||
"textdomain": "woo-gutenberg-products-block", | ||
"apiVersion": 2 | ||
} |
41 changes: 41 additions & 0 deletions
41
assets/js/blocks/cart/inner-blocks/cart-cross-sells-block/edit.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import type { TemplateArray } from '@wordpress/blocks'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor'; | ||
|
||
export const Edit = (): JSX.Element => { | ||
const blockProps = useBlockProps( { | ||
className: 'wc-block-cart__cross-sells', | ||
} ); | ||
const defaultTemplate = [ | ||
[ | ||
'core/heading', | ||
{ | ||
content: __( | ||
'You may be interested in…', | ||
'woo-gutenberg-products-block' | ||
), | ||
level: 3, | ||
}, | ||
, | ||
[], | ||
], | ||
[ 'woocommerce/cart-cross-sells-products-block', {}, [] ], | ||
] as TemplateArray; | ||
|
||
return ( | ||
<div { ...blockProps }> | ||
<InnerBlocks template={ defaultTemplate } templateLock={ false } /> | ||
</div> | ||
); | ||
}; | ||
|
||
export const Save = (): JSX.Element => { | ||
return ( | ||
<div { ...useBlockProps.save() }> | ||
<InnerBlocks.Content /> | ||
</div> | ||
); | ||
}; |
24 changes: 24 additions & 0 deletions
24
assets/js/blocks/cart/inner-blocks/cart-cross-sells-block/frontend.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useStoreCart } from '@woocommerce/base-context/hooks'; | ||
|
||
interface Props { | ||
children?: JSX.Element | JSX.Element[]; | ||
className?: string; | ||
} | ||
|
||
const FrontendBlock = ( { | ||
children, | ||
className = '', | ||
}: Props ): JSX.Element | null => { | ||
const { crossSellsProducts, cartIsLoading } = useStoreCart(); | ||
|
||
if ( cartIsLoading || crossSellsProducts.length < 1 ) { | ||
return null; | ||
} | ||
|
||
return <div className={ className }>{ children }</div>; | ||
}; | ||
|
||
export default FrontendBlock; |
24 changes: 24 additions & 0 deletions
24
assets/js/blocks/cart/inner-blocks/cart-cross-sells-block/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Icon, column } from '@wordpress/icons'; | ||
import { registerExperimentalBlockType } from '@woocommerce/block-settings'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { Edit, Save } from './edit'; | ||
import metadata from './block.json'; | ||
|
||
registerExperimentalBlockType( metadata, { | ||
icon: { | ||
src: ( | ||
<Icon | ||
icon={ column } | ||
className="wc-block-editor-components-block-icon" | ||
/> | ||
), | ||
}, | ||
edit: Edit, | ||
save: Save, | ||
} ); |
31 changes: 31 additions & 0 deletions
31
assets/js/blocks/cart/inner-blocks/cart-cross-sells-products/block.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "woocommerce/cart-cross-sells-products-block", | ||
"version": "1.0.0", | ||
"title": "Cart Cross-Sells products", | ||
"description": "Shows the Cross-Sells products.", | ||
"category": "woocommerce", | ||
"supports": { | ||
"align": false, | ||
"html": false, | ||
"multiple": false, | ||
"reusable": false, | ||
"inserter": false, | ||
"lock": false | ||
}, | ||
"attributes": { | ||
"columns": { | ||
"type": "number", | ||
"default": 3 | ||
}, | ||
"lock": { | ||
"type": "object", | ||
"default": { | ||
"remove": true, | ||
"move": true | ||
} | ||
} | ||
}, | ||
"parent": [ "woocommerce/cart-cross-sells-block" ], | ||
"textdomain": "woo-gutenberg-products-block", | ||
"apiVersion": 2 | ||
} |
33 changes: 33 additions & 0 deletions
33
assets/js/blocks/cart/inner-blocks/cart-cross-sells-products/block.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useStoreCart } from '@woocommerce/base-context/hooks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import CartCrossSellsProductList from '../../cart-cross-sells-product-list'; | ||
import metadata from './block.json'; | ||
|
||
interface BlockProps { | ||
className?: string | undefined; | ||
columns: number; | ||
} | ||
|
||
const Block = ( { className, columns }: BlockProps ): JSX.Element => { | ||
const { crossSellsProducts } = useStoreCart(); | ||
|
||
if ( typeof columns === 'undefined' ) { | ||
columns = metadata.attributes.columns.default; | ||
} | ||
|
||
return ( | ||
<CartCrossSellsProductList | ||
className={ className } | ||
columns={ columns } | ||
products={ crossSellsProducts } | ||
/> | ||
); | ||
}; | ||
|
||
export default Block; |
Oops, something went wrong.