diff --git a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/attributes.ts b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/attributes.ts
index d7194c6d347..cf1ec2d22be 100644
--- a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/attributes.ts
+++ b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/attributes.ts
@@ -12,6 +12,10 @@ export const blockAttributes: BlockAttributes = {
type: 'boolean',
default: false,
},
+ isDescendantOfAllProducts: {
+ type: 'boolean',
+ default: false,
+ },
};
export default blockAttributes;
diff --git a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/block.tsx b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/block.tsx
index c933a74fe33..544b020f9db 100644
--- a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/block.tsx
+++ b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/block.tsx
@@ -7,7 +7,11 @@ import {
useInnerBlockLayoutContext,
useProductDataContext,
} from '@woocommerce/shared-context';
-import { useColorProps, useTypographyProps } from '@woocommerce/base-hooks';
+import {
+ useColorProps,
+ useTypographyProps,
+ useSpacingProps,
+} from '@woocommerce/base-hooks';
import { withProductDataContext } from '@woocommerce/shared-hocs';
import type { HTMLAttributes } from 'react';
@@ -17,22 +21,43 @@ import type { HTMLAttributes } from 'react';
import './style.scss';
import type { BlockAttributes } from './types';
-const lowStockText = ( lowStock: number ): string => {
- return sprintf(
- /* translators: %d stock amount (number of items in stock for product) */
- __( '%d left in stock', 'woo-gutenberg-products-block' ),
- lowStock
- );
-};
-
-const stockText = ( inStock: boolean, isBackordered: boolean ): string => {
- if ( isBackordered ) {
+/**
+ * Get stock text based on stock. For example:
+ * - In stock
+ * - Out of stock
+ * - Available on backorder
+ * - 2 left in stock
+ *
+ * @param stockInfo Object containing stock information.
+ * @param stockInfo.isInStock Whether product is in stock.
+ * @param stockInfo.isLowStock Whether product is low in stock.
+ * @param stockInfo.lowStockAmount Number of items left in stock.
+ * @param stockInfo.isOnBackorder Whether product is on backorder.
+ * @return string Stock text.
+ */
+const getTextBasedOnStock = ( {
+ isInStock = false,
+ isLowStock = false,
+ lowStockAmount = null,
+ isOnBackorder = false,
+}: {
+ isInStock?: boolean;
+ isLowStock?: boolean;
+ lowStockAmount?: number | null;
+ isOnBackorder?: boolean;
+} ): string => {
+ if ( isLowStock && lowStockAmount !== null ) {
+ return sprintf(
+ /* translators: %d stock amount (number of items in stock for product) */
+ __( '%d left in stock', 'woo-gutenberg-products-block' ),
+ lowStockAmount
+ );
+ } else if ( isOnBackorder ) {
return __( 'Available on backorder', 'woo-gutenberg-products-block' );
+ } else if ( isInStock ) {
+ return __( 'In stock', 'woo-gutenberg-products-block' );
}
-
- return inStock
- ? __( 'In Stock', 'woo-gutenberg-products-block' )
- : __( 'Out of Stock', 'woo-gutenberg-products-block' );
+ return __( 'Out of stock', 'woo-gutenberg-products-block' );
};
type Props = BlockAttributes & HTMLAttributes< HTMLDivElement >;
@@ -43,8 +68,9 @@ export const Block = ( props: Props ): JSX.Element | null => {
const { product } = useProductDataContext();
const colorProps = useColorProps( props );
const typographyProps = useTypographyProps( props );
+ const spacingProps = useSpacingProps( props );
- if ( ! product.id || ! product.is_purchasable ) {
+ if ( ! product.id ) {
return null;
}
@@ -54,28 +80,39 @@ export const Block = ( props: Props ): JSX.Element | null => {
return (
- { lowStock
- ? lowStockText( lowStock )
- : stockText( inStock, isBackordered ) }
+ { getTextBasedOnStock( {
+ isInStock: inStock,
+ isLowStock: !! lowStock,
+ lowStockAmount: lowStock,
+ isOnBackorder: isBackordered,
+ } ) }
);
};
diff --git a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/edit.tsx b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/edit.tsx
index 162ab4311d8..83efbf324ef 100644
--- a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/edit.tsx
+++ b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/edit.tsx
@@ -24,7 +24,9 @@ const Edit = ( {
setAttributes,
context,
}: BlockEditProps< BlockAttributes > & { context: Context } ): JSX.Element => {
- const blockProps = useBlockProps();
+ const { style, ...blockProps } = useBlockProps( {
+ className: 'wc-block-components-product-stock-indicator',
+ } );
const blockAttrs = {
...attributes,
@@ -38,7 +40,15 @@ const Edit = ( {
);
return (
-
+
diff --git a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/supports.ts b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/supports.ts
index 6059f2454ee..6c6f9120303 100644
--- a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/supports.ts
+++ b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/supports.ts
@@ -2,17 +2,39 @@
* External dependencies
*/
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
+import {
+ // @ts-expect-error We check if this exists before using it.
+ // eslint-disable-next-line @wordpress/no-unsafe-wp-apis
+ __experimentalGetSpacingClassesAndStyles,
+} from '@wordpress/block-editor';
+
+/**
+ * Internal dependencies
+ */
+import sharedConfig from '../shared/config';
export const supports = {
- ...( isFeaturePluginBuild() && {
- color: {
- text: true,
- background: false,
- link: false,
+ ...sharedConfig.supports,
+ color: {
+ text: true,
+ background: true,
+ },
+ typography: {
+ fontSize: true,
+ lineHeight: true,
+ ...( isFeaturePluginBuild() && {
+ __experimentalFontWeight: true,
+ __experimentalFontFamily: true,
+ __experimentalFontStyle: true,
+ __experimentalTextTransform: true,
+ __experimentalTextDecoration: true,
+ __experimentalLetterSpacing: true,
+ } ),
+ },
+ ...( typeof __experimentalGetSpacingClassesAndStyles === 'function' && {
+ spacing: {
+ margin: true,
+ padding: true,
},
- typography: {
- fontSize: true,
- },
- __experimentalSelector: '.wc-block-components-product-stock-indicator',
} ),
};
diff --git a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/types.ts b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/types.ts
index b695a3b5f5d..c68e4b51c4e 100644
--- a/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/types.ts
+++ b/plugins/woocommerce-blocks/assets/js/atomic/blocks/product-elements/stock-indicator/types.ts
@@ -1,4 +1,5 @@
export interface BlockAttributes {
productId: number;
isDescendentOfQueryLoop: boolean;
+ isDescendantOfAllProducts: boolean;
}
diff --git a/plugins/woocommerce-blocks/src/BlockTypes/ProductStockIndicator.php b/plugins/woocommerce-blocks/src/BlockTypes/ProductStockIndicator.php
index 51ae70dc3fd..f95ad94951e 100644
--- a/plugins/woocommerce-blocks/src/BlockTypes/ProductStockIndicator.php
+++ b/plugins/woocommerce-blocks/src/BlockTypes/ProductStockIndicator.php
@@ -22,28 +22,6 @@ class ProductStockIndicator extends AbstractBlock {
*/
protected $api_version = '2';
- /**
- * Get block supports. Shared with the frontend.
- * IMPORTANT: If you change anything here, make sure to update the JS file too.
- *
- * @return array
- */
- protected function get_block_type_supports() {
- return array(
- 'color' =>
- array(
- 'link' => false,
- 'background' => false,
- 'text' => true,
- ),
- 'typography' =>
- array(
- 'fontSize' => true,
- ),
- '__experimentalSelector' => '.wc-block-components-product-stock-indicator',
- );
- }
-
/**
* Register script and style assets for the block type before it is registered.
*
@@ -125,7 +103,7 @@ class ProductStockIndicator extends AbstractBlock {
$classnames .= $is_on_backorder ? ' wc-block-components-product-stock-indicator--available-on-backorder ' : '';
$output = '';
- $output .= '